adding a logo to spreadsheet header

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
when printing a sheet, I have the code below (example) which puts a logo in the left header section of my printout.
The logo is stored on a path in C:

Now, when this path changes, or when the logo would not exist, I want NO LOGO to be printed.

However, it remains being printed. Can anyone solve ?

VBA Code:
Sub AddFooterHeaderImage()

Dim sht As Worksheet
Dim ImagePath As String
Dim Validation As String

'Where is Image Located?
  ImagePath = "C:\Users\bob\Documents\Company Logo.png"
  
'Does the Image File Exist?
  On Error Resume Next
    Validation = Dir(ImagePath)
  On Error GoTo 0

  If Validation = "" Then
    MsgBox "Could not locate the image file located here: " & ImagePath
    Exit Sub
  End If
      
'Add Image To Each Active Sheet
  For Each sht In ActiveWorkbook.Windows(1).SelectedSheets
    
    'Insert Image (ie "LeftFooter","CenterFooter","RightFooter", _
                      "LeftHeader","CenterHeader","RightHeader")
      sht.PageSetup.LeftFooter = "&G"
      sht.PageSetup.LeftFooterPicture.Filename = ImagePath
    
    'Ensure Pagebreaks don't show (they are annoying!)
      sht.DisplayPageBreaks = False
  
  Next sht

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Once you set up the logo, if you later execute that code and the file is not found, the code just exits, so it makes no change to the footer. Code below does not exit, and updates the footer accordingly.

Rich (BB code):
Sub AddFooterHeaderImage()

Dim sht As Worksheet
Dim ImagePath As String
Dim Validation As String

'Where is Image Located?
  ImagePath = "C:\Users\bob\Documents\Company Logo.png"
 
'Does the Image File Exist?
  On Error Resume Next
    Validation = Dir(ImagePath)
  On Error GoTo 0

  If Validation = "" Then
    MsgBox "Could not locate the image file located here: " & ImagePath
    ' Exit Sub removed
  End If
   
'Add Image To Each Active Sheet
  For Each sht In ActiveWorkbook.Windows(1).SelectedSheets
 
    'Insert Image (ie "LeftFooter","CenterFooter","RightFooter", _
                      "LeftHeader","CenterHeader","RightHeader")
      sht.PageSetup.LeftFooter = "&G"
      If Validation = "" Then
         sht.PageSetup.LeftFooterPicture.Filename = ""
         sht.PageSetup.LeftFooter = sht.PageSetup.LeftFooter & " NO LOGO"

      Else
         sht.PageSetup.LeftFooterPicture.Filename = ImagePath
      End If
 
    'Ensure Pagebreaks don't show (they are annoying!)
      sht.DisplayPageBreaks = False
 
  Next sht

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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