Graphic Object Example

The following routine prompts the user for a graphic file. If chosen, it places the graphic in the header of the active sheet as a watermark and sizes it to fit the page:

Sub AddWatermark()

Dim oSheet As Object Dim sFile As String

Set oSheet = ActiveSheet On Error GoTo 0

'Make sure there is an active sheet If Not oSheet Is Nothing Then

'Set the properties of the File Open dialog With Application.FileDialog(msoFileDialogFilePicker)

'Change the default dialog title

.Title = "Insert Graphic In Center Header"

'Allow only one file .AllowMultiSelect = False

'Clear the filters and create your own

'Switch to the custom filter before showing the dialog

.Filters.Add "All Pictures", _

"*.gif; *.jpg; *.jpeg; *.bmp; *.wmf; *.gif;*.emf;*.dib;*.jfif;*.jpe",


'Show thumbnails to display small representation ' of the image

.InitialView = msoFileDialogViewThumbnail

'-1 means they didn't cancel

'Store the chosen file sFile = .SelectedItems(l)

'Set up the graphic in the Header

With .CenterHeaderPicture .Filename = sFile .ColorType = msoPictureWatermark .LockAspectRatio = True

'Make it fill the page 'c Assumes a letter size portrait) .Width = Application.InchesToPoints(17) End With

'Make the graphic appear 'Without this, nothing happens .CenterHeader = "&G" End With

End If

'Remove the filter when done .Filters.Clear

End With



End Sub

0 0

Post a comment