程序求教,关于实时错误91
											我写了一段程序,有一张现成的word,里面有很多空格需要写入数据,我通过给每个空格添加书签的方式来写入数据,每个数据都有自己的上下限范围,现在要求在写入一个数据后然后回车的 时候,对写入该空格的数据进行判断,是否在规定的上下限范围内,如果不在范围内那么该空格写入的数据字体标成
红色。我想在If KeyAscii = 13 Then 下面加下面这个代码来给第一个空格写入的数据判断和标颜色。但是提示实时错误91,说是对象变量或者with块没有设置。帮我看下怎么写。
If Val(Text1(0).Text) >= 0 And Val(Text1(0).Text) <= 100 Then
worddoc.Range(worddoc.Bookmarks("MARKT0").Start, worddoc.Bookmarks("MARKT0").Start + Len(Text1(0).Text)).Font.Color = wdColorRed
End If
下面是全代码
程序代码:
Option Explicit
Private Sub command2_Click(Index As Integer)
Dim wordapp As Word.Application
Dim worddoc As Word.Document
Dim path
Set wordapp = CreateObject("word.Application")
wordapp.Visible = True
Set worddoc = Word.Application.Documents.Open("E:\sw_data\vb\PROJECT\RDOM4 Test Data Collection System\UDS")
'For i = 0 To 47
'worddoc.Bookmarks(i).Range.Text = Text1(i).Text
'Next i
worddoc.Bookmarks("MARKT0").Range.Text = Text1(0).Text
worddoc.Bookmarks("MARKT0").Range.Underline = True
worddoc.Bookmarks("MARKT1").Range.Text = Text1(1).Text
worddoc.Bookmarks("MARKT1").Range.Underline = True
worddoc.Bookmarks("MARKT2").Range.Text = Text1(2).Text
worddoc.Bookmarks("MARKT3").Range.Text = Text1(3).Text
worddoc.Bookmarks("MARKT4").Range.Text = Text1(4).Text
worddoc.Bookmarks("MARKT5").Range.Text = Text1(5).Text
worddoc.Bookmarks("MARKT6").Range.Text = Text1(6).Text
worddoc.Bookmarks("MARKT7").Range.Text = Text1(7).Text
worddoc.Bookmarks("MARKT8").Range.Text = Text1(8).Text
worddoc.Bookmarks("MARKT9").Range.Text = Text1(9).Text
worddoc.Bookmarks("MARKT10").Range.Text = Text1(10).Text
worddoc.Bookmarks("MARKT11").Range.Text = Text1(11).Text
worddoc.Bookmarks("MARKT12").Range.Text = Text1(12).Text
worddoc.Bookmarks("MARKT13").Range.Text = Text1(13).Text
worddoc.Bookmarks("MARKT14").Range.Text = Text1(14).Text
worddoc.Bookmarks("MARKT15").Range.Text = Text1(15).Text
worddoc.Bookmarks("MARKT16").Range.Text = Text1(16).Text
worddoc.Bookmarks("MARKT17").Range.Text = Text1(17).Text
worddoc.Bookmarks("MARKT18").Range.Text = Text1(18).Text
worddoc.Bookmarks("MARKT19").Range.Text = Text1(19).Text
worddoc.Bookmarks("MARKT20").Range.Text = Text1(20).Text
worddoc.Bookmarks("MARKT21").Range.Text = Text1(21).Text
worddoc.Bookmarks("MARKT22").Range.Text = Text1(22).Text
worddoc.Bookmarks("MARKT23").Range.Text = Text1(23).Text
worddoc.Bookmarks("MARKT24").Range.Text = Text1(24).Text
worddoc.Bookmarks("MARKT25").Range.Text = Text1(25).Text
worddoc.Bookmarks("MARKT26").Range.Text = Text1(26).Text
worddoc.Bookmarks("MARKT27").Range.Text = Text1(27).Text
worddoc.Bookmarks("MARKT28").Range.Text = Text1(28).Text
worddoc.Bookmarks("MARKT29").Range.Text = Text1(29).Text
worddoc.Bookmarks("MARKT30").Range.Text = Text1(30).Text
worddoc.Bookmarks("MARKT31").Range.Text = Text1(31).Text
worddoc.Bookmarks("MARKT32").Range.Text = Text1(32).Text
worddoc.Bookmarks("MARKT33").Range.Text = Text1(33).Text
worddoc.Bookmarks("MARKT34").Range.Text = Text1(34).Text
worddoc.Bookmarks("MARKT35").Range.Text = Text1(35).Text
worddoc.Bookmarks("MARKT36").Range.Text = Text1(36).Text
worddoc.Bookmarks("MARKT37").Range.Text = Text1(37).Text
worddoc.Bookmarks("MARKT38").Range.Text = Text1(38).Text
worddoc.Bookmarks("MARKT39").Range.Text = Text1(39).Text
worddoc.Bookmarks("MARKT40").Range.Text = Text1(40).Text
worddoc.Bookmarks("MARKT41").Range.Text = Text1(41).Text
worddoc.Bookmarks("MARKT42").Range.Text = Text1(42).Text
worddoc.Bookmarks("MARKT43").Range.Text = Text1(43).Text
worddoc.Bookmarks("MARKT44").Range.Text = Text1(44).Text
worddoc.Bookmarks("MARKT45").Range.Text = Text1(45).Text
worddoc.Bookmarks("MARKT46").Range.Text = Text1(46).Text
worddoc.Bookmarks("MARKT47").Range.Text = Text1(47).Text
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 47
Text1(i).Text = ""
Next i
End Sub
Private Sub text1_KeyPress(Index As Integer, KeyAscii As Integer)
Dim wordapp As Word.Application
Dim worddoc As Word.Document
'for text when input non-enter and non-num.
 If KeyAscii <> 13 And KeyAscii < 48 And KeyAscii <> 8 And KeyAscii <> 45 And KeyAscii <> 46 Then
        KeyAscii = 0
        MsgBox "Only enter number", 48, "Reminder"
  ElseIf KeyAscii > 57 Then
        KeyAscii = 0
        MsgBox "Only enter number ", 48, "Reminder"
  End If
