Sign in to follow this  
Followers 0
Xavianna

Inserting multiple images onto an existing word document

8 posts in this topic

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!

Share this post


Link to post
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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Would you also be able to advise on how to add or create a new page within a word document please?

Thank you!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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
Sign in to follow this  
Followers 0