I want insert watermark for all files in VBA

quangthay

New Member
Joined
Sep 30, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi everybody,
I want insert watermark for all files in VBA but I haven't found a solution yet.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to the MrExcel Message Board!

Hi everybody,
I want insert watermark for all files in VBA but I haven't found a solution yet.

How about looping through .xlsx files in the selected folder, opening them one by one, applying the watermark by using the .PageSetup property, and save & close each workbook.

Something like the following as a starting point:
Notes:
  • You need to change the image file path with yours: strImageFile
  • The current code applies the watermark to all worksheets in the workbook. If you have multiple sheets and only want to apply the watermark to the first sheet, then the code needs to be changed for that.
  • The current code is looking for the .xlsx files only. If you have other Excel files with different extensions, then the code needs to be changed for that as well.
Please test it first with sample workbook files since the code is going to overwrite the files in the selected folder.

VBA Code:
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
 
Upvote 0
Solution
Thank you very much.
I tested the program but it shows problem
Although i try to fix it by going to trust center but it still doesn't work
1664762368212.png
 
Upvote 0
Try removing the '?' in that first line of code below the 'Dim' section.
 
Upvote 0
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.
 
Upvote 0
Thank you for your feedback
I removed the ? redundancy and moving images to another folder avoids security issues
now it has problems about CenterHeader.
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.
1664870094899.png
 
Upvote 0
Thank you very much
I have run the program successfully after using it with the printer.
Wishing you a productive day.
Once again thank you so much smozgur and johnnyL.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,310
Members
452,906
Latest member
phanmemchatdakenhupviral

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