Jump to content
usick

[SOLVED] Adding a watermark or a picture to all pages of a word document.

Recommended Posts

usick

Hi,

I am trying to add a watermark or a picture to all pages (and sections) of a word document.

I generated a VBA macro in Word:

ActiveDocument.Sections(1).Range.Select
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.HeaderFooter.Shapes.AddPicture(FileName:= _
        "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", LinkToFile:= _
        False, SaveWithDocument:=True).Select
    Selection.ShapeRange.Name = "WordPictureWatermark200631871"
    Selection.ShapeRange.PictureFormat.Brightness = 0.85
    Selection.ShapeRange.PictureFormat.Contrast = 0.15
    ' more settings go here
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

 

My attempt in AutoIT:

$oWord = _Word_Create()
$sFileName = ""
$oDoc = _Word_DocOpen($oWord,$sFileName, Default,Default,True)

$sImgFilePath = ""
$bLinkToFile = False
$bSaveWithDocument = True

$oRange= $oDoc.Sections(1).HeaderFooter.Range
$oWord.ActivePane.View.SeekView.wdSeekCurrentPageHeader
$oShape = $oDoc.Shapes.AddPicture($sImgFilePath, $bLinkToFile, $bSaveWithDocument, $oRange)

$oShape.ShapeRange.Name = "Picture"
; more settings go here

I was able to add a picture in front of text to a single page, but I cannot access the properties of the returned object $oShape.

Suggestions or solutions would be appreciated. Thanks for your time.

I believe this is the 114000th topic in this forum :).

Edited by usick
Added [SOLVED] to title.

Share this post


Link to post
Share on other sites
water

  I was able to add a picture in front of text to a single page, but I cannot access the properties of the returned object $oShape.

What have you tried (please show us the code) and what are the results (error messages etc.)?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
usick

Hi water,
The full code looks like:

#include <Word.au3>

$oWord = _Word_Create()
$sFileName = @ScriptDir & "\testDoc.docx"
$oDoc = _Word_DocOpen($oWord,$sFileName, Default,Default,True)

$sImgFilePath = @ScriptDir & "\testImg.jpg"
$bLinkToFile = False
$bSaveWithDocument = True

$oRange= $oDoc.Sections(1).HeaderFooter.Range
$oWord.ActivePane.View.SeekView.wdSeekCurrentPageHeader
$oShape = $oDoc.Shapes.AddPicture($sImgFilePath, $bLinkToFile, $bSaveWithDocument, $oRange)
If IsObj($oShape) Then
    ConsoleWrite("The variable is an object" & @CRLF) ; true
Else
    ConsoleWrite("The variable is not an object" & @CRLF)
EndIf
$oShape.ShapeRange.Name = "Picture"
ConsoleWrite(@error) ;-2147352570
; more settings go here
$oShape.ShapeRange.PictureFormat.Brightness = 0.85
$oShape.ShapeRange.PictureFormat.Contrast = 0.15
; etc.
$oWord.ActivePane.View.SeekView.wdSeekMainDocument

 

The image is added and the variable $oShape is an object but when I try to access its property using the dot notation the error is "-2147352570". I hope that answers your question.

Share this post


Link to post
Share on other sites
water

As

$oDoc.Shapes.AddPicture

returns a Shape object your commands should look like:

$oShape.PictureFormat.Brightness = 0.85

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
usick

For anyone else, here is the solution I came up with:

#include <Word.au3>

$oWord = _Word_Create()
$sFileName = @ScriptDir & "\testDoc.docx"
$oDoc = _Word_DocOpen($oWord,$sFileName, Default,Default,True)
$iPages= $oDoc.ComputeStatistics(2)                             ; no. of pages
;$iPage = $oWord.Selection.Information(3)                       ; current page

$sImgFilePath = @ScriptDir & "\testImg.jpg"
$bLinkToFile = False
$bSaveWithDocument = True

$oWord.Selection.GoTo(1,1)                                  ; go to 1st page
For $j = 1 To $iPages
    $oShape = $oDoc.Shapes.AddPicture($sImgFilePath, $bLinkToFile, _
                                        $bSaveWithDocument)
    $oShape.PictureFormat.Brightness = 0.85
    $oShape.PictureFormat.Contrast = 0.15
    $oShape.PictureFormat.LockAspectRatio = False
    $oShape.Height = 100
    $oShape.Width = 100
    $oShape.Top = 20
    $oShape.Left = 350
    $oShape.WrapFormat.AllowOverlap = True
    $oShape.WrapFormat.Side.wdWrapNone
    $oShape.WrapFormat.Type = 5
    $oWord.Selection.GoTo(1,2)                              ; go to next page
Next

 

Edited by usick

Share this post


Link to post
Share on other sites
water

Glad you got it working :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×