【问题标题】:Why does the values in cells (Excel) aren't replacing the variables in the Words files?为什么单元格 (Excel) 中的值没有替换 Words 文件中的变量?
【发布时间】: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


【解决方案1】:

你有很多重复的代码,应该在一个单独的子目录中。

例如:

Sub createPDF()

    Dim objWord As Object, doc As Object
    Dim ws As Worksheet
    Dim theString As String
    Dim TheFileName As String, nm, i As Long
    Dim TemplatePath As String, myExtension, myfile
    Dim Pscope As String

    Set ws = ThisWorkbook.ActiveSheet
    TemplatePath = ThisWorkbook.Path

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    myExtension = "*.doc*"
    myfile = Dir(TemplatePath + "\Template" & "\" & myExtension)

    Do While myfile <> ""
        Set doc = objWord.Documents.Open(TemplatePath + "\Template" & "\" & myfile)
        For Each nm In Array("company_ename", "owner_fname1", "owner_pname1", _
                             "owner_fullname1", "owner_id1", "owner_allotted1", _
                             "house", "director_pname1", "director_fname1")
            DoReplace doc, ws, nm
        Next nm

        For i = 2 To 4
            For Each nm In Array("owner_fname", "owner_pname", "owner_fullname", _
                                    "owner_id", "owner_allotted")
                DoReplace doc, ws, nm & CStr(i)
            Next nm
        Next i

        TheFileName = TemplatePath & "\Output\" & ws.Range("company_ename").Value & _
                      "_" & Replace(myfile, "docx", "") & ".docx"

        doc.SaveAs TheFileName
        doc.Close savechanges:=False

        myfile = Dir
    Loop
    Set objWord = Nothing

    MsgBox "Generation Complete!"

End Sub

Sub DoReplace(doc As Object, ws As Worksheet, txt)
    With doc.Content.Find
        .Text = "{" & txt & "}" 'in the Word doc the tag is enclosed in{}
        .MatchCase = False
        .MatchWholeWord = True
        .replacement.Text = ws.Range(txt).Value
        .wrap = 1 'wdfindcontinue
        .Execute Replace:=2 'wdReplaceAll
    End With
End Sub

【讨论】:

    最近更新 更多