简体   繁体   中英

copy word doc footer to excel using vba

Trying to get the footer text in a MS_Word Doc with an excel macro and paste it into an excel sheet. this VBA code works in MS_WORD but NOT in MS_EXCEL macro. I checked and the section count = 1 in the document.

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

I was able to use the "watch window" under the VBA project view to determine the Syntax structure. the correct syntax is as follows:

For Each h In .Sections.Item.Footers

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

Next

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM