通过userform替换自动填充的文本

时间:2017-07-06 00:26:53

标签: vba word-vba userform autofill

我为工作中使用的字母开发了一个用户表单,在输入所需数据后自动填充文档。

在当前时间点 - 当您点击OK时,将输入数据并且数据将填入表格。有些用户只是试图在已经填写的表单顶部输入信息,并将先前输入的数据堆叠到信中。

问题:如何获取用户表单来替换输入的数据,而不是添加输入的数据。

因此,如果我输入John Wayne的名字,请填写我的信并决定在同一个打开的文档上写另一封信 - 如何重新打开我的宏,填充数据,然后覆盖上一封信的所有先前信息。

    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
        .Bookmarks("Lodge").Range.Text = ComboBoxLodge.Value
        .Bookmarks("Form").Range.Text = tbForm.Value
        .Bookmarks("Form2").Range.Text = tbForm.Value
        .Bookmarks("AGN").Range.Text = tbGN.Value
        .Bookmarks("AFN").Range.Text = tbFN.Value
        .Bookmarks("LGN").Range.Text = IIf(useAforB, _
                                     tbGN.Value, TBLPGN.Value)
        .Bookmarks("RGN").Range.Text = IIf(useAforB, _
                                     tbGN.Value, TBLPGN.Value)
        .Bookmarks("LFN").Range.Text = IIf(useAforB, _
                                     tbFN.Value, TBLPFN.Value)
        .Bookmarks("RFN").Range.Text = IIf(useAforB, _
                                     tbFN.Value, TBLPFN.Value)
        .Bookmarks("DOB").Range.Text = tbDOB.Value
        .Bookmarks("LT").Range.Text = cbLT.Value
        .Bookmarks("PN").Range.Text = tbPN.Value
        .Bookmarks("PN2").Range.Text = tbPN.Value
        .Bookmarks("PN3").Range.Text = tbPN.Value
        .Bookmarks("PN4").Range.Text = tbPN.Value
        .Bookmarks("Issued").Range.Text = tbissue.Value
        .Bookmarks("Expiry").Range.Text = tbexpiry.Value
        .Bookmarks("LTD").Range.Text = tbLTD.Value
        .Bookmarks("LTD2").Range.Text = tbLTD.Value
        .Bookmarks("Narrative").Range.Text = tbNarrative.Value
        .Bookmarks("PRR").Range.Text = tbPRR.Value
        .Bookmarks("Recommendation").Range.Text = cbRecommendation.Value
    End With
    Application.ScreenUpdating = True
    Unload Me
End Sub


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

Private Sub Tbfn_Change()
    tbFN = UCase(tbFN)
End Sub
Private Sub Tblpfn_Change()
    TBLPFN = UCase(TBLPFN)
End Sub

Private Sub Tbpn_Change()
    tbPN = UCase(tbPN)
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

Public Sub AutoOpen()
    frmminute.Show
End Sub

Sub CallUF()
Dim oFrm As frmminute
  Set oFrm = New frmminute
  oFrm.Show
  Unload oFrm
  Set oFrm = Nothing
lbl_Exit:
  Exit Sub
End Sub

Sub AutoNew()
  CallUF
lbl_Exit:
Exit Sub
End Sub

新代码当前遇到运行时错误:

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

Application.ScreenUpdating = False
With ActiveDocument
    Call UpdateBookmark("Title", ComboBoxTitle.Value)
    Call UpdateBookmark("GN", TextBoxGN.Value)
    Call UpdateBookmark("FN", TextBoxFN.Value)
    Call UpdateBookmark("FN2", TextBoxFN.Value)
    Call UpdateBookmark("Street", TextBoxStreet.Value)
    Call UpdateBookmark("suburb", TextBoxSuburb.Value)
    Call UpdateBookmark("postcode", TextBoxpostcode.Value)
    Call UpdateBookmark("state", ComboBoxState.Value)
    Call UpdateBookmark("street2", .Range.Text = IIf(useAforB, _
                                 TextBoxStreet.Value, TextBoxStreet2.Value))

    Call UpdateBookmark("Suburb2", .Range.Text = IIf(useAforB, _
                                 TextBoxSuburb.Value, TextBoxSuburb2.Value))
    Call UpdateBookmark("State2", .Range.Text = IIf(useAforB, _
                                 ComboBoxState.Value, ComboBoxState2.Value))
    Call UpdateBookmark("PostCode2", .Range.Text = IIf(useAforB, _
                                 TextBoxpostcode.Value, TextBoxPostcode2.Value))
    Call UpdateBookmark("CD", TextBoxCD.Value)
    Call UpdateBookmark("MPN", TextboxMPN.Value)
    Call UpdateBookmark("MPN2", TextboxMPN.Value)
    Call UpdateBookmark("MPN3", TextboxMPN.Value)
    Call UpdateBookmark("MPN4", TextboxMPN.Value)
    Call UpdateBookmark("MPN5", TextboxMPN.Value)
    Call UpdateBookmark("MPDD", TextBoxMPDD.Value)
    Call UpdateBookmark("NPN", TextBoxNPN.Value)
    Call UpdateBookmark("NPDD", TextBoxNPDD.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

1 个答案:

答案 0 :(得分:2)

在阅读完您的问题之后,我意识到您想要做的是更新word文档中的书签。

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

   Application.ScreenUpdating = False

   Call UpdateBookmark("Lodge", ComboBoxLodge.Value)
   Call UpdateBookmark("Form", tbForm.Value)
   'Do for the rest.....

   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
相关问题