我在Word中创建了一个用户形式,用顾问的姓名,地址等自动填写合同。但是,我有多个合同,我发送给每个顾问,我希望能够输入一次信息并填写所有合同。
我试着寻找这是如何运作的,但我一直无法将我学到的点点滴滴给成功的执行。
以下是我的用户表单在单个文档中的代码,如果它是相关的 - 如果我甚至可以使用这个确切的表单填充多个文档,那将节省大量时间。
Private Sub CancelButton_Click()
ConsultantInfo.Hide
End Sub
Private Sub FillButton_Click()
Dim consultantName1 As Range
Set consultantName1 = ActiveDocument.Bookmarks("ConsultantName1").Range
Dim consultantName2 As Range
Set consultantName2 = ActiveDocument.Bookmarks("ConsultantName2").Range
Dim consultantName3 As Range
Set consultantName3 = ActiveDocument.Bookmarks("ConsultantName3").Range
Dim consultantNameCaps As Range
Set consultantNameCaps =
ActiveDocument.Bookmarks("ConsultantNameCaps").Range
consultantName1.Text = Me.TextName.Value
consultantName2.Text = Me.TextName.Value
consultantName3.Text = Me.TextName.Value
consultantNameCaps.Text = Me.TextName.Value
consultantNameCaps.Font.AllCaps = True
Dim consultantTitle As Range
Set consultantTitle = ActiveDocument.Bookmarks("ConsultantTitle").Range
consultantTitle.Text = Me.TextTitle.Value
Dim consultantAddress1 As Range
Set consultantAddress1 =
ActiveDocument.Bookmarks("ConsultantAddress1").Range
consultantAddress1.Text = Me.TextAddress1.Value
Dim consultantAddress2 As Range
Set consultantAddress2 =
ActiveDocument.Bookmarks("ConsultantAddress2").Range
consultantAddress2.Text = Me.TextAddress2.Value
Me.Repaint
ConsultantInfo.Hide
End Sub
Private Sub FillSaveButton_Click()
Dim consultantName1 As Range
Set consultantName1 = ActiveDocument.Bookmarks("ConsultantName1").Range
Dim consultantName2 As Range
Set consultantName2 = ActiveDocument.Bookmarks("ConsultantName2").Range
Dim consultantName3 As Range
Set consultantName3 = ActiveDocument.Bookmarks("ConsultantName3").Range
Dim consultantNameCaps As Range
Set consultantNameCaps =
ActiveDocument.Bookmarks("ConsultantNameCaps").Range
consultantName1.Text = Me.TextName.Value
consultantName2.Text = Me.TextName.Value
consultantName3.Text = Me.TextName.Value
consultantNameCaps.Text = Me.TextName.Value
consultantNameCaps.Font.AllCaps = True
Dim consultantTitle As Range
Set consultantTitle = ActiveDocument.Bookmarks("ConsultantTitle").Range
consultantTitle.Text = Me.TextTitle.Value
Dim consultantAddress1 As Range
Set consultantAddress1 =
ActiveDocument.Bookmarks("ConsultantAddress1").Range
consultantAddress1.Text = Me.TextAddress1.Value
Dim consultantAddress2 As Range
Set consultantAddress2 =
ActiveDocument.Bookmarks("ConsultantAddress2").Range
consultantAddress2.Text = Me.TextAddress2.Value
ActiveDocument.SaveAs2 FileName:=ActiveDocument.Path & "\Consulting
Agreement Master " & consultantName1, FileFormat:=wdFormatDocument
ActiveDocument.SaveAs2 FileName:=ActiveDocument.Path & "\Consulting
Agreement Master " & consultantName1, FileFormat:=wdFormatPDF
Me.Repaint
ConsultantInfo.Hide
End Sub
答案 0 :(得分:0)
可以使用IncludeText
字段将Word文档链接到另一个文档的书签内的内容。但这意味着您需要在“源”文档中保留书签。您的代码可能正在删除书签。举例说明在将数据写入书签后,如何更改代码以确保数据位于书签内:
Dim doc as Word.Document
Set doc = ActiveDocument
Dim rngConsultantNameCaps As Range
Dim sConsultantNameCaps as String
sConsultantNameCaps = "ConsultantNameCaps"
Set rngConsultantNameCaps = doc.Bookmarks(sConsultantNameCaps).Range
rngConsultantNameCaps.Text = Me.TextName.Value
doc.Bookmarks.Add(sConsultantNameCaps, rngConsultantNameCaps)
打开每个文档(“数据模板”除外)并用字段替换每个书签:
{ IncludeText "Path and filename" "bookmarkName" }
(请记住,如果您手动执行此操作,则需要使用 Ctrl + F9 插入{}括号。)
如果要将所有文档保存在同一文件夹中,则不需要整个路径 - 源文件名就足够了。 Word会自动查找源文件的同一文件夹。
该链接还会带来字符格式。如果您不想这样做,请将\* CharFormat
开关添加到域代码中,以强制它将格式应用于该字段的第一个字符:
{ IncludeText "Path and filename" "bookmarkName" \* CharFormat }
如果有许多文档,写一个宏来执行此操作甚至是有意义的:只需循环书签,保存书签名称,在该范围内插入带有书签名称的字段。
文档需要稍后单独打开并更新字段(也可以在打印时完成)。
请注意,也可以使用Link
字段,但该字段类型使用OLE进行通信,这对于镜像“简单”文本来说有点“过度”。