Jump to content

Appending picture in Word


Recommended Posts

I want to be able to append a picture and then text.

The functions I have here work fine (much thanks to a lot of help from people here).

Problem I have is the _WordDocAddPicture does not appear to append. So I write text and a picture and then text and a picture. And the document has two pictures and then two pieces of text. What I would want to see it text and picture and then text and picture. Not all the pictures and then all the text.

I do not see an option to append in the help file is there a good way to do this.

>>>>>>>>>>>>>>

Also I want to test to see if word is installed. I would expect to be able to do an if on word_version and if that fails then word is not installed but I have word on current machine and it is saying it is not installed. Any ideas on that?

CODE
#include <Word.au3>

$wordLogFileName="test.doc"

$testPicName="test.jpg"

; $aVersion = _Word_VersionInfo ()

; MsgBox(0, "Word.au3 Version", $aVersion[5] & " released " & $aVersion[4])

; check to see if word is installed

$aVersion = _Word_VersionInfo ()

if Not $aVersion then

MsgBox(0,"", "Word not installed")

EndIf

;Func Main()

LogToWord("Write Something",$testPicName)

LogToWord("Write Something Else",$testPicName)

;EndFunc

;$line=1

;while $line

;FileReadLine("TempLog.txt",$Line)

Func _WordAddLineBreak(ByRef $o_object)

$o_object.Activedocument.Content.InsertParagraphAfter

EndFunc

Func _WordAddSection(ByRef $o_object)

$o_object.Activedocument.Sections.Add

EndFunc

Func _WordAppendText(ByRef $o_object, $sText)

If Not IsObj($o_object) Then

__WordErrorNotify("Error", "_WordAppendText", "$_WordStatus_InvalidDataType")

SetError($_WordStatus_InvalidDataType, 1)

Return 0

EndIf

$o_object.Range.insertAfter($sText)

SetError($_WordStatus_Success)

Return 1

EndFunc ;==>_WordAppendText

Func LogToWord($text,$picName)

$oWordApp = _WordCreate(@ScriptDir & "/" & $wordLogFileName)

$oDoc = _WordDocGetCollection($oWordApp, 0)

$filqq1 = @SCRIPTDIR & "\" & $picName

$oShape = _WordDocAddPicture($oDoc, $filqq1, 0, 1)

_WordAppendText($oDoc, @CRLF & $text & @CRLF & @CRLF)

$oDoc.Save "test.doc" 6

$oDoc.close

WinClose("Microsoft Word")

EndFunc

Link to comment
Share on other sites

Also I want to test to see if word is installed. I would expect to be able to do an if on word_version and if that fails then word is not installed but I have word on current machine and it is saying it is not installed. Any ideas on that?

From the helpfile:

Returns an array of information about the Word.au3 version

It doesn't tell you what version of WORD is installed, but rather what version of the WORD UDF/AU3 (the file containing the _Word functions) you are using.
Link to comment
Share on other sites

I was hoping before I do any word functions to be able to see if the user actually has word installed.

Is the better or only approach then just to check and see if the individual functions fail (appending or adding pictures).

Any idea on why the pictures always go to the front?

Thanks again for all your help

Link to comment
Share on other sites

I was hoping before I do any word functions to be able to see if the user actually has word installed.

Is the better or only approach then just to check and see if the individual functions fail (appending or adding pictures).

Any idea on why the pictures always go to the front?

Thanks again for all your help

Haven't looked at the picture placement problem yet, but here's a _WordGetVersion() function that works as you want

Returns 0 if no scriptable installation of Word is found, otherwise returns version number of current word installation.