If KeyAscii = 13 Then             ' for text when input enter then focus jump to next
' If Val(Text1(0).Text) >= 0 And Val(Text1(0).Text) <= 100 Then
 'worddoc.Range(worddoc.Bookmarks("MARKT0").Start, worddoc.Bookmarks("MARKT0").Start + Len(Text1(0).Text)).Font.Color = wdColorRed
 ' End If
      
  If Index < 9 Then
      If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Text1(Index + 1).SetFocus
      Exit Sub
      End If
      
      
  End If
  
  If Index = 9 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Combo1(0).SetFocus
      Exit Sub
      End If
      
   End If
   
      
      
      
   If Index > 9 And Index < 12 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Text1(Index + 1).SetFocus
      Exit Sub
      End If
      
   End If
   
   
   
   If Index = 12 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Combo1(1).SetFocus
      Exit Sub
      End If
      
   End If
   
   
   If Index > 12 And Index < 20 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Text1(Index + 1).SetFocus
      Exit Sub
      End If
      
   End If
   
   
   
   If Index = 20 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Combo1(3).SetFocus
      Exit Sub
      End If
      
   End If
  
  
  
  
    If Index > 20 And Index < 28 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Text1(Index + 1).SetFocus
      Exit Sub
      End If
      
   End If
   
   
   
   If Index = 28 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Combo1(4).SetFocus
      Exit Sub
      End If
      
   End If
  
  
  
  
     If Index > 28 And Index < 37 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Text1(Index + 1).SetFocus
      Exit Sub
      End If
      
   End If
   
   
   
   If Index = 37 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Combo1(5).SetFocus
      Exit Sub
      End If
      
   End If
  
  
     If Index > 37 And Index < 46 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Text1(Index + 1).SetFocus
      Exit Sub
      End If
      
   End If
   
   
   
   If Index = 46 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
       Combo1(6).SetFocus
      Exit Sub
      End If
      
   End If
  
  
  
     If Index = 47 Then
       If Text1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Text1(Index).SetFocus
      Exit Sub
      Else
      
      Command1(0).SetFocus
      
      
      Exit Sub
        End If
    
     End If
  
 
 
  End If
End Sub
Private Sub combo1_KeyPress(Index As Integer, KeyAscii As Integer)    'for combo when input enter then goto next
If KeyAscii = 13 Then
  If Index = 0 Then
      If Combo1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Combo1(Index).SetFocus
      Exit Sub
      Else
       Text1(10).SetFocus
      Exit Sub
      End If
  End If
  If Index = 1 Then
      If Combo1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Combo1(Index).SetFocus
      Exit Sub
      Else
       Combo1(2).SetFocus
      Exit Sub
      End If
  End If
  If Index = 2 Then
      If Combo1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Combo1(Index).SetFocus
      Exit Sub
      Else
       Text1(13).SetFocus
      Exit Sub
      End If
  End If
  If Index = 3 Then
      If Combo1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Combo1(Index).SetFocus
      Exit Sub
      Else
       Text1(21).SetFocus
      Exit Sub
      End If
  End If
  If Index = 4 Then
      If Combo1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Combo1(Index).SetFocus
      Exit Sub
      Else
       Text1(29).SetFocus
      Exit Sub
      End If
  End If
  
  
  
    If Index = 5 Then
      If Combo1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Combo1(Index).SetFocus
      Exit Sub
      Else
       Text1(38).SetFocus
      Exit Sub
      End If
  End If
  
  
  
     If Index = 6 Then
      If Combo1(Index).Text = "" Then
      MsgBox "It can't be blank", vbOKOnly, "Reminder"
      Combo1(Index).SetFocus
      Exit Sub
      Else
       Text1(47).SetFocus
      Exit Sub
      End If
     End If
  
  
End If
  
  End Sub
  
[ 本帖最后由 zys304 于 2015-5-2 21:14 编辑 ]



											
	    

	

