dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,392
- Office Version
- 365
- 2016
- Platform
- Windows
I have a drop down box on a sheet called Start_here with 2 options and each option is a town. When the option is selected and a Continue button is pressed I need a header and footer to be loaded onto the sheet quote_sheet depending on the town selected.
This is the code that I have tried to find in various places online but it won't work. Could someone help me with the correct code please?
- The headers and footers are images and are stored on a sheet called Sheet2.
- The image names are
- Town 1
- ImgWestHeader
- ImgWestFooter
- Town 2
- ImgRivHeader
- ImgDYFooter
- Town 1
This is the code that I have tried to find in various places online but it won't work. Could someone help me with the correct code please?
Code:
Private Sub cmdContinue_Click()
Call Header_footer
With ThisWorkbook
.Worksheets("quote_sheet").Visible = True
.Worksheets("quote_sheet").Activate
.Worksheets("Start_here").Visible = xlVeryHidden
End With
End Sub
Sub Header_footer()
Dim city As String
'The city variable becomes the value selected on the Start_here sheet at H9
city = ThisWorkbook.Worksheets("Start_here").Range("H9").Value
Select Case city
Case Is = "Town1"
Call Town1_header_footer
Case Is = "Town2"
Call Town2_header_footer
End Select
End Sub
Sub Town1_header_footer()
Dim printWorksheet As Worksheet, logoShape As Shape, tempImageFile As String
'The worksheet on which the print page setup will apply
Set printWorksheet = ThisWorkbook.Worksheets("quote_sheet")
'The name of shape to be used in page setup
Set logoShape = ThisWorkbook.Worksheets("sheet2").Shapes("ImgWestHeader")
'Save the shape as a temporary image
tempImageFile = Environ("temp") & "\image.jpg"
Save_Object_As_Picture logoShape, tempImageFile
With printWorksheet.PageSetup
.CenterHeaderPicture.fileName = tempImageFile
.CenterHeader = "&G"
' .CenterFooter =
End With
Kill tempImageFile
End Sub
Sub Town2_header_footer()
Dim printWorksheet As Worksheet, logoShape As Shape, tempImageFile As String
Dim footshape As Shape
'The worksheet on which the print page setup will apply
Set printWorksheet = ThisWorkbook.Worksheets("quote_sheet")
'The sheet location and name of shape to be used in page setup
Set logoShape = ThisWorkbook.Worksheets("sheet2").Shapes("ImgRivHeader")
Set footshape = ThisWorkbook.Worksheets("sheet2").Shapes("ImgDYFooter")
'Save the shape as a temporary image
tempImageFile = Environ("temp") & "\image.jpg"
Save_Object_As_Picture logoShape, tempImageFile
With printWorksheet.PageSetup
.CenterHeaderPicture.fileName = tempImageFile
.CenterHeader = "&G"
End With
Kill tempImageFile
tempImageFile = Environ("temp") & "\image.jpg"
Save_Object_As_Picture footshape, tempImageFile
With printWorksheet.PageSetup
.CenterFooterPicture.fileName = tempImageFile
.CenterFooter = "&G"
End With
Kill tempImageFile
End Sub
Private Sub Save_Object_As_Picture(saveObject As Object, imageFileName As String)
Dim temporaryChart As ChartObject
Application.ScreenUpdating = False
saveObject.CopyPicture xlScreen, xlPicture
Set temporaryChart = ActiveSheet.ChartObjects.Add(0, 0, saveObject.Width + 1, saveObject.Height + 1)
With temporaryChart
.Activate
.border.LineStyle = xlLineStyleNone 'No border
.Chart.Paste
.Chart.Export imageFileName
.Delete
End With
Application.ScreenUpdating = True
Set temporaryChart = Nothing
End Sub