Insert image from sheet to header in the first page

sharifadabi

New Member
Joined
Nov 26, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, I have found this nice code here for inserting image from a sheet to header, but I have problem in using that for the first page:

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
FirstPage.CenterHeader.Picture.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
Could you please help me?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try the following instead . . .

VBA Code:
With ActiveSheet.PageSetup
    .CenterHeaderPicture.Filename = tempImageFile
    .CenterHeader = "&G"
End With

Although, to speed up things, I would suggest setting the PrintCommunication property to False before setting the properties, and then back to True afterwards . . .

VBA Code:
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .CenterHeaderPicture.Filename = tempImageFile
    .CenterHeader = "&G"
End With
Application.PrintCommunication = False

As per the following link . . .

Application.PrintCommunication property (Excel)

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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