【问题标题】:Call UpdateBookmark & useAforB as Boolean code assistance调用 UpdateBookmark 和 useAforB 作为布尔代码帮助
【发布时间】:2017-07-07 00:29:35
【问题描述】:

在本网站上的一些人的帮助下,我一直在整理一些代码。我遇到了一些障碍 - 我使用 Call UpdateBookmark 函数将以前输入的信息替换到 word 文档中,但我在现有代码中实现这一点时遇到问题。

Private Sub cmdOk_Click()
    Dim useAforB As Boolean
    useAforB = CheckBox1.Value

    Application.ScreenUpdating = False
    With ActiveDocument
        Call UpdateBookmark("Lodge", ComboBoxLodge.Value)
        Call UpdateBookmark("Form", tbForm.Value)
        Call UpdateBookmark("Form2", tbForm.Value)
        Call UpdateBookmark("AGN", tbGN.Value)
        Call UpdateBookmark("AFN", tbFN.Value)
        Call UpdateBookmark("LGN", useAforB, _
                                     tbGN.Value, TBLPGN.Value)
        Call UpdateBookmark("RGN", useAforB, _
                                     tbGN.Value, TBLPGN.Value)
        Call UpdateBookmark("LFN", useAforB, _
                                     tbFN.Value, TBLPFN.Value)
        Call UpdateBookmark("RFN", useAforB, _
                                     tbFN.Value, TBLPFN.Value)
        Call UpdateBookmark("DOB", tbDOB.Value)
        Call UpdateBookmark("LT", cbLT.Value)
        Call UpdateBookmark("PN", tbPN.Value)
        Call UpdateBookmark("PN2", tbPN.Value)
        Call UpdateBookmark("PN3", tbPN.Value)
        Call UpdateBookmark("PN4", tbPN.Value)
        Call UpdateBookmark("Issued", tbissue.Value)
        Call UpdateBookmark("Expiry", tbexpiry.Value)
        Call UpdateBookmark("LTD", tbLTD.Value)
        Call UpdateBookmark("LTD2", tbLTD.Value)
        Call UpdateBookmark("Narrative", tbNarrative.Value)
        Call UpdateBookmark("PRR", tbPRR.Value)
        Call UpdateBookmark("Recommendation", cbRecommendation.Value)
    End With
    Application.ScreenUpdating = True
    Unload Me
End Sub


Sub UpdateBookmark(BookmarkToUpdate As String, TextAtBookmark As String)
   Dim BookmarkRange As Range
   Set BookmarkRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
   BookmarkRange.Text = TextAtBookmark
   ActiveDocument.Bookmarks.Add BookmarkToUpdate, BookmarkRange

End Sub

每当我尝试运行代码时,我都会收到一条错误消息,显示为Compile error: ByRef argument type mismatch - 有人可以给我一些帮助吗?谢谢大家。

为清楚起见添加了整个代码

Option Explicit
Private Sub CheckBox1_Click()
    Dim en As Boolean
    en = Not CheckBox1.Value
    EnableControls Array(TBLPGN, TBLPFN), en

    If CheckBox1.Value = True Then ComboBoxLodge.Value = "Applicant"
    If CheckBox1.Value = False Then ComboBoxLodge.Value = "Lodging parent"

End Sub


'utility sub: enable/disable controls
Private Sub EnableControls(cons, bEnable As Boolean)
    Dim con
    For Each con In cons
        With con
            .Enabled = bEnable
            .BackColor = IIf(bEnable, vbWhite, RGB(200, 200, 200))
        End With
    Next con
End Sub
Private Sub cmdCancel_Click()
    Unload Me
End Sub

Private Sub cmdClear_Click()
tbForm.Value = Null
tbFN.Value = Null
tbGN.Value = Null
tbdob.Value = Null
cbLT.Value = Null
tbPN.Value = Null
tbissue.Value = Null
tbexpiry.Value = Null
tbLTD.Value = Null
tbNarrative.Value = Null
tbPRR.Value = Null
cbRecommendation.Value = Null
CheckBox1.Value = False
ComboBoxLodge.Value = Null
End Sub

