Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-21-2022, 07:20 AM
Italophile Italophile is offline Help with VBA to set header on documents with multiple sections Windows 11 Help with VBA to set header on documents with multiple sections Office 2021
Expert
 
Join Date: Mar 2022
Posts: 538
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default


You don't need to select the headers, you can work with them directly as you do in the loop where you delete the existing headers.

Just as you looped through the sections to delete the existing headers you need to do the same thing to add the replacements.

Code:
Sub StdLetterhead()

    Dim oSec As Section
    Dim oHead As HeaderFooter

    For Each oSec In ActiveDocument.Sections
        For Each oHead In oSec.Headers
            If oHead.Exists Then oHead.Range.Delete
        Next oHead

    Next oSec

    ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True

    Dim ohShape As Shape, ohRange As Range
    Dim hPfad As String
    hPfad = "\\Server\StdLetterhead.jpg"

    Set ohRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
    Set ohShape = ActiveDocument.Shapes.AddPicture(FileName:=hPfad, LinkToFile:=False, SaveWithDocument:=True, Anchor:=ohRange)
    With ohShape
        .Height = CentimetersToPoints(29.79)
        .Width = CentimetersToPoints(21.08)
        .Left = CentimetersToPoints(-1.59)
        .Top = CentimetersToPoints(-1.35)
        .ZOrder msoSendBehindText
    End With

    On Error GoTo ErrorHandler

    Dim ohcShape As Shape, ohcRange As Range
    Dim hcPfad As String
    hcPfad = "\\Server\Continuation.jpg"
    For Each oSec In ActiveDocument.Sections
    
        Set ohcRange = oSec.Headers(wdHeaderFooterPrimary).Range
        Set ohcShape = ActiveDocument.Shapes.AddPicture(FileName:=hcPfad, LinkToFile:=False, SaveWithDocument:=True, Anchor:=ohcRange)
        With ohcShape
            .Height = CentimetersToPoints(29.79)
            .Width = CentimetersToPoints(21.08)
            .Left = CentimetersToPoints(-1.59)
            .Top = CentimetersToPoints(-1.35)
            .ZOrder msoSendBehindText
        End With
    Next oSec

ErrorHandler:


End Sub
Reply With Quote
  #2  
Old 03-28-2022, 11:56 PM
xdavemarshallx xdavemarshallx is offline Help with VBA to set header on documents with multiple sections Windows 10 Help with VBA to set header on documents with multiple sections Office 2021
Novice
Help with VBA to set header on documents with multiple sections
 
Join Date: Mar 2022
Posts: 2
xdavemarshallx is on a distinguished road
Default

Quote:
Originally Posted by Italophile View Post
You don't need to select the headers, you can work with them directly as you do in the loop where you delete the existing headers.

Just as you looped through the sections to delete the existing headers you need to do the same thing to add the replacements.
Italophile, thank you and sorry for the delay in responding. After a couple of amendments to add in a routine to set only the first section as having a different first page, I now appear to have this working as intended. I am very grateful.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract Document ID and description from header in multiple Word documents and paste in new word doc venkat_m Word VBA 2 05-23-2020 03:57 AM
VBA to add Custom header to multiple documents in a folder Tenacious1 Word VBA 1 08-14-2018 03:46 PM
Help with VBA to set header on documents with multiple sections How to extract sections with the same heading from multiple documents and merge into new single doc edumac Word 2 04-10-2017 04:11 PM
Separate header for different sections shawpnik Word 1 09-30-2014 07:04 PM
Different Header Same Footer across two sections - Letterhead bostockm Word 1 07-21-2014 05:36 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:53 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft