Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-21-2023, 03:47 AM
hank1234 hank1234 is offline Issue with logo proportions after insertion in header Windows 11 Issue with logo proportions after insertion in header Office 2016
Novice
Issue with logo proportions after insertion in header
 
Join Date: Sep 2020
Posts: 9
hank1234 is on a distinguished road
Default Issue with logo proportions after insertion in header

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
Attached Images
File Type: jpg logo_frontpage.jpg (9.2 KB, 17 views)
File Type: png logo_subsequentpages.png (6.5 KB, 17 views)
Attached Files
File Type: doc testfile1.doc (32.0 KB, 1 views)
File Type: docx testfile2.docx (14.9 KB, 1 views)

Last edited by hank1234; 10-21-2023 at 11:44 AM.
Reply With Quote
  #2  
Old 10-22-2023, 07:09 AM
Italophile Italophile is offline Issue with logo proportions after insertion in header Windows 11 Issue with logo proportions after insertion in header Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

I was expecting the attached documents to have the images inserted so we could understand your issue.

As you are inserting a known image you already know what the height and width are supposed to be. So why are you not setting them explicitly? Always better to do that than relying on setting LockAspectRatio.

However, if the quality of the inserted image is important you should always ensure that the image is the correct size before you insert it, instead of relying on Word's scaling. JPEG is definitely not the format of choice for a logo as it will not scale well. PNG works better for older documents, SVG is perfect for current version of Word (Word automatically creates a PNG of the SVG as a fallback).
Reply With Quote
  #3  
Old 10-22-2023, 12:27 PM
hank1234 hank1234 is offline Issue with logo proportions after insertion in header Windows 11 Issue with logo proportions after insertion in header Office 2016
Novice
Issue with logo proportions after insertion in header
 
Join Date: Sep 2020
Posts: 9
hank1234 is on a distinguished road
Default

Thank you for your feedback on the issue.

Firstly, to clarify, the provided testfile1.doc and testfile2.docx do have empty headers where the logos should be inserted. If you save the logos in the C:\Test folder and execute the code on the test documents, you'll be able to replicate the issue I mentioned.

I appreciate your point on setting the height and width explicitly, and I've tried this approach. However, the issue persists specifically with .doc files and .docx files in compatibility mode. Locking the aspect ratio was one of the methods I attempted to maintain the proportions, but it didn't rectify the problem.

Regarding the image formats, I understand the merits of SVGs and PNGs over JPEGs for scaling. But the main concern here isn't about the quality of the scaled image; rather, it's about the proportions of the logos being distorted exclusively in the .doc files and .docx files running in compatibility mode.

If you could help address this specific problem or point me towards any workarounds, it would be greatly appreciated.
Reply With Quote
  #4  
Old 10-22-2023, 07:43 PM
Guessed's Avatar
Guessed Guessed is offline Issue with logo proportions after insertion in header Windows 10 Issue with logo proportions after insertion in header Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If you aren't getting consistent behaviour of the locked aspect ratio then ignore it and make use of the scaleheight and scalewidth eg
Code:
aShp.Width = CentimetersToPoints(3.94)
aShp.ScaleHeight = aShp.ScaleWidth
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 10-23-2023, 01:07 AM
hank1234 hank1234 is offline Issue with logo proportions after insertion in header Windows 11 Issue with logo proportions after insertion in header Office 2016
Novice
Issue with logo proportions after insertion in header
 
Join Date: Sep 2020
Posts: 9
hank1234 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
If you aren't getting consistent behaviour of the locked aspect ratio then ignore it and make use of the scaleheight and scalewidth eg
Code:
aShp.Width = CentimetersToPoints(3.94)
aShp.ScaleHeight = aShp.ScaleWidth
Unfortunately, leaving the LockAspectRatio out and adding these two lines of code doesn't do the trick either. I get an error 5148 in the new line of code
Reply With Quote
  #6  
Old 10-23-2023, 02:00 AM
Italophile Italophile is offline Issue with logo proportions after insertion in header Windows 11 Issue with logo proportions after insertion in header Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by hank1234 View Post
Thank you for your feedback on the issue.

