Consulting

Results 1 to 4 of 4

Thread: Macro to Edit Contents of in Table in Word Header

  1. #1
    VBAX Newbie
    Joined
    Mar 2022
    Posts
    2
    Location

    Macro to Edit Contents of in Table in Word Header

    Hi all,

    I'm trying to develop a macro that will loop through a folder of word docs, open each one, update the cells of a table in the header according to values I have stored in excel, save and close. The hope is that this will speed up the time consuming process of manually updating 100+ docs one by one when the headers need to be changed to reflect the next project phase.

    I'm getting a couple different errors on the "Update Header" line, stating the remote server machine does not exist and that the requested member of the collection does not exist (seems to be alternating between those two ). Another thing I'm unsure of is how to have the macro work on both .doc and .docx files - perhaps an asterisk on that line?

    I have very little VBA experience and any help would be greatly appreciated on how to get this up and running! Happy to provide any additional info it it helps. Here is what I've cobbled together from other forum posts so far:

    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
    section(1).headers(wdHeaderFooterFirstPage).Range.tables (1)
    .Cells(6).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
    Last edited by Aussiebear; 03-16-2022 at 09:07 AM. Reason: Close up the whitespace

  2. #2
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    Cross-posted (with an answer and good comments) at Stack Overflow: https://stackoverflow.com/questions/...ms-word-header.

    For cross-posting etiquette, please read: A Message to Forum Cross-Posters https://www.excelguru.ca/content.php?184

    I recommend that responses be posted there if possible.

  3. #3
    VBAX Newbie
    Joined
    Mar 2022
    Posts
    2
    Location
    Quote Originally Posted by Chas Kenyon View Post
    Cross-posted (with an answer and good comments) at Stack Overflow: https://stackoverflow.com/questions/...ms-word-header.

    For cross-posting etiquette, please read: A Message to Forum Cross-Posters https://www.excelguru.ca/content.php?184

    I recommend that responses be posted there if possible.
    Thank you for the pointers! Still learning so I appreciate the resource.

    I've updated the post on Stack Overflow with my progress/code edits and errors I'm getting.

  4. #4
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    I suspect that everyone who has posted much in forums has cross-posted. It is just important to do it with understanding for your audience if you do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •