Consulting

Results 1 to 17 of 17

Thread: Replace Image in Document Header (Multiple Documents)

  1. #1
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location

    Replace Image in Document Header (Multiple Documents)

    Hi all....

    Our company name has changed, this means that some 3000-odd documents (maintenance routines) with the old company image in the header require replacing!

    Each header is a table with the image in the top left cell, and text (which isn't changing) in the remaining cells and rows of the header - is there any way to select a folder containing all the documents and run code to replace the (jpg) image in them all?

    TIA

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,351
    Location
    In basic form:

    Sub ChangePic()
    Dim oFile
    oFile = Dir("C:\Users\Maxey\Desktop\BatchFolder\*.doc*") 'Change this path to reflect your batch folder path. 
    Do While oFile <> ""
      Application.Documents.Open oFile
        With ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
          .GoTo what:=wdGoToGraphic, Count:=1
          .Delete
          .InlineShapes.AddPicture FileName:="D:\Replacement Pic.jpg" 'Change this file path and name to reflect your new picture path and name.
        End With
      If ActiveDocument.Saved = False Then ActiveDocument.Save
      ActiveDocument.Close
      oFile = Dir
    Loop
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location
    Thanks so much, I'll try this tomorrow.....

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,351
    Location
    Be sure to try it on a small sample batch of files first.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location
    OK, decided to try a small batch with an easy folder path..... it pretty much bombs on the line Application.Documents.Open oFile - quick Google search and the MS article on the error didn't really help?

    Capture1.JPG

    It knows the file is there as it states the name in the error......

    Capture2.JPG


    I wish I understood this stuff better, but I'm now STUCK!

  6. #6
    It doesn't find it because you need to supply the path as well as the name to Application.documents.open. The following will work subject to provisos
    1. that the image is in the primary header of the first section.
    2. That the image to be replaced is the first image in that header
    3. That the image to be replaced is inserted in line.

    If any of these are not true for your documents, then provide the missing details:
    Option Explicit
    
    Sub ChangePic()
    Dim strFile As String
    Dim oRng As Range
    Dim oShape As InlineShape
    Const strPath As String = "C:\PMRHeader\PMR\"
    Const strNewImage As String = "C:\PMRHeader\Repso1.jpg"
    Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FolderExists(strPath) Then
            MsgBox "The document folder '" & strPath & "' is not available."
            GoTo lbl_Exit
        End If
        If Not fso.FileExists(strNewImage) Then
            MsgBox "Unable to locate the image file '" & strNewImage & "'"
            GoTo lbl_Exit
        End If
        strFile = Dir$(strPath & "*.doc*")
        Do While strFile <> ""
            Application.Documents.Open strPath & strFile
            If ActiveDocument.StoryRanges(wdPrimaryHeaderStory).InlineShapes.Count > 0 Then
                Set oRng = ActiveDocument.StoryRanges(wdPrimaryHeaderStory).InlineShapes(1).Range
                oRng.Text = ""
                oRng.InlineShapes.AddPicture FileName:=strNewImage
                If ActiveDocument.Saved = False Then ActiveDocument.Save
            End If
            ActiveDocument.Close
            strFile = Dir$
        Loop
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location
    Probably closer..... it goes through the motions, flickers as it appears to be doing each document, but nothing in the header actually changes when I look at them..... your three points above. 1 & 2 are definitely a 'yes', but what exactly does 'in line' mean in 3? Thanks again.....

    The header looks like this....

    Capture.JPG

  8. #8
    By default Word inserts images 'in line' with the text, but they can be inserted with various wrap options, which require different processing. However as you have a table in the header that makes things easier

    Option Explicit
    
    Sub ChangePic()
        Dim strFile As String
        Dim oRng As Range
        Dim oShape As InlineShape
        Const strPath As String = "C:\PMRHeader\PMR\"
        Const strNewImage As String = "C:\PMRHeader\Repso1.jpg"
        Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FolderExists(strPath) Then
            MsgBox "The document folder '" & strPath & "' is not available."
            GoTo lbl_Exit
        End If
        If Not fso.FileExists(strNewImage) Then
            MsgBox "Unable to locate the image file '" & strNewImage & "'"
            GoTo lbl_Exit
        End If
        strFile = Dir$(strPath & "*.doc*")
        Do While strFile <> ""
            Application.Documents.Open strPath & strFile
            If ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Tables.Count > 0 Then
                Set oRng = ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Tables(1).Range.Cells(1).Range
                oRng.End = oRng.End - 1
                oRng.Text = ""
                oRng.InlineShapes.AddPicture FileName:=strNewImage
                If ActiveDocument.Saved = False Then ActiveDocument.Save
            End If
            ActiveDocument.Close
            strFile = Dir$
        Loop
    lbl_Exit:
        Exit Sub
    End Sub
    If that doesn't work for you, post a document sample.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,351
    Location
    Graham is correct of course concerning path and name. However something weird happened yesterday because the code as posted ran twice without error.
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location
    It works!! ....... but removes the page border?

    Capture.JPG

    Capture1.JPG

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,351
    Location
    Can you attached one of these documents and your new graphic file?
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location
    Yup, here's one of the original files plus the graphic..... thanks.

    Files.zip

  13. #13
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,351
    Location
    Ok, the following processes the file you attached:

    Sub ChangePic()
    Dim strFile As String
    Dim oRng As Range
    Const strPath As String = "C:\Users\Maxey\Desktop\AAA\" '"C:\PMRHeader\PMR\"
    Const strNewImage As String = "D:\Replacement Pic.jpg" '"C:\PMRHeader\Repso1.jpg"
      
      strFile = Dir$(strPath & "*.doc")
      Do While strFile <> ""
        Application.Documents.Open strPath & strFile
        If ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Count = 2 Then
          Set oRng = ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(2).Anchor
          ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(2).Delete
          oRng.InlineShapes.AddPicture  strNewImage 
          If ActiveDocument.Saved = False Then ActiveDocument.Save
         End If
         ActiveDocument.Close
        strFile = Dir$
      Loop
    lbl_Exit:
      Exit Sub
    End Sub
    Last edited by gmaxey; 08-03-2016 at 12:42 PM.
    Greg

    Visit my website: http://gregmaxey.com

  14. #14
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location
    Greg, thanks..... I'm just about to hit the road home but will try this first thing in the morning when back in the office and update the thread....

  15. #15
    VBAX Regular
    Joined
    Jul 2007
    Posts
    34
    Location
    Works perfect, thanks. A huge thanks to Greg and Graham for their assistance...... I'm in awe of people who can do this stuff; I tried for quite a long time to get my head around it, but I had to admit defeat and came to the conclusion my brain just isn't 'wired' for coding of any kind..... probably too old (or not enough brain cells) to start it.....

  16. #16
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,351
    Location
    For the set of documents where the image to be replaced is inline, use:

    strFile = Dir$(strPath & "*.doc")
        Do While strFile <> ""
            Application.Documents.Open strPath & strFile
            If ActiveDocument.StoryRanges(wdPrimaryHeaderStory).InlineShapes.Count = 1 Then
                Set oRng = ActiveDocument.StoryRanges(wdPrimaryHeaderStory).InlineShapes(1).Range
                ActiveDocument.StoryRanges(wdPrimaryHeaderStory).InlineShapes(1).Delete
                oRng.InlineShapes.AddPicture strNewImage
                If ActiveDocument.Saved = False Then ActiveDocument.Save
            End If
            ActiveDocument.Close
            strFile = Dir$
        Loop
    Greg

    Visit my website: http://gregmaxey.com

  17. #17
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    @ theHydra

    Your question has been moved to http://www.vbaexpress.com/forum/show...ocument-Header

    And this thread has been closed
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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