Word VBA“标签未定义”如果书签存在命令
我对 VBA 很陌生,刚刚学习。这是我的情况和问题:
1)我创建了一个工作用户表单,其中包含链接到书签的文本和组合框 2)问题是,如果某些书签不存在,它就不起作用(并且项目将需要这样做:表单需要在并非所有书签都存在的文档上运行) 3)如果书签不存在,我希望表单停止给我错误消息,而只需填写该特定文档中存在的书签 4)这是代码:
Private Sub cmdOK_Click()
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists("cboYourName") Then
.Range.Text = cboYourName.Value
Else: GoTo 28
End If
If .Bookmarks.Exists("cboYourPhone") Then
.Range.Text = cboYourPhone.Value
Else: GoTo 32
End If
If .Bookmarks.Exists("cboYourFax") Then
.Range.Text = cboYourFax.Value
Else: GoTo 36
End If
If .Bookmarks.Exists("cboYourEmail") Then
.Range.Text = cboYourEmail.Value
Else: GoTo 40
End If
If .Bookmarks.Exists("txtContractName") Then
.Range.Text = txtContractName.Value
Else: GoTo 44
End If
If .Bookmarks.Exists("txtContractNumber") Then
.Range.Text = txtContractNumber.Value
Else: End
End If
End With
Application.ScreenUpdating = True
Unload Me
End Sub
4)我如何让它工作?????????
I am very new to VBA and just learning. Here's my situation and problem:
1) I created a working userform with text and comboboxes linking to bookmarks
2) Problem is that it doesn't work if some bookmarks don't exist (and the project will require this: the form will need to run on documents where not all bookmarks are present)
3) I would like the form stop giving me error messages if bookmarks arent there and just fill out the ones that are existing in that particular ocument
4) Here's the Code:
Private Sub cmdOK_Click()
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists("cboYourName") Then
.Range.Text = cboYourName.Value
Else: GoTo 28
End If
If .Bookmarks.Exists("cboYourPhone") Then
.Range.Text = cboYourPhone.Value
Else: GoTo 32
End If
If .Bookmarks.Exists("cboYourFax") Then
.Range.Text = cboYourFax.Value
Else: GoTo 36
End If
If .Bookmarks.Exists("cboYourEmail") Then
.Range.Text = cboYourEmail.Value
Else: GoTo 40
End If
If .Bookmarks.Exists("txtContractName") Then
.Range.Text = txtContractName.Value
Else: GoTo 44
End If
If .Bookmarks.Exists("txtContractNumber") Then
.Range.Text = txtContractNumber.Value
Else: End
End If
End With
Application.ScreenUpdating = True
Unload Me
End Sub
4) How do I get this to work?????????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你已经很接近了。首先,避免 Goto 语句。在您的代码中,很难说出您的意思。我认为错误来自 Goto 语句。它的参数是一个标签,而不是行号。其次,避免使用End。最好有一个关闭程序。也就是说,该代码适用于任意数量的 Exists 语句。
但是,请注意,每个找到的书签都会完全替换文档的内容,包括随后找到的书签。这就是你的意思吗?
I think you're close. First, avoid Goto statements. In your code, it's hard to tell what you mean to do. I think the errors are from the Goto statements. Its parameter is a label, not a line number. Second, avoid using End. It's better to have a closing routine. That said, the code works with any number of Exists statements.
However, be aware that each found bookmark completely replaces the content of the document, including subsequently found bookmarks. Is that what you mean to do?