使用vba复制word doc footer excel

时间:2015-01-09 17:05:38

标签: excel-vba vba excel

尝试使用excel宏在MS_Word文档中获取页脚文本并将其粘贴到Excel工作表中。此VBA代码在MS_WORD中工作,但不在MS_EXCEL宏中。我检查了文档中的section count = 1。

Dim wdDoc As Object
Dim h As Object 'Word.HeaderFooter
Dim docver As String 'Paste this to excel

Set wdDoc = GetObject("D:docname.docx")

With wdDoc

    For Each h In .Sections

        docver = h.Footers(wdHeaderFooterPrimary).Range.Text

    Next

End With

2 个答案:

答案 0 :(得分:2)

我能够使用VBA项目视图下的“监视窗口”来确定语法结构。正确的语法如下:

对于每个h In .Sections.Item.Footers

docver = h.Footers.Item(1).Range.Text

下一步

答案 1 :(得分:-1)

how to I extract a footer for each document. It is only finding the footer for the first document. 

Currently my code is 

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim docvers As String

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("U:\word" & "\")
i = 1
strDirectory = "U:\word" & "\"

varDirectory = Dir(strDirectory, vbNormal)
docvers = strDirectory & varDirectory

Dim wdDoc As Object
Dim h As Object 'Word.HeaderFooter
Dim docver As String 'Paste this to excel

Set wdDoc = GetObject(docvers)

With wdDoc

For Each h In .Sections

        docver = h.footers.Item(2).Range.Text

    Next

End With
For Each objFile In objFolder.Files
 'print file name
 Cells(i + 1, 1) = objFile.Name
'print file path
 Cells(i + 1, 2) = objFile.Path
 Cells(i + 1, 3) = docver

 i = i + 1
Next objFile