Document Header and Footer images placed in exact locations in bulk

OpPot

New Member
Joined
Nov 12, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi guys,

I'm currently changing the logos in bulk for 1000s of documents and know there to be an easier way to do this.

I have some template documents which contain the right header and footer placements for each of the images, but I, so far, have not been able to translate this into bulk header and footer image changes for multiple documents through VBA code (I lack suitable knowledge in this department)

What I currently have is the following:
Logo #1 and Logo #2 - One for a portrait layout, the other for a landscape layout respectively.
Footer #1 and Footer #2 - One for a portrait layout, the other for a landscape layout respectively.
Both should have Text Wrapping: Behind Text.

What I'd like:
I'd like to apply logo #1 and footer #1 for portrait with a specific position on the page (in headers and footers with the positions given below)
If the document is not portrait, I'd like for logo #2 and footer #2 for landscape to be used on a specific position on the page (in headers and footers with positions given below)
I would also like to be able to select the locations of these logos and footers or alternatively, just write their path so they can be imported from there and pasted in bulk

The exact location and s regarding where the logos should be in the header (depending on page layout portrait or landscape):

Landscape Logo:
1606131608805.png


Portrait Logo:

1606131802737.png



The exact locations regarding where the footer image should be in the footer (depending on page layout portrait or landscape)

Footer:
(The same position whether or not its landscape or portrait)

1606131491315.png


Any help would be appreciated. If there is a smarter way to do this without VBA code, please let me know!
 

Attachments

  • 1606131643390.png
    1606131643390.png
    24.5 KB · Views: 12

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Create a single document with two Sections.
Unlink the headers & footers for the last Section from the first section.
Give the first Section a portrait layout and the header footer content for that layout.
Give the last Section a landscape layout and the header footer content for that layout.
Add the following macro to the document.
Run the macro and select the folder to process.
VBA Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
Dim DocSrc As Document, DocTgt As Document
Dim StrSrcNm As String, StrTgtNm As String
strFolder = GetFolder: If strFolder = "" Then Exit Sub
Set DocSrc = ActiveDocument: StrSrcNm = DocSrc.FullName
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  DocTgt = strFolder & "\" & strFile
  If StrSrcNm < DocTgt Then
    Set DocTgt = Documents.Open(FileName:=DocTgt, AddToRecentFiles:=False, Visible:=False)
    With DocTgt
      With .Sections.First
        Select Case .PageSetup.Orientation
          Case wdOrientLandscape
            Select Case .PageSetup.DifferentFirstPageHeaderFooter
              Case True
                With .Headers(wdHeaderFooterFirstPage).Range
                  .FormattedText = DocSrc.Sections.First.Headers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
                With .Footers(wdHeaderFooterFirstPage).Range
                  .FormattedText = DocSrc.Sections.First.Footers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
              Case False
                With .Headers(wdHeaderFooterPrimary).Range
                  .FormattedText = DocSrc.Sections.First.Headers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
                With .Footers(wdHeaderFooterPrimary).Range
                  .FormattedText = DocSrc.Sections.First.Footers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
            End Select
          Case wdOrientPortrait
            Select Case .PageSetup.DifferentFirstPageHeaderFooter
              Case True
                With .Headers(wdHeaderFooterFirstPage).Range
                  .FormattedText = DocSrc.Sections.Last.Headers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
                With .Footers(wdHeaderFooterFirstPage).Range
                  .FormattedText = DocSrc.Sections.Last.Footers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
              Case False
                With .Headers(wdHeaderFooterPrimary).Range
                  .FormattedText = DocSrc.Sections.Last.Headers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
                With .Footers(wdHeaderFooterPrimary).Range
                  .FormattedText = DocSrc.Sections.Last.Footers(wdHeaderFooterPrimary).Range.FormattedText
                  .Characters.Last.Previous = vbNullString
                End With
            End Select
        End Select
      End With
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
For PC macro installation & usage instructions, see: Installing Macros

Note1: The macro replaces any existing header/footer content.

Note2: You should not update any documents that have already been sent to third parties. Doing so can have significant negative legal implications.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top