Вызовите UpdateBookmark и используйте AforB в качестве помощи с логическим кодом
Я добавляю код вместе с помощью людей на этом сайте. Я наткнулся на небольшую загадку - я использую 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
1 ответ
Решение
Ваш Sub UpdateBookmark
ожидайте два аргумента типа String, но в некоторых случаях вы передаете четыре аргумента: неясно, что там происходит.
Вы имели в виду что-то вроде
Call UpdateBookmark("LGN", IIf(useAforB, tbGN.Value, TBLPGN.Value))
?