Hi everybody,
I want insert watermark for all files in VBA but I haven't found a solution yet.
.PageSetup
property, and save & close each workbook. strImageFile
.xlsx
files only. If you have other Excel files with different extensions, then the code needs to be changed for that as well.Sub InsertWatermark()
Dim strImageFile As String
Dim dlgFolder As FileDialog
Dim strFile As String
Dim wrk As Workbook
Dim sht As Worksheet
strImageFile = "C:\Somewhere\Pictures\watermark.jpg"
Set dlgFolder = Application.FileDialog(msoFileDialogFolderPicker)
With dlgFolder
.Show
If .SelectedItems.Count Then
strFile = Dir(.SelectedItems(1) & "\*.xlsx", vbNormal)
Do While strFile <> ""
Set wrk = Application.Workbooks.Open(.SelectedItems(1) & "\" & strFile)
For Each sht In wrk.Worksheets
With sht.PageSetup
.CenterHeaderPicture.Filename = strImageFile
.CenterHeader = "&G"
End With
Next sht
wrk.Close True
strFile = Dir
Loop
End If
End With
End Sub
After removing the mistyped ? as @johnnyL explained, please rerun the code and when the execution halts with the error, click on the Debug button and let us know the highlighted line in the code pane. We are looking for the code line causes the error.
I assume it is about the image file folder, may be the folder permissions or something like that, but it would help if we know the exact error line.