Private Sub cmdOk_Click()
    Dim useAforB As Boolean
    useAforB = CheckBox1.Value

    Application.ScreenUpdating = False
    With ActiveDocument
        Call UpdateBookmark("Lodge", ComboBoxLodge.Value)
        Call UpdateBookmark("Form", tbForm.Value)
        Call UpdateBookmark("Form2", tbForm.Value)
        Call UpdateBookmark("AGN", tbGN.Value)
        Call UpdateBookmark("AFN", tbFN.Value)
        Call UpdateBookmark("LGN" useAforB _
                                     tbGN.Value, TBLPGN.Value)
        Call UpdateBookmark("RGN", useAforB, _
                                     tbGN.Value, TBLPGN.Value)
        Call UpdateBookmark("LFN", useAforB, _
                                     tbFN.Value, TBLPFN.Value)
        Call UpdateBookmark("RFN", useAforB, _
                                     tbFN.Value, TBLPFN.Value)
        Call UpdateBookmark("DOB", tbdob.Value)
        Call UpdateBookmark("LT", cbLT.Value)
        Call UpdateBookmark("PN", tbPN.Value)
        Call UpdateBookmark("PN2", tbPN.Value)
        Call UpdateBookmark("PN3", tbPN.Value)
        Call UpdateBookmark("PN4", tbPN.Value)
        Call UpdateBookmark("Issued", tbissue.Value)
        Call UpdateBookmark("Expiry", tbexpiry.Value)
        Call UpdateBookmark("LTD", tbLTD.Value)
        Call UpdateBookmark("LTD2", tbLTD.Value)
        Call UpdateBookmark("Narrative", tbNarrative.Value)
        Call UpdateBookmark("PRR", tbPRR.Value)
        Call UpdateBookmark("Recommendation", cbRecommendation.Value)
    End With
    Application.ScreenUpdating = True
    Unload Me
End Sub


Sub UpdateBookmark(BookmarkToUpdate As String, TextAtBookmark As String)
   Dim BookmarkRange As Range
   Set BookmarkRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
   BookmarkRange.Text = TextAtBookmark
   ActiveDocument.Bookmarks.Add BookmarkToUpdate, BookmarkRange

End Sub

Private Sub Tbform_Change()
    tbForm = UCase(tbForm)
End Sub


Private Sub Tbpn_Change()
    tbPN = UCase(tbPN)
End Sub


Private Sub tbPRR_Change()
    tbPRR = UCase(tbPRR)
End Sub

Private Sub UserForm_Initialize()
    With cbLT
        .AddItem "lost"
        .AddItem "stolen"
    End With
    With cbRecommendation
        .AddItem "I believe there is an entitlement to have the l/t flag turned off as the applicant has not contributed to the loss of Passport number: "
        .AddItem "I believe there is no entitlement to have the l/t flag turned off as the applicant has contributed to the loss of Passport number:  "
    End With
    With ComboBoxLodge
        .AddItem "Lodging parent"
        .AddItem "Applicant"
     End With
     With CheckBox1
        CheckBox1.Value = True
    End With
lbl_Exit:
    Exit Sub
End Sub

【问题讨论】:

  • UpdateBookmark 期望有两个字符串类型的参数,但在某些情况下,您会传递四个参数:不清楚其中的意图是什么。你的意思是像Call UpdateBookmark("LGN", IIf(useAforB, tbGN.Value, TBLPGN.Value)) 这样的东西吗?
  • 嘿@TimWilliams,尝试了您的代码并完全修复了它 - 您是一个传奇(想把它写下来作为答案,以便我可以选择您的答案?)

标签: vba call ms-word userform autofill


【解决方案1】:

您的 Sub UpdateBookmark 需要两个字符串类型的参数,但在某些情况下,您会传递四个参数:不清楚其中的意图是什么。

你的意思是像

Call UpdateBookmark("LGN", IIf(useAforB, tbGN.Value, TBLPGN.Value))

?

【讨论】:

  • 完美就是我所需要的,非常感谢您的帮助:)
猜你喜欢
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多