Excel VBA - 文字没有被替换,但 PDF 正在生成

Excel VBA - 文字没有被替换,但 PDF 正在生成

在此代码中,我尝试替换模板 ms word 文件中“#”所针对的特定单词,然后生成 PDF 并保存文件,但问题是......PDF 正在生成,但模板文件中的单词没有被替换。请告诉我我做错了什么!!

Private Sub genPDF_Click()
submit_Click
FilePath = "C:\Users\Lucifer\Downloads\" & Me.TextBox1 & "_" & Me.TextBox3 & ".pdf"
On Error Resume Next
Kill FilePath

DocFile = "C:\Users\Lucifer\Downloads\test1.docx" 'Template FIle
Dim WordApp As Word.Application
Dim WordDoc As Word.Document

Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open(DocFile, False)
Word.Application.Visible = True
For DataCol = 4 To 9
With WordDoc.Content.Find
 .Text = "#" & Me("lbl" & DataCol).Caption & "#"
 .Replacement.Text = Me("Txt" & DataCol).Value
 .Execute Replace:=wdReplaceAll
 
End With
Next DataCol

WordDoc.ExportAsFixedFormat OutputFileName:=FilePath, ExportFormat:=wdExportFormatPDF
WordDoc.Close SaveChanges:=False
WordApp.Quit

Set WordApp = Nothing
Set WordDoc = Nothing

Me.WebBrowser1.Navigate (FilePath)
End Sub

相关内容