简体   繁体   中英

Trouble with Macro Updating Table Cells in MS Word Header

I'm working on a macro that will update the cells of a table in an MS Word header according to values I'm storing in excel. Hoping this will speed up the process of manually updating the headers containing the project phase and due date in the 100+ word docs I'm working with. I know very little about VBA but cobbled this code together and hoping someone who knows what they're doing could point me in the right direction to get this code working. Happy to provide more information if it helps. Thanks!

Update: Thanks to all who have provided suggestions for getting this to work - still getting an error for some reason. Having some trouble recognizing and editing the table in the header.

I'm getting Error 5941 on this line - Requested member of the collection does not exist

With oWordDoc.Sections(1)...    

Here's what I've got:

Sub UpdateSpecHeaders()
Dim oWordApp As Object
Dim oWordDoc As Object
Dim sFolder As String, strFilePattern As String
Dim strFileName As String, sFileName As String

    '> Folder containing files to update
sFolder = Range("A20").Value

    '> Identify file extension to search for
strFilePattern = "*.doc"

'> Establish a Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
    Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True

Application.ScreenUpdating = False

'> Loop through the folder to get the word files
strFileName = Dir$(sFolder & strFilePattern)
Do Until strFileName = ""
    sFileName = sFolder & strFileName
    

    '> Open the word doc
    Set oWordDoc = oWordApp.Documents.Open(sFileName)
           
    '> Update Header
              
    With oWordDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Range
    
            .Cells(Row:=3, Column:=1).Text = Range("A3").Value
            .Cells(Row:=3, Column:=2).Text = Range("B3").Value
    End With
                
    '> Save and close the file
           
    oWordDoc.SaveAs Filename:=oWordDoc.Name
    oWordDoc.Close SaveChanges:=False
        
    '> Find next file
    strFileName = Dir$()
Loop

'> Quit and clean up
Application.ScreenUpdating = True
oWordApp.Quit

Set oWordDoc = Nothing
Set oWordApp = Nothing

End Sub

I'm getting Error 424 - Object Required on this line

With ActiveDocument.Sections(1)...    

That is because you run the VBA macro from Excel where shorthand properties are ActiveWorksheet , ActiveWorkbook and etc. But the ActiveDocument property makes sense only when you run the code in Word. So, the following code should be changed:

  '> Update Fields
    oWordApp.ActiveDocument.Fields.Update
    
    
    '> Update Header
    
        With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(2)
        .Cell(Row:=3, Column:=1).Text = Range("A3").Value
        .Cell(Row:=3, Column:=2).Text = Range("B3").Value
            
        End With

It should look like that:

  '> Update Fields
    oWordApp.ActiveDocument.Fields.Update
    
    
    '> Update Header
    
        With oWordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(2)
        .Cell(Row:=3, Column:=1).Text = Range("A3").Value
        .Cell(Row:=3, Column:=2).Text = Range("B3").Value
            
        End With

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