Adding watermark in word using Access VbA

for example :

Public WordApp As Word.Application
Public WordDoc As Word.Document
Public WordLogo As Word.InlineShape

'Background Picture
If Not fnBackGroundPic = "" Then
Set WordLogo = WordApp.ActiveDocument.Bookmarks("BackGroundPicture").Range.InlineShapes.AddPicture(FileName:=fnBackGroundPic, LinkToFile:=False, SaveWithDocument:=True)
With WordLogo
.ConvertToShape
.LockAspectRatio = msoTrue
.Range.ShapeRange.WrapFormat.Type = wdWrapBehind
.PictureFormat.ColorType = msoPictureGrayscale
'Debug.Print .Title
.PictureFormat.Contrast = 0.4
.PictureFormat.Brightness = 0.8
.Width = 538.58
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.ParagraphFormat.Alignment = wdAlignParagraphJustifyLow
.Range.ParagraphFormat.LeftIndent = WordApp.CentimetersToPoints(-1#)
.Range.ShapeRange.Align msoAlignCenters, True
.Range.ShapeRange.Align msoAlignMiddles, True
.Range.ParagraphFormat.SpaceBeforeAuto = False
.Range.ParagraphFormat.SpaceAfterAuto = False
End With
End If
 
Thanks Contractor. The code below is not giving any error message but its not inserting the watermark as well. I have a word document already created and if I want to set that as a active document then how can I do that? As I want to insert watermark to that word document.
Code:
Private Sub Command0_Click()
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim WordLogo As Word.InlineShape
'Background Picture
If Not fnBackGroundPic = "" Then
Set WordLogo = WordApp.ActiveDocument.Bookmarks("BackGroundPicture").Range.InlineShapes.AddPicture(FileName:="C:\Documents and Settings\Desktop\LTC.JPEG", LinkToFile:=False, SaveWithDocument:=True)
With WordLogo
.ConvertToShape
.LockAspectRatio = msoTrue
.Range.ShapeRange.WrapFormat.Type = wdWrapBehind
.PictureFormat.ColorType = msoPictureGrayscale
'Debug.Print .Title
.PictureFormat.Contrast = 0.4
.PictureFormat.Brightness = 0.8
.Width = 538.58
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.ParagraphFormat.Alignment = wdAlignParagraphJustifyLow
.Range.ParagraphFormat.LeftIndent = WordApp.CentimetersToPoints(-1#)
.Range.ShapeRange.Align msoAlignCenters, True
.Range.ShapeRange.Align msoAlignMiddles, True
.Range.ParagraphFormat.SpaceBeforeAuto = False
.Range.ParagraphFormat.SpaceAfterAuto = False
End With
End If
End Sub
Thanks
 
Hi aman,

You have to add a bookmark on your word document in order to insert the jpg file,

Needed reference :

Microsoft Word 14.0 Object lib
microsoft Office 14.0 object Lib
 
Thanks contractor. Sorry i didn't make it clear that I want Access/vba code. As I developed a system in Access and now I want the code that will check if the user click on Button1 then Watermark1 should get displayed on already created word document name WDoc1. If Button2 is clicked then Watermark2 should be displayed on Wdoc1 and so on..

So I need code in Access/vba that will look for word documwent Wdoc in "C:\Folder1" and insert the corresponding Watermark on it.

Pls help me in this.

Regards,
Aman
 
Hi Aman,

It is not important that you may or may not have created a word document,
You need to assign a bookmark to that document called BackgroundPicture
so your code can insert the corresponding watermark.

Did you looked at my demo ???
Use your imagination.....

You can also record a macro with a word document if you want to insert text watermark
see if dat helps....

regards,
 
Hi Contractor

Can u pls make some changes in the code so that it works according to my requirements. The following code doesn't display any error message but I want it to open the mentioned word document and insert the watermark on it.

Code:
Private Sub Command0_Click()
Dim WordApp As Object
Dim WordDoc As String
Dim WordLogo As Word.InlineShape
WordDoc = "C:\Documents and Settings\Desktop\Doc1.doc"
'Background Picture
If Not fnBackGroundPic = "" Then
 WordApp.Documents.Open WordDoc
Set WordLogo = WordApp.ActiveDocument.Bookmarks("BackGroundPicture").Range.InlineShapes.AddPicture(FileName:="C:\Documents and Settings\Amanpreet Kaur\Desktop\LTC.JPEG", LinkToFile:=False, SaveWithDocument:=True)
With WordLogo
.ConvertToShape
.LockAspectRatio = msoTrue
.Range.ShapeRange.WrapFormat.Type = wdWrapBehind
.PictureFormat.ColorType = msoPictureGrayscale
'Debug.Print .Title
.PictureFormat.Contrast = 0.4
.PictureFormat.Brightness = 0.8
.Width = 538.58
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.ParagraphFormat.Alignment = wdAlignParagraphJustifyLow
.Range.ParagraphFormat.LeftIndent = WordApp.CentimetersToPoints(-1#)
.Range.ShapeRange.Align msoAlignCenters, True
.Range.ShapeRange.Align msoAlignMiddles, True
.Range.ParagraphFormat.SpaceBeforeAuto = False
.Range.ParagraphFormat.SpaceAfterAuto = False
End With
End If
End Sub

Many Thanks
 
Hi aman,

code behind Form on Button :
Code:
Private Sub Command0_Click()
Call WordSetup("C:\Documents and Settings\Desktop\Doc1.doc", "C:\Documents and Settings\Amanpreet Kaur\Desktop\LTC.JPEG")
End Sub

Code Behind Module :
Code:
Sub WordSetup(fnTemplate As String, fnBackGroundPic As String)
    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    
    If Err.Number <> 0 Then
            'Launch a new instance of Word
            Err.Clear
        On Error GoTo ErrorHandler
        Set WordApp = CreateObject("Word.Application") 'New Word.Application
    End If
    WordApp.Documents.Add (fnTemplate)
    Set WordDoc = WordApp.ActiveDocument
    WordApp.Visible = True
    InsertHeaderLogo (fnBackGroundPic)

Exit_ErrorHandler:
    Exit Sub

ErrorHandler:
        msgbox Err.Number & vbcrlf & Err.Description & vbcrlf & vbcrlf & "Calling Proc: WordSetup()"  ,vbOKOnly,"Error"
        Resume Exit_ErrorHandler

End Sub



Public Function InsertHeaderLogo(fnBackGroundPic As String)
Dim Shp As Word.Shape

    On Error Resume Next
    'Background Picture
    If Not fnBackGroundPic = "" Then
        Set WordLogo = WordApp.ActiveDocument.Bookmarks("BackGroundPicture").Range.InlineShapes.AddPicture(FileName:=fnBackGroundPic, LinkToFile:=False, SaveWithDocument:=True)
            With WordLogo
                .ConvertToShape
                .LockAspectRatio = msoTrue
                .Range.ShapeRange.WrapFormat.Type = wdWrapBehind
                .PictureFormat.ColorType = msoPictureGrayscale
                'Debug.Print .Title
                .PictureFormat.Contrast = 0.4
                .PictureFormat.Brightness = 0.8
                .Width = 538.58
                .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
                .Range.ParagraphFormat.Alignment = wdAlignParagraphJustifyLow
                .Range.ParagraphFormat.LeftIndent = WordApp.CentimetersToPoints(-1#)
                .Range.ShapeRange.Align msoAlignCenters, True
                .Range.ShapeRange.Align msoAlignMiddles, True
                .Range.ParagraphFormat.SpaceBeforeAuto = False
                .Range.ParagraphFormat.SpaceAfterAuto = False
            End With
    End If
End Function

If this doesn't help than i don't now how to help you !!!

!!!Don't forget to add a bookmark called "BackGroundPicture"!!!

Regards,
Contractor
 
Last edited:
Thanks Contractor and sorry to bother u again. The code Gives me compile error at following line:

On Error GoTo ErrorHandler

Please could u help me in this? Thanks again for your help so far.
 
Just wondering why don't you just create an instance of Word despite any open Word Objects?

The reason for your Error is because you ask the compiler to GoTo ErrorHandler, but have faile to say where that is, try the following..
Code:
Sub WordSetup(fnTemplate As String, fnBackGroundPic As String)
On Error GoTo ErrorHandler
    [COLOR=Blue][B]Set WordApp = CreateObject("Word.Application") [/B][/COLOR][COLOR=Green]'New Word.Application[/COLOR]
    WordApp.Documents.Add (fnTemplate)
    Set WordDoc = WordApp.ActiveDocument
    WordApp.Visible = True
    InsertHeaderLogo (fnBackGroundPic)
[COLOR=Red][B]ExitErrorHandler:
    Exit Sub
ErrorHandler:
    MsgBox "Error (" & Err.Number & ") : " & Err.Description & vbCrLf & vbCrLf & "Exiting procedure - WordSetUp", vbCritical
    Resume ExitErrorHandler[/B][/COLOR]
End Sub
 
Hi aman,

Sorry for that, i forgot to add the errorhandler to the code,

i have edited my privous post,

regards,
Contractor
 
Thanks pr2-eugin The error has been resolved but I can't see any watermark on the word document using this code.

Any help would be much appreciated.

Thanks
 
Thanks Contractor. The code automatically opens the word document but i can't see any watermark on it. Would you please help me in this.

Regards,
Aman
 
Aman, Did you added a bookmark on your word Document called : BackGroundPicture ?

If you don't do that nothing will happen,

PS: if you can add example of your database i will look at it.

Contractor
 
Thanks Contractor. Could you please tell me how to add a bookmark on word document? Watermarks and bookmarks are new things for me. I never used that before.

Thanks again.
 
Hi Contractor

Please see attached the watermark that I want to insert in the word document. This containes header and footer (So basically company logo).

So where shall I set bookmark in the word document?

Thanks
 

Attachments

  • PAPSLD.jpg
    PAPSLD.jpg
    86.4 KB · Views: 124
Aman,

You should set the Bookmark in the header section on the first paragraph,

Contractor
 
Hi, I have added bookmark named "BackGroundPicture" but still when I press command button then it opens up a word document but in that I can't see any watermark.

Any suggestions Why its not working?

Thanks
 

Users who are viewing this thread

Back
Top Bottom