Background:
When I execute the code on a test document (testfile1.doc), I notice that the proportions/dimensions of both logos appear stretched, not aligning with the proportions specified in the code. This overstretching problem is exclusive to .doc files and .docx files running in compatibility mode. Notably, the issue is absent when working with native .docx files that aren't in compatibility mode.
It's also worth pointing out that the logo on the first page differs from the one on the second and subsequent pages. Furthermore, for clarity, the logo on the each page should be positioned 1.5cm from the top of the page and centered within the header. This means the middle of the logo should align with the center of the header.
Given the extensive number of .doc documents I'm working with, finding a solution that guarantees an accurate conversion without any distortion is paramount.
What I've tried:
I have explored various solutions and tried different solutions/approaches, but nothing seems to solve this inexplicable issue.
Attachments:
To provide a clearer picture, I have attached the following files:
- testfile1 - doc (where the issue is observed)
- testfile2 - docx (works correctly)
- logo frontpage (jpg)
- logo subsequent pages (png)
The logos should be saved in the C:\Test folder.
Question:
I would be grateful for any insights or solutions to this problem. I will be happy to give you more background.
Code:
Option Explicit
Sub ReplaceHeaderandInsertLogo()
' Turn off screen updating
Application.ScreenUpdating = False
Dim oDoc As Document
Dim oSec As Section
Dim oHead As HeaderFooter
Dim oRng As Range
Dim oShape As InlineShape
' Define the locations of the logos
Dim strLogoFrontpage As String
Dim strLogoSubsequentPages As String
strLogoFrontpage = "C:\Test\logo_frontpage.jpg"
strLogoSubsequentPages = "C:\Test\logo_subsequentpages.png"
' Set the active document
Set oDoc = ActiveDocument
' Loop through each section in the document
For Each oSec In oDoc.Sections
' Set the first page to have a different header/footer (applies to the entire document)
oSec.PageSetup.DifferentFirstPageHeaderFooter = True
' Loop through each header in the section
For Each oHead In oSec.Headers
If oHead.Exists Then
Set oRng = oHead.Range
With oRng
.Text = Chr(13) ' Reset the header text
.Collapse 0
.ParagraphFormat.alignment = wdAlignParagraphCenter ' Center align the content
' Check if the current header is for the first page or subsequent pages
If oHead.index = wdHeaderFooterFirstPage Then
Set oShape = .InlineShapes.AddPicture(FileName:=strLogoFrontpage)
oShape.LockAspectRatio = msoTrue
oShape.Width = CentimetersToPoints(3.94)
oShape.AlternativeText = "frontpage"
ElseIf oHead.index = wdHeaderFooterPrimary Then
Set oShape = .InlineShapes.AddPicture(FileName:=strLogoSubsequentPages)
oShape.LockAspectRatio = msoTrue
oShape.Width = CentimetersToPoints(0.73)
oShape.AlternativeText = "subsequent page"
End If
End With
End If
Next oHead
Next oSec
' Turn on screen updating
Application.ScreenUpdating = True
lbl_Exit:
Set oSec = Nothing
Set oHead = Nothing
Set oShape = Nothing
Set oRng = Nothing
Set oDoc = Nothing
End Sub