VBA代码读取Word文档页脚 [英] VBA code to read word document footer
问题描述
我有一个完整的Word文档表单文件夹,并且我有一个excel文件,可将表单上的所有答案读入电子表格的不同工作表中.数据导出到的工作表取决于word文档的文件名.
I have a folder of completed word document forms and I have an excel file that reads all the answers on the forms into different worksheets in spreadsheet. The worksheet that the data is exported to depends on the filename of the word document.
这目前可以正常工作.
但是,我现在需要它能够考虑存储在word文档页脚中的表单的版本号,但是我不知道如何引用它.
However, I now need it to be able to take into account the version number of the form which is stored in the footer of the word document but I don't know how to reference it.
在VBA方面,我是个菜鸟,所以还没有尝试很多.
I'm quite a noob when it comes to VBA so havent tried much.
我尝试过的VBA可以在下面找到,但是不足为奇.
The VBA that I have tried can be found below but unsurprisingly doesnt work.
Sub ReadWordDoc(filenme As String)
Dim Val As String
Dim WrdDoc As Document
Dim FormFieldCounter As Integer
Dim version As String
Set wordapp = CreateObject("word.Application")
wordapp.Documents.Open filenme
wordapp.ScreenUpdating = False
Set WrdDoc = wordapp.Documents(filenme)
wordapp.Visible = True
version = WrdDoc.Sections(1).Footers(wdHeaderFooterFirstPage).Range.Text
FormFieldCounter = 1
If InStr(version, "5.00") Then
RowCounter = RowCounter + 1
Sheets("Version 5").Cells(RowCounter, FormFieldCounter) = filenme
Do While FormFieldCounter <= 125
WrdDoc.FormFields(FormFieldCounter).Select
Val = WrdDoc.FormFields(FormFieldCounter).result
Sheets("Version 5").Cells(RowCounter, FormFieldCounter + 1) = Val
FormFieldCounter = FormFieldCounter + 1
Loop
wordapp.Documents(filenme).Close SaveChanges:=wdDoNotSaveChanges
wordapp.Quit
Else
'Do something else
End If
End Sub
推荐答案
玩了一段时间,有了Google之后,我发现了此页面,该页面可以帮助我解决问题 https://msdn.microsoft.com/zh-cn/library/office/aa221970(v = office.11).aspx
After having a bit of a play and a Google, I found this page which helped me fix my issue https://msdn.microsoft.com/en-us/library/office/aa221970(v=office.11).aspx
我更改了我的代码,如下所示:
I changed my code as below:
version = WrdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text
尽管我不确定为什么以前的版本不起作用,因为首页上有页脚.
Although I'm not sure why the previous version didnt work as there is a footer on the first page.
这篇关于VBA代码读取Word文档页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!