Excel VBA生成Word文档:页脚填充新文件名问题求助
解决Word页脚添加新文件名的问题
我仔细看了你的代码,核心问题是你在生成文件名后直接保存关闭了Word文档,完全没给修改页脚的机会。而且你需要把已经定义好的SDD文件名变量,插入到Word文档的页脚区域里。下面是具体的修改方案:
关键调整要点
- 调整操作顺序:必须在保存文档之前修改页脚(这样不用二次打开文档,效率更高)
- 定位Word页脚:通过文档的Section对象访问主页脚区域
- 插入文件名:清空原有页脚内容(或替换占位符),把生成的文件名填进去
修改后的完整代码
Public Function Txtmkr_SDD() Dim appWord As Object 'Word-Instance Dim wdDoc As Object 'Word-Document Dim wdRngE As Object 'Word-Range 1 Dim wdRngR As Object 'Word-Range 2 Dim wdRngC As Object 'Word-Range 3 Dim wdRngCN As Object 'Word-Range 4 Dim wks As Worksheet 'Excel-Worksheet Dim AdresseCE As String Dim neueAdresseCE As Long Dim Processname1 As String Dim Processname2 As String Dim Version As String ' 新增页脚操作相关变量 Dim wdFooter As Object Dim wdFooterRange As Object '*** Word start *** Set appWord = CreateObject("Word.Application") '*** opens File *** Set wdDoc = appWord.Documents.Add(Template:=Worksheets("StartPage").Cells(48, 4) & "\Document_Templates\SDD_Template.dotx", NewTemplate:=False, DocumentType:=0) '*** Word visible *** appWord.Visible = True '*** just in case Document is protected *** 'doc.Unprotect '*** Jump to Textmarker in Word *** '*** Check of existence *** '*** Take Value from "CopyData" Cell "B1" and insert Textmarker *** If wdDoc.Bookmarks.Exists("Processname1") Then With wdDoc.Bookmarks("Processname1") Set wdRngE = .Range wdRngE.Text = Worksheets("CopyData").Cells(1, 2).Value wdDoc.Bookmarks.Add "Processname1", wdRngE End With Else MsgBox "Missing Link [Processname1]." End If '*** Take Value from "CopyData" Cell "B2" and insert Textmarker *** If wdDoc.Bookmarks.Exists("Processname2") Then With wdDoc.Bookmarks("Processname2") Set wdRngE = .Range wdRngE.Text = Worksheets("CopyData").Cells(2, 2).Value wdDoc.Bookmarks.Add "Processname2", wdRngE End With Else MsgBox "Missing Link [Processname2]." End If If wdDoc.Bookmarks.Exists("Version") Then With wdDoc.Bookmarks("Version") Set wdRngE = .Range wdRngE.Text = Worksheets("CopyData").Cells(3, 2).Value wdDoc.Bookmarks.Add "Version", wdRngE End With Else MsgBox "Missing Link [Version]." End If If wdDoc.Bookmarks.Exists("Create_Date") Then With wdDoc.Bookmarks("Create_Date") Set wdRngE = .Range wdRngE.Text = Worksheets("CopyData").Cells(4, 2).Value wdDoc.Bookmarks.Add "Create_Date", wdRngE End With Else MsgBox "Missing Link [Create_Date]." End If If wdDoc.Bookmarks.Exists("Author") Then With wdDoc.Bookmarks("Author") Set wdRngE = .Range wdRngE.Text = Worksheets("CopyData").Cells(6, 2).Value wdDoc.Bookmarks.Add "Author", wdRngE End With Else MsgBox "Missing Link [Author]." End If '*** Set Time_Date and SDD Path *** Dim time_date As String time_date = Format(Date, "yyyy_mm_dd") Dim SDD As String '*** Define SDD as RegularPath *** SDD = (time_date & "_" & Worksheets("CopyData").Cells(1, 2).Value & "_" & Worksheets("CopyData").Cells(21, 2).Value & "_" & Worksheets("Helper#3").Cells(3, 2).Value & "_" & "V" & Worksheets("CopyData").Cells(3, 2).Value & ".docx") ' --- 新增:修改页脚内容 --- ' 获取文档第一部分的主页脚(1对应wdHeaderFooterPrimary常量,后期绑定需用数值) Set wdFooter = wdDoc.Sections(1).Footers(1) Set wdFooterRange = wdFooter.Range ' 清空原有页脚内容(如果模板页脚有占位符,可改用查找替换) wdFooterRange.Text = "" ' 插入生成的文件名 wdFooterRange.Text = SDD ' 可选:设置页脚文本居中对齐(1对应wdAlignParagraphCenter常量) wdFooterRange.ParagraphFormat.Alignment = 1 '*** Set up SavePath & Filename *** appWord.ActiveDocument.SaveAs Worksheets("Variables").Cells(3, 8).Value & "\" & (Worksheets("Setup#2_DirectoryList").Cells(1, 1)) & "\" & Worksheets("Setup#2_DirectoryList").Cells(3, 3).Value & "\" & Worksheets("Setup#2_DirectoryList").Cells(14, 21).Value & "\" & SDD '*** Word quit *** appWord.ActiveDocument.Close appWord.Quit '*** disable Variables *** Set wdRngE = Nothing Set wdRngR = Nothing Set wdRngC = Nothing Set wdRngCN = Nothing Set wdFooter = Nothing Set wdFooterRange = Nothing Set wdDoc = Nothing Set appWord = Nothing Set sFolder = Nothing End Function
额外提示
- 因为你用的是后期绑定(
CreateObject("Word.Application")),所以不能直接使用Word内置常量,只能用对应的数值:主页脚是1,首页页脚是2,偶数页页脚是3,根据你的模板情况调整。 - 如果你的模板页脚已经有固定内容,不想全部清空,可以在模板页脚里加个占位符(比如
{{FILENAME}}),然后用查找替换的方式替换成文件名:With wdFooterRange.Find .Text = "{{FILENAME}}" .Replacement.Text = SDD .Execute Replace:=2 ' 2对应wdReplaceAll常量 End With - 测试前确保Word模板的页脚没有被保护,否则修改操作会失败。
内容的提问来源于stack exchange,提问作者IonDOne
相关产品推荐
相关产品推荐

