Word VBA“标签未定义”如果书签存在命令

发布于 2024-10-05 07:49:12 字数 1161 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2024-10-12 07:49:12

我想你已经很接近了。首先,避免 Goto 语句。在您的代码中,很难说出您的意思。我认为错误来自 Goto 语句。它的参数是一个标签,而不是行号。其次,避免使用End。最好有一个关闭程序。也就是说,该代码适用于任意数量的 Exists 语句。

Private Sub cmdOK_Click()
    Application.ScreenUpdating = False
    With ActiveDocument
        If .Bookmarks.Exists("cboYourName") Then
            .Range.Text = "cboYourName text."
        Else
            Debug.Print "Bookmark exists."
        End If

        If .Bookmarks.Exists("cboYourPhone") Then
           .Range.Text = "cboYourPhone text"
        Else
            Debug.Print "Bookmark does not exists."
        End If
    End With

    Application.ScreenUpdating = True
    Unload Me
End Sub

但是,请注意,每个找到的书签都会完全替换文档的内容,包括随后找到的书签。这就是你的意思吗?

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.

Private Sub cmdOK_Click()
    Application.ScreenUpdating = False
    With ActiveDocument
        If .Bookmarks.Exists("cboYourName") Then
            .Range.Text = "cboYourName text."
        Else
            Debug.Print "Bookmark exists."
        End If

        If .Bookmarks.Exists("cboYourPhone") Then
           .Range.Text = "cboYourPhone text"
        Else
            Debug.Print "Bookmark does not exists."
        End If
    End With

    Application.ScreenUpdating = True
    Unload Me
End Sub

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文