Firstly, to clarify, the provided testfile1.doc and testfile2.docx do have empty headers where the logos should be inserted. If you save the logos in the C:\Test folder and execute the code on the test documents, you'll be able to replicate the issue I mentioned.

I appreciate your point on setting the height and width explicitly, and I've tried this approach. However, the issue persists specifically with .doc files and .docx files in compatibility mode. Locking the aspect ratio was one of the methods I attempted to maintain the proportions, but it didn't rectify the problem.

Regarding the image formats, I understand the merits of SVGs and PNGs over JPEGs for scaling. But the main concern here isn't about the quality of the scaled image; rather, it's about the proportions of the logos being distorted exclusively in the .doc files and .docx files running in compatibility mode.

If you could help address this specific problem or point me towards any workarounds, it would be greatly appreciated.
When you did your troubleshooting, did you bother to check the height of the "stretched" image? If you had you would see that it is still at the original height.

As I said to you originally you should set the images to the correct height before you insert them. That way you will have less code and no potential for problems.

The following code works perfectly in Compatibility Mode:
Code:
                    ' Check if the current header is for the first page or subsequent pages
                    If oHead.index = wdHeaderFooterFirstPage Then
                        Set oShape = .InlineShapes.AddPicture(filename:=strLogoFrontpage)
                        With oShape
                            .LockAspectRatio = msoTrue
                            .Width = CentimetersToPoints(3.94)
                            .Height = CentimetersToPoints(3.94)
                            .AlternativeText = "frontpage"
                        End With
                    ElseIf oHead.index = wdHeaderFooterPrimary Then
                        Set oShape = .InlineShapes.AddPicture(filename:=strLogoSubsequentPages)
                        With oShape
                            .LockAspectRatio = msoTrue
                            .Width = CentimetersToPoints(0.73)
                            .Height = CentimetersToPoints(0.73)
                            .AlternativeText = "subsequent page"
                        End With
                    End If
Attached Images
File Type: png Screenshot 2023-10-23 095858.png (9.0 KB, 13 views)
File Type: png Screenshot 2023-10-23 095918.png (9.4 KB, 13 views)
Reply With Quote
  #7  
Old 10-31-2023, 03:26 AM
hank1234 hank1234 is offline Issue with logo proportions after insertion in header Windows 11 Issue with logo proportions after insertion in header Office 2016
Novice
Issue with logo proportions after insertion in header
 
Join Date: Sep 2020
Posts: 9
hank1234 is on a distinguished road
Default

That works!! Many thanks!!
Reply With Quote
  #8  
Old 10-31-2023, 02:18 PM
Guessed's Avatar
Guessed Guessed is offline Issue with logo proportions after insertion in header Windows 10 Issue with logo proportions after insertion in header Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

FWIW, I would not recommend code that needs to refer out to external files if you can avoid it. I would save both graphics as building blocks in the same template that houses the code. Then there is less manipulation required to size and place it and less likelihood of the issues with the external files availability.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 10-31-2023, 10:54 PM
hank1234 hank1234 is offline Issue with logo proportions after insertion in header Windows 11 Issue with logo proportions after insertion in header Office 2016
Novice
Issue with logo proportions after insertion in header
 
Join Date: Sep 2020
Posts: 9
hank1234 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
FWIW, I would not recommend code that needs to refer out to external files if you can avoid it. I would save both graphics as building blocks in the same template that houses the code. Then there is less manipulation required to size and place it and less likelihood of the issues with the external files availability.
That's an interesting approach. I'll try it out and post my experience here!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
header format issue mfitness92 Word 3 07-31-2022 01:03 AM
VBA Insert Image(logo) into header for multiple Word Docs Axis Word VBA 4 02-09-2022 10:34 PM
Issue with logo proportions after insertion in header Change Logo in Header in many Word-Documents ika Word VBA 15 10-20-2016 11:08 PM
Issue with logo proportions after insertion in header Macro to Insert Logo in header on 1st page only and bookmark it. youseeme Word VBA 9 09-16-2016 05:25 AM
Issue with logo proportions after insertion in header How to print header logo only in 1st page Shafraz Khahir Word 1 11-29-2010 11:52 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:10 PM.


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