通过用户表单替换自动填充的文本
Posted
技术标签:
【中文标题】通过用户表单替换自动填充的文本【英文标题】:Replace auto filled text via userform 【发布时间】:2017-07-06 00:26:52 【问题描述】:我为我们在工作中使用的字母开发了一个用户表单,在输入所需数据后自动填写文档。
在当前时间点 - 当您点击 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】:读完你的问题后,我意识到你想做的是更新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
【讨论】:
抱歉,我想替换输入的信息,这样人们就不必为空白模板一直打开文档。 不能再调用宏吗?我相信它会覆盖现有数据 再次调用宏并输入信息只是将新数据添加到旧数据旁边。我只想显示新数据 - 有什么想法吗? 如果是这种情况,请用空字符串清除文本框,我编辑了我的答案,如果您有任何问题,请告诉我:) 所以我要把我的:.Bookmarks("Title").Range.Text = ComboBoxTitle.Value
改成.frmletter4.comboboxtitle.text=
????以上是关于通过用户表单替换自动填充的文本的主要内容,如果未能解决你的问题,请参考以下文章