Jump to content

Recommended Posts

Posted (edited)

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.
Posted

  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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

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.

Posted

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted (edited)

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
Posted

Glad you got it working :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...