Jump to content

Inserting multiple images onto an existing word document


Recommended Posts

Hello all,

I am new to scripting with AutoIt and I'm trying to insert multiple images into an existing word document (in between words). Is this possible with AutoIt?

I had a look at the script below and I have changed the file path but instead of putting the images into the existing word document, it creates a new word document. I have also changed the path to the image location but it's just showing a blank text box.

'?do=embed' frameborder='0' data-embedContent>>

It would be great if I can obtain some help :).

Many thanks in advance!

Link to comment
Share on other sites

Hi,

Try this :

#include <Word.au3>

$oWord = _WordAttach("Document1 - Microsoft Word", "title")

$oDoc = _WordDocGetCollection($oWord, 0)

_WordDocAddPicture($oDoc, @ScriptDir & "\t.jpg", 0, 1)
Note: Don't forget to change the title or use another mode, change the picture path too.

Br, FireFox.

Link to comment
Share on other sites

Create a range object where you want to insert the page break and use:

$oRng.InsertBreak($wdPageBreak)

$wdPageBreak is one of the WdBreakType enumeration.

How to create a range you could have a look at my WordEX UDF (unfortunately incompatible with Word UDF)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

This is the original code

#include <lib/WordEX.au3>
#include <Word.au3>

$sFile = "Test.docx"

$oApp1 = _Word_Create(Default, Default)
_Word_DocOpen($oApp1, $sFile)

$oDoc = _WordDocGetCollection($oApp1, 0)

; Image 1
$tbox_left = 200
$tbox_top = 200
$tboxw = 250
$tboxh = 200
$TextVertical = 1 ; 1 or 5

$oShape1 = $oDoc.Shapes.AddCanvas($oApp1.PixelsToPoints($tbox_left), $oApp1.PixelsToPoints($tbox_top), $oApp1.PixelsToPoints($tboxw), $oApp1.PixelsToPoints($tboxh))
$oTextbox = $oShape1.CanvasItems.AddTextbox($TextVertical, 1, 1, $oShape1.Width - 1, $oShape1.Height - 1)

$oRange1 = $oTextbox.TextFrame.TextRange     
_WordDocAddPicture($oDoc, @ScriptDir & "\image.jpg", False, True, $oRange1)

; Image 2
$tbox_left2 = 200
$tbox_top2 = 500
$tboxw2 = 250
$tboxh2 = 200
$TextVertical2 = 1 ; 1 or 5

$oShape2 = $oDoc.Shapes.AddCanvas($oApp1.PixelsToPoints($tbox_left2), $oApp1.PixelsToPoints($tbox_top2), $oApp1.PixelsToPoints($tboxw2), $oApp1.PixelsToPoints($tboxh2))
$oTextbox2 = $oShape2.CanvasItems.AddTextbox($TextVertical2, 1, 1, $oShape2.Width - 1, $oShape2.Height - 1)


$oRange2 = $oTextbox2.TextFrame.TextRange
_WordDocAddPicture($oDoc, @ScriptDir & "\image2.jpg", False, True, $oRange2)

Instead of inserting a page break, I have decided to go by a different method - Replacing texts with the appropriate images.

I am using the WordEX UDF and the code below works.

$sFindText = "Image1"
$sToReplace = "Image Here"
_Word_DocFindReplace($oDoc, $sFindText, $sToReplace)

However when I try to replace the word with an image in a text box, as the original code, I receive an error message (WordEX error 423). The script still places the image within the specified parameters above, in the word document but it is not replacing the word "Image1".

$sFindText = "Image1"
$oRange1 = $oTextbox.TextFrame.TextRange
$sReplaceWith = _WordDocAddPicture($oDoc, @ScriptDir & "\image.jpg", False, True, $oRange1)
_Word_DocFindReplace($oDoc, $sFindText, $sReplaceWith)

Is there a way to create a loop/tag replacement where the following is done:
1) Find text "Image1"

2) Replace "Image1" with the Image I want, at the coordinates of "Image1" (Optionally in a textbox)

3) Repeat 1 for "Image2" etc.

I am hoping that with this, I don't have to create page breaks and the script will just replace all instances of "Image1" in the word document with a picture of  "Image1". The idea is to place images in between words.

Sorry to be such a nuisance but any help is very much appreciated! :)

Edited by Xavianna
Link to comment
Share on other sites

Can't test at the moment but I hope to come up with a solution quite soon.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

Xavianna,

you are mixing the old and the new Word UDF. I've never tested this, so I'm not sure it works.

"WordEX Error 423": Is this the value of @error or @extended? The UDF never returns such an error.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

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