| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 638 人关注过本帖
标题:程序求教,关于实时错误91
只看楼主 加入收藏
zys304
Rank: 1
等 级:新手上路
帖 子:5
专家分:4
注 册:2015-1-11
结帖率:0
收藏
 问题点数:0 回复次数:3 
程序求教,关于实时错误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 编辑 ]
搜索更多相关主题的帖子: Text1 
2015-05-02 21:11
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
你确定有wdColorRed这个常数吗?我只知道有vbColorRed.如果这个常数没问题就不知道怎么回事了。
2015-05-02 22:17
zys304
Rank: 1
等 级:新手上路
帖 子:5
专家分:4
注 册:2015-1-11
收藏
得分:0 
没有人回答啊。。不知道是不是worddoc变量没有做全局变量的缘故
2015-05-03 21:16
lianyicq
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:26
帖 子:737
专家分:3488
注 册:2013-1-26
收藏
得分:0 
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
这句判断没有问题。
你的想法是正确的,试试就知道了

Dim wordapp As Word.Application
Dim worddoc As Word.Document
定义在option explicit 之后试试
KeyPress事件中有重复定义.


[ 本帖最后由 lianyicq 于 2015-5-4 14:12 编辑 ]

大开眼界
2015-05-04 10:22
快速回复:程序求教,关于实时错误91
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020245 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved