【发布时间】:2025-12-06 18:30:02
【问题描述】:
我制作了一个 Excel 文件,用户应该填写一列详细信息,完成后他单击一个生成三个 Word 文件的按钮。 在 Excel 中,我命名了写入用户详细信息的单元格。在 word 文件中,我将变量(单元格的名称)放在我想要的位置。 除了将用户输入的详细信息替换为单词文件中的变量之外,一切都运行良好。
Sub createPDF()
Application.ScreenUpdating = False
Dim objWord As Object
Dim ws As Worksheet
Dim theString As String
Dim TemplatePath As String
Dim xWb As Workbook
Dim Pscope As String
'ws.Activate
Set ws = ThisWorkbook.ActiveSheet
Set objWord = CreateObject("Word.Application")
Set xWb = Application.ThisWorkbook
TemplatePath = xWb.Path
objWord.Visible = True
'Target File Extension (must include wildcard "*")
myExtension = "*.doc*"
'Target Path with Ending Extention
myfile = Dir(TemplatePath + "\Template" & "\" & myExtension)
'Loop through each word file in folder
Do While myfile <> ""
objWord.Documents.Open TemplatePath + "\Template" & "\" & myfile 'TemplatePath + "\ProposalTemplate.dotm" ' change as required
With objWord.ActiveDocument.Content.Find
.Text = "company_ename"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("company_ename").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_fname1"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_fname1").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_pname1"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_pname1").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_fullname1"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_fullname1").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_id1"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_id1").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_allotted1"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_allotted1").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
For i = 2 To 4
.Text = "owner_fname" & CStr(i)
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_fname" & CStr(i)).Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_pname" & CStr(i)
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_pname" & CStr(i)).Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_fullname" & CStr(i)
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_fullname" & CStr(i)).Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_id" & CStr(i)
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_id" & CStr(i)).Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "owner_allotted" & CStr(i)
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("owner_allotted" & CStr(i)).Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
Next i
.Text = "house"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("house").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "director_pname1"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("director_pname1").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
.Text = "director_fname1"
.MatchCase = False
.MatchWholeWord = True
.replacement.Text = ws.Range("director_fname1").Value
.wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll
End With
Dim TheFileName As String
TheFileName = TemplatePath + "\Output\" + ws.Range("company_ename").Value + "_" + Replace(myfile, "docx", "") + ".docx"
'(SaveAs is for Office 2003 and earlier - deprecated)
objWord.ActiveDocument.SaveAs TheFileName
'replaces existing .doc iff exists
' Close Documents and Quit Word
objWord.ActiveDocument.Close savechanges:=False
' objWord.ActiveDocument.Close 'close .DOCx
myfile = Dir
Loop
Set objWord = Nothing
MsgBox "Generation Complete!"
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
我们需要更多信息。请提供 Excel 数据和 Word 文档的小样本。此外,鉴于您想要做什么的描述,我强烈建议您阅读 BOOKMARKS 和 CONTENTCONTROLS 作为“数据目标”,而不是依赖查找/替换。例如,请参阅*.com/questions/49903311/…
-
尝试使用一些
Debug.Print语句来确定哪里出了问题。 -
如果您要替换的某些项目是其他项目的子字符串,您可能会遇到问题:我会放置更像“{owner_fname}”的标签
-
考虑改写问题的标题,标题对我来说很不清楚。此外,您提供的示例越准确、越切题,您就越有可能得到建设性的答案。
标签: excel vba ms-word generator