Jump to content

Struggling with _Word_DocPictureAdd


Go to solution Solved by Nine,

Recommended Posts

Hello,

 

so I have started to learn to use the Word UDF and got issue to add my pictures after exact paragraphs. I was searching in the forum for the solution, checked with examples and still I don't understand how to add pictures after paragraph in new line.

 

My word document has like 8 pages and for example on page I have paragraph named "My examples:" and here starts my problem.

 

I have tried to do this:

#include <Word.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($Word, @ScriptDir & "\examples.docx", Default, Default, True)

$oSearchRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 0)
$oRangeFound = _Word_DocFind($oDoc, "My examples:", $oSearchRange)

_Word_DocPictureAdd($oDoc, @ScriptDir & "\pic1.jpg", Default, Default, $oRangeFound)

And here the picture adds before the paragraph My examples: on same line and looks like

{pic1}My Examples:

 

What I want to see is:

My examples:

{PICTURE IN NEW LINE} which is pic1.jpg from my code.

 

How I can do that? I want to add 3 pictures in a row in new line each like:

My examples:

{pic1.jpg}

{pic2.jpg}

{pic3.jpg}

 

Hope I explained this well, but you can ask me if you need any additional information to clarify.

Edited by diff
Link to comment
Share on other sites

  • Solution

There was a number of typos in your snippet, but here a clean working example :

#include <Word.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\V.doc", Default, Default, True)

$oSearchRange = _Word_DocRangeSet($oDoc, -1)
$oRangeFound = _Word_DocFind($oDoc, "Dijon", $oSearchRange)
$oSearchRange = _Word_DocRangeSet($oDoc, $oRangeFound, $wdParagraph, 1)
_Word_DocPictureAdd($oDoc, @ScriptDir & "\Pic1.jpg", Default, Default, $oRangeFound)

 

Link to comment
Share on other sites

4 minutes ago, Nine said:

There was a number of typos in your snippet, but here a clean working example :

#include <Word.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\V.doc", Default, Default, True)

$oSearchRange = _Word_DocRangeSet($oDoc, -1)
$oRangeFound = _Word_DocFind($oDoc, "Dijon", $oSearchRange)
$oSearchRange = _Word_DocRangeSet($oDoc, $oRangeFound, $wdParagraph, 1)
_Word_DocPictureAdd($oDoc, @ScriptDir & "\Pic1.jpg", Default, Default, $oRangeFound)

 

Yeah, sorry for the typos as I wasn't be able to copy now the code so I wrote from my memory. I have fixed the first post now.

 

I have quickly checked your solution and looks like now the image is inserted exactly where I needed, I will try later to do this with more pictures in a row if that will work.

 

Thank you for the solution, looks so simple where I was stuck :sweating:

Link to comment
Share on other sites

Sorry for double post, just adding solution with multiple pictures if someone will need it

 

I added a loop to add more screenshots, probably it could be written better, but it's working for me and I am still learning ;)

So the pictures will be added in ascending order like

My examples:

{Pic1.jpg}

{Pic2.jpg}

{Pic3.jpg}

My extra code below on @Nine example:

#include <Word.au3>

$picName = "Pic"
$picFormat = ".jpg"

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\V.doc", Default, Default, True)

$oSearchRange = _Word_DocRangeSet($oDoc, -1)
$oRangeFound = _Word_DocFind($oDoc, "Dijon", $oSearchRange)
$oSearchRange = _Word_DocRangeSet($oDoc, $oRangeFound, $wdParagraph, 1)

Local $i = 1

While 1

    _Word_DocPictureAdd($oDoc, @ScriptDir & "\" & $picName & $i & $picFormat, Default, Default, $oRangeFound)
    _Word_DocRangeSet($oDoc, $oRangeFound, $wdParagraph, 1) ;   to add pics in ascending order
    $i += 1

    If FileExists(@ScriptDir & "\" & $picName & $i & $picFormat) = 0 Then
        ExitLoop
    EndIf

WEnd

 

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

×
×
  • Create New...