(based on http://vb.mvps.org/articles/qa200206.asp)

MsgBox(0, "", _WordGetVersion())

Func _WordGetVersion()
    Local $oWord, $WordVer
    ; Quick test to determine if Word is
    ;installed, and return version.
    $oWord = ObjCreate("Word.Basic")
    If IsObj($oWord) Then
        $WordVer = $oWord.AppInfo(2)
    Else
        $WordVer = 0
    EndIf
    $oWord = ""
    Return $WordVer
EndFunc
Edited by ResNullius
Link to comment
Share on other sites

I was playing around with this some more. The function has a range parameter which lets you specify where in the file the pic goes. Problem is (I think) if you have 50 files and 50 lines of text you would have to go through and keep up with exactly what coordinate to place the file in every time. I want to hope there is a way to append pics just like there is with text.

I was looking a little at this

http://www.autoitscript.com/forum/index.php?showtopic=61543

I am tempted just to go the clipboard method but I don't want to.

Any ideas? Anyone?

Link to comment
Share on other sites

I was hoping the default argument for this would make it append but I am not sure what it does. Was using "" also tried "range" as an argument.

Was looking here at the function in Autoit

http://www.autoitscript.com/forum/index.ph...st&id=14124

and then at the MSDN

http://msdn2.microsoft.com/en-us/library/m...addpicture.aspx

I am not clear on what MS is saying happens with the range argument.

"Range

Optional Object. The location where the picture will be placed in the text. If the range isn't collapsed, the picture replaces the range; otherwise, the picture is inserted. If this argument is omitted, the picture is placed automatically."

I was hoping too that if I moved the cursor to the end then the pic would be placed where the cursor goes but even if I pause execution and manually move the cursor to the end the pic still goes at the top.

Link to comment
Share on other sites

I am not sure what the range variable is doing. I tried the hack below to set the range and make it higher each time but I still get the pic at the top.

CODE
#include <Word.au3>

$wordLogFileName="test.doc"

$testPicName="test.jpg"

$range=1000

LogToWord("Write Something",$testPicName)

LogToWord("Write Something Else",$testPicName)

; check to see if word is installed

Func IsWordInstalled()

Local $oWord, $WordVer

; Quick test to determine if Word is

;installed

$oWord = ObjCreate("Word.Basic")

If IsObj($oWord) Then

$WordVer = $oWord.AppInfo(2)

Return True

Else

$WordVer = 0

Return True

EndIf

;$oWord = ""

;Return $WordVer

EndFunc

Func _WordAddLineBreak(ByRef $o_object)

$o_object.Activedocument.Content.InsertParagraphAfter

EndFunc

Func _WordAddSection(ByRef $o_object)

$o_object.Activedocument.Sections.Add

EndFunc

Func _WordAppendText(ByRef $o_object, $sText)

If Not IsObj($o_object) Then

__WordErrorNotify("Error", "_WordAppendText", "$_WordStatus_InvalidDataType")

SetError($_WordStatus_InvalidDataType, 1)

Return 0

EndIf

$o_object.Range.insertAfter($sText)

SetError($_WordStatus_Success)

Return 1

EndFunc ;==>_WordAppendText

Func LogToWord($text,$picName)

$oWordApp = _WordCreate(@ScriptDir & "/" & $wordLogFileName)

$oDoc = _WordDocGetCollection($oWordApp, 0)

$filqq1 = @SCRIPTDIR & "\" & $picName

;MsgBox(0,"","where am I")

Send( "!{END}" )

$oShape = _WordDocAddPicture($oDoc, $filqq1, 0,$range)

$range=$range+1000

;$oShape = _WordDocAddPicture($oDoc, $filqq1, 0,"range")

_WordAppendText($oDoc, @CRLF & $text & @CRLF & @CRLF)

$oDoc.Save "test.doc" 6

$oDoc.close

WinClose("Microsoft Word")

EndFunc

Link to comment
Share on other sites

Well, I don't know what the deal is with the range parameter; I can't make it work either.

But I did some googling, and the following example will do what you want

I also included a couple of other things I found: inserting the contents of a file into your doc, and putting a border around your picture.

#include <Word.au3>
;http://www.microsoft.com/technet/scriptcenter/resources/officetips/sept05/tips0922.mspx

Const $wdLineStyleSingle = 1

$oWordApp = _WordCreate()
$oDoc = _WordDocGetCollection($oWordApp, 0)

$oSelection = $oWordApp.Selection

$oSelection.TypeText("This is the first line")
$oSelection.TypeParagraph()

$oSelection.TypeText("This is the second line")

;uncomment the next line to insert the contents of a file into the document
;$oSelection.InsertFile(@ScriptDir & "\test.txt")

$oSelection.TypeParagraph()

$oShape = $oSelection.InlineShapes.AddPicture(@ScriptDir & "\test.jpg")

;uncomment the following line to add a border around the picture
;$oShape.Borders.OutsideLineStyle = $wdLineStyleSingle

$oSelection.TypeParagraph()
$oSelection.TypeText("This is the 3rd line")
$oSelection.TypeParagraph()
Link to comment
Share on other sites

Wow, thanks again for the help.

Works great.

Seems to me (and I claim to know nothing) that the standard function should work that way. Maybe there is a good reason I don't know about but the append seems more intuitive or maybe an option to append or have the pic go at the top of the file.

Thanks again.

Link to comment
Share on other sites

Wow, thanks again for the help.

Works great.

Seems to me (and I claim to know nothing) that the standard function should work that way. Maybe there is a good reason I don't know about but the append seems more intuitive or maybe an option to append or have the pic go at the top of the file.

Thanks again.

Your welcome.

I've learned some useful things myself because of this.

If I ever figure out how/why the standard function works/doesn't work I'll post a follow up here.

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