Adding Picture from worksheet to Header (VBA)

Flycrj

New Member
Joined
Jan 9, 2020
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I'm Trying to add a picture from the worksheet (Picture 1) to the header and I'm getting a Run-time error '438' Object doesn't support this property or method.

The error happens on this line ActiveSheet.PageSetup.CenterHeaderPicture = ActiveSheet.Shapes.Range(Array("Picture 1"))

Thanks for your Help!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Welcome to Mr Excel forums.

You have to save the picture (shape) as a (temporary) image file and insert the file in the header with the "&G" formatting code. Try this macro:

VBA Code:
Public Sub Setup_Page_Header()

    Dim shp As Shape
    Dim tempImageFile As String
       
    Set shp = ActiveSheet.Shapes("Picture 1")
   
    'Save the shape as a temporary image file
   
    tempImageFile = Environ("temp") & "\image.bmp"
    Save_Object_As_Bitmap shp, tempImageFile
   
    'Add the file to the page header
   
    With ActiveSheet.PageSetup
        .CenterHeaderPicture.Filename = tempImageFile
        .CenterHeader = "&G"
    End With

    Kill tempImageFile
   
End Sub


Private Sub Save_Object_As_Bitmap(saveObject As Object, imageFileName As String)

    'Save an object in bitmap format.
   
    'Arguments
    'saveObject     - any object in the CopyPicture method's 'Applies To' list, for example a Range or Shape
    'imageFileName  - the .bmp, .gif, .jpg, or .png file name (including folder path if required) the object will be saved as
   
    Dim temporaryChart As ChartObject
    
    saveObject.CopyPicture xlScreen, xlBitmap
   
    Set temporaryChart = ActiveSheet.ChartObjects.Add(0, 0, saveObject.Width + 6, saveObject.Height + 6)
    With temporaryChart
        .Activate                                'Required, otherwise image is blank with Excel 2016
        .Border.LineStyle = xlLineStyleNone      'No border
        .Chart.Paste
        .Chart.Export imageFileName
        .Delete
    End With
   
    Set temporaryChart = Nothing
   
End Sub
 
Last edited:
Upvote 0
I still cannot use the code for the header in the first page:

FirstPage.CenterHeader.Picture.Filename = tempImageFile

it receives the same error
 
Upvote 0
I still cannot use the code for the header in the first page:

FirstPage.CenterHeader.Picture.Filename = tempImageFile

it receives the same error

Try this macro. It puts the first picture (shape) on the active sheet in the header for the first page and the second picture for the other page(s).

VBA Code:
Public Sub Setup_Page_Header_Different_First_Page()

    Dim shpFirstPage As Shape, shpOtherPages As Shape
    Dim tempImageFile1 As String, tempImageFile2 As String
       
    Set shpFirstPage = ActiveSheet.Shapes(1)
    Set shpOtherPages = ActiveSheet.Shapes(2)
   
    'Save the shapes as temporary image files
   
    tempImageFile1 = Environ("temp") & "\image1.bmp"
    Save_Object_As_Bitmap shpFirstPage, tempImageFile1
    tempImageFile2 = Environ("temp") & "\image2.bmp"
    Save_Object_As_Bitmap shpOtherPages, tempImageFile2
   
    'Add the images to the first page header and other pages
   
    With ActiveSheet.PageSetup
        .DifferentFirstPageHeaderFooter = True
        .FirstPage.CenterHeader.Picture.Filename = tempImageFile1
        .FirstPage.CenterHeader.Text = "&G"
        .CenterHeaderPicture.Filename = tempImageFile2
        .CenterHeader = "&G"
    End With

    Kill tempImageFile1
    Kill tempImageFile2
   
End Sub

Private Sub Save_Object_As_Bitmap(saveObject As Object, imageFileName As String)

    'Save an object in bitmap format.
  
    'Arguments
    'saveObject     - any object in the CopyPicture method's 'Applies To' list, for example a Range or Shape
    'imageFileName  - the .bmp, .gif, .jpg, or .png file name (including folder path if required) the object will be saved as
  
    Dim temporaryChart As ChartObject
   
    saveObject.CopyPicture xlScreen, xlBitmap
  
    Set temporaryChart = ActiveSheet.ChartObjects.Add(0, 0, saveObject.Width + 6, saveObject.Height + 6)
    With temporaryChart
        .Activate                                'Required, otherwise image is blank with Excel 2016
        .Border.LineStyle = xlLineStyleNone      'No border
        .Chart.Paste
        .Chart.Export imageFileName
        .Delete
    End With
  
    Set temporaryChart = Nothing
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,920
Messages
6,175,373
Members
452,638
Latest member
Oluwabukunmi

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