Sign in to follow this  
Followers 0
maestro1024

Copy pic into word doc

14 posts in this topic

Hello,

I want to take a screen cap of a window and insert it in a word doc.

I would like to use the word.au3 library but it does not appear to be working for me.

First question is do I actually have to have word installed (could I get by with using open office)?

Here is what I was trying. I already have a test.doc on the desktop and a test.bmp file.

CODE
#include <Word.au3>

$sPath = "C:\Users\test\Desktop\"

MsgBox(0,"",$sPath)

$search = FileFindFirstFile($sPath & "*.bmp")

; Check if the search was successful

If $search = -1 Then

MsgBox(0, "Error", "No images found")

Exit

EndIf

$oWordApp = _WordCreate ()

$oDoc = _WordDocGetCollection ($oWordApp, 0)

MsgBox(0, "", "obj"+$oDoc)

;While 1

$file = FileFindNextFile($search)

;If @error Then ExitLoop

;$oShape =

if _WordDocAddPicture ($oDoc, $sPath &"test.doc") Then

MsgBox(0, "", "Doc Created")

Else

MsgBox(0, "", "Doc not created")

EndIf

;If Not @error Then $oShape.Range.InsertAfter (@CRLF)

;WEnd

MsgBox(0,"",$sPath & "test.doc")

; Close the search handle

FileClose($search)

Share this post


Link to post
Share on other sites



Hi.

Use WORDPAD.EXE

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

I don't have a problem viewing the file. I have word viewer on the machine and can see the file. Problem is the pic does not get into the word document.

I just didn't know if I need to have word installed for the word object to get created. Is that the case?

Also if that isn't the case is there another reason the code above does not insert the pic into the word document?

Thanks again.

Share this post


Link to post
Share on other sites

Yes, Word needs to be installed.

Also, even if it were, there are several errors in your code.

You've taken the help file example and broken it.

OpenOffice can be scripted, but no, not with the Word UDF.

Share this post


Link to post
Share on other sites

ResNullius,

thanks for the help. I was sort of afraid of that. I just wasn't sure if I had broke the script and word would be required to create the doc or not.

When you say word can't be scripted does that mean you must actually open word to place the pic in there?

What I want to do is take pics I already have a .jpg or .bmp file and insert them at certain places in a word document. What I have been doing is creating a .doc file and trying to use that. Autoit is definitely able to insert text into the file but I also want to add pics.

Is this possible? What is the best approach?

Was just trying this (which fails)

CODE
#include <Word.au3>

if _WordCreate ("test.doc", 1) Then

MsgBox(0,"","create passed")

Else

MsgBox(0,"","create failed")

EndIf

$oWordApp = _WordCreate ("test.doc", 1)

; Check @extended return value to see if attach was successful

If @extended Then

MsgBox(0, "", "Attached to Existing Window")

Else

MsgBox(0, "", "Created New Window")

EndIf

Share this post


Link to post
Share on other sites

ResNullius,

thanks for the help. I was sort of afraid of that. I just wasn't sure if I had broke the script and word would be required to create the doc or not.

When you say word can't be scripted does that mean you must actually open word to place the pic in there?

What I want to do is take pics I already have a .jpg or .bmp file and insert them at certain places in a word document. What I have been doing is creating a .doc file and trying to use that. Autoit is definitely able to insert text into the file but I also want to add pics.

Is this possible? What is the best approach?

Was just trying this (which fails)

CODE
#include <Word.au3>

if _WordCreate ("test.doc", 1) Then

MsgBox(0,"","create passed")

Else

MsgBox(0,"","create failed")

EndIf

$oWordApp = _WordCreate ("test.doc", 1)

; Check @extended return value to see if attach was successful

If @extended Then

MsgBox(0, "", "Attached to Existing Window")

Else

MsgBox(0, "", "Created New Window")

EndIf

I didn't say "Word can't be scripted", I said OpenOffice can't be scripted using the Word UDF (word.au3).

I take it if you can insert text with AutoIt, that you have Word installed on your computer?

If so, did you try the _WordDocAddPicture() sample from the help file as is, without modifying it?

It works.

Share this post


Link to post
Share on other sites

I am using Filewriteline to add text so I didn't have any problems inserting text with that.

I will see if I can find a different machine with word to try this out on.

Thanks again.

Share this post


Link to post
Share on other sites

Hi,

#include <Word.au3>
$oWordApp = _WordCreate(@ScriptDir & "/qqqw3200.doc")
$oDoc = _WordDocGetCollection($oWordApp, 0)

$filqq1 = "C:\Downloads\Fotos\Auto\DSC00763.JPG"
$filqq2 = "C:\Downloads\Fotos\Auto\DSC00762.JPG"
$filqq3 = "C:\Downloads\Fotos\Auto\DSC00761.JPG"

;~ Global $range = $oWordApp.Activedocument.Paragraphs(1).Range
;~ Global $range = $oWordApp.Activedocument.Range(0, 10)

$oShape = _WordDocAddPicture($oDoc, $filqq1, 0, 1)
_WordAppendText($oDoc, @CRLF & "Bild 1" & @CRLF & @CRLF)
ConsoleWrite('Bild 1')

$oShape = _WordDocAddPicture($oDoc, $filqq2, 0, 2)
;~ $oWordApp.Activedocument.Sections.Range.InsertAfter('Hello')
_WordAppendText($oDoc, @CRLF & "Bild 2" & @CRLF & @CRLF)
_WordAddSection($oWordApp)
ConsoleWrite('Bild 2')

$oShape = _WordDocAddPicture($oDoc, $filqq3, 0, 3)
 _WordAppendText($oDoc, @CRLF & "Bild 3" & @CRLF & @CRLF)
ConsoleWrite('Bild 3')

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

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Thanks for the help. I was able to run that and see it working.

Just so I am clear it looks like the way this works is to actually start word and paste the pic into the document.

Is there a way to just insert the pic like a file stream? I am able to insert text without starting word. Can I insert pics without starting the word application?

Share this post


Link to post
Share on other sites

I am using Filewriteline to add text so I didn't have any problems inserting text with that.

Thanks for the help. I was able to run that and see it working.

Just so I am clear it looks like the way this works is to actually start word and paste the pic into the document.

Is there a way to just insert the pic like a file stream? I am able to insert text without starting word. Can I insert pics without starting the word application?

Are you seriously using FileWriteLine() on a Word document?.

Or are you just using it to create a new file with a ".doc" extension?

If the latter, you are just creating a plain text document that Word will auto open because of the file extension.

If the former, then I don't see how your text can possibly be showing up when you re-open the document in Word or Word viewer.

Word documents (even the new 2007 .docx format) are structured with specific locations in the file for the text body, headers & footers, and other info, and you can't just throw text on the end and expect it to show up.

If you're just creating a plain text document with a .doc extension, then I can't see how you could possibly stick a picture in there that Word will recognize (unless it's ascii art) .

Share this post


Link to post
Share on other sites

Yes, that is what I am doing and when you open the doc it converts to a real word document.

I can change the text insert to use the Autoit Word library but wanted I wanted to know was, does the library always actually start the word application?

Or is there a way to stream the text and images into the document like you would witht eh writeline function?

Thanks again

Share this post


Link to post
Share on other sites

Yes, that is what I am doing and when you open the doc it converts to a real word document.

I can change the text insert to use the Autoit Word library but wanted I wanted to know was, does the library always actually start the word application?

Or is there a way to stream the text and images into the document like you would witht eh writeline function?

Thanks again

Hi,

I dunno, but I guess it starts the application if you use the word API to do the modify.

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Thanks for the help.

I have played with this some and think it will be ok for what I want to do if the application opens and then the text and pic is added.

The problem I am running into now is the pic does not actually get created. I have a file named test.jpg in the same folder as where this script is being executed. The document is created and the text added but the pic is never added. Is there something else I need to do? Did I break the script from what it was before?

CODE
#include <Word.au3>

$oWordApp = _WordCreate(@ScriptDir & "/test.doc")

$oDoc = _WordDocGetCollection($oWordApp, 0)

$filqq1 = "test.jpg"

;~ Global $range = $oWordApp.Activedocument.Paragraphs(1).Range

;~ Global $range = $oWordApp.Activedocument.Range(0, 10)

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

_WordAppendText($oDoc, @CRLF & "Write Some text" & @CRLF & @CRLF)

ConsoleWrite('Bild 1')

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

Share this post


Link to post
Share on other sites

Thanks for the help.

I have played with this some and think it will be ok for what I want to do if the application opens and then the text and pic is added.

The problem I am running into now is the pic does not actually get created. I have a file named test.jpg in the same folder as where this script is being executed. The document is created and the text added but the pic is never added. Is there something else I need to do? Did I break the script from what it was before?

From the help file:

$s_FilePath The path and file name of the picture.

$filqq1 =  "test.jpg"

Should be

$filqq1 = @SCRIPTDIR & "\test.jpg"

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