Jump to content

problem with _WordDocAddPicture function


Recommended Posts

Hello, everybody. I plan to insert an image into a new created word document by using the following script. The script runs perfectly at first, however, it always suddenly encounters a big error that causes the script to quit. The error information is

C:\Documents and Settings\Administrator\??\quest.au3 (85) : ==> ??????(?????).:

_WordDocAddPicture ($oDoc, @ScriptDir&"\1.jpg", 0, 1,$oWordApp.Activedocument.Paragraphs(Random(1,$oWordApp.Activedocument.Paragraphs.Count,1)).Range)

_WordDocAddPicture ($oDoc, @ScriptDir&"\1.jpg", 0, 1,$oWordApp.Activedocument.Paragraphs(Random(1,$oWordApp.Activedocument.Paragraphs.Count,1))^ ERROR

Can anyone give some suggestions to modify the manuscript? Many thanks in advance!!!

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Word.au3>
#include<IE.au3>
#Include <GuiListView.au3>


$Form1_1 = GUICreate("word add picture", 587, 572)
$listview1=GUICtrlCreateListView("path",50,50,400,400)
$buton1=GUICtrlCreateButton("add",450,450,50,50)
$buton2=GUICtrlCreateButton("start",520,520,50,50)
GUISetState()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $buton1
            open()
        Case $buton2
            start()
    EndSwitch
WEnd

Func open()

$file=FileOpenDialog("open","","(*.txt)",4)



If Not StringInStr($file,"|") Then 
    GUICtrlCreateListViewItem($file,$listview1)
Else
    $filearray=StringSplit($file,"|")

    ;_ArrayDisplay($filearray)
    Dim $path[$filearray[0]-1]
    For $i=0 To $filearray[0]-2
        $path[$i]=$filearray[1]&"\"&$filearray[$i+2]
    Next;...........................................多选文件时得到每个文件的完整路径

    ;_ArrayDisplay($path)
    For $i=0 To $filearray[0]-2
    ;MsgBox(0,0,$path[$i])

    GUICtrlCreateListViewItem($path[$i],$listview1)

    Next
EndIf
EndFunc




Func start()
    
If Not FileExists(@ScriptDir&"\afer") Then DirCreate(@ScriptDir&"\afer")
    
For $i=0 To _GUICtrlListView_GetItemCount($listview1)-1
    
    
    
    $oWordApp = _WordCreate(_GUICtrlListView_GetItemText($listview1,$i,0),1,0)
    $oDoc = _WordDocGetCollection ($oWordApp, 0)

    
        
        $filename=@ScriptDir&"\afer\"&getfilename(_GUICtrlListView_GetItemText($listview1,0))&".doc"
        
        $oWordApp = _WordCreate ($filename,0,0)

        $oDoc = _WordDocGetCollection ($oWordApp, 0)

        $oWordApp.ActiveDocument.Sections.first.Range.InsertAfter(FileRead(_GUICtrlListView_GetItemText($listview1,0)))
        
        _WordDocAddPicture ($oDoc, @ScriptDir&"\1.jpg", 0, 1,$oWordApp.ActiveDocument.Paragraphs(Random(1,$oWordApp.ActiveDocument.Paragraphs.Count,1)).Range)
        
        _WordDocSaveAs ($oDoc)
        _WordQuit($oWordApp)

        FileDelete(_GUICtrlListView_GetItemText($listview1,$i))

        _GUICtrlListView_DeleteItem($listview1,0)
        

Next
    MsgBox(0,"","finish")
EndFunc



Func getfilename($path)

    $splits=StringSplit($path,"\")

    $fullname=$splits[UBound($splits)-1]

    $filename  = StringLeft($fullname,StringLen($fullname)-4)

    Return $filename

EndFunc
Edited by eastern314
Link to comment
Share on other sites

Have you tried selecting/setting the range first and then calling AddPicture just referencing the doc object?

I don't often use the functions in the Word UDF.

I tend to park images in Word docs without the UDF using something similar to this:

$oWord = ObjCreate("Word.Application")
$oWord.Documents.Open($Temp_Path)
$oWord.ActiveDocument.Shapes.AddPicture($img, False, True, $xpos, $ypos)
$oWord.ActiveDocument.Save
$oWord.Application.Quit

I'd just get something basic working that sticks the image into your doc, and afterward work out placement/range issues.

Edited by Spiff59
Link to comment
Share on other sites

Have you tried selecting/setting the range first and then calling AddPicture just referencing the doc object?

I don't often use the functions in the Word UDF.

I tend to park images in Word docs without the UDF using something similar to this:

$oWord = ObjCreate("Word.Application")
$oWord.Documents.Open($Temp_Path)
$oWord.ActiveDocument.Shapes.AddPicture($img, False, True, $xpos, $ypos)
$oWord.ActiveDocument.Save
$oWord.Application.Quit

I'd just get something basic working that sticks the image into your doc, and afterward work out placement/range issues.

Thanks very much. I modify the script as below according to your suggestion and it works perfectly. I found that word.au3 is not such a good UDF that usually causes errors.

$oWord = ObjCreate("Word.Application")

$oWord.visible = False

$oWord.documents.add

$filename=@ScriptDir&"\after\"&getfilename(_GUICtrlListView_GetItemText($listview1,0))&".doc"

$oWord.Activedocument.Sections.first.Range.InsertAfter(FileRead(_GUICtrlListView_GetItemText($listview1,0)))

$paragraphs=Random(1,$oWord.Activedocument.Paragraphs.Count,1)

$range=$oWord.Activedocument.Paragraphs($paragraphs).Range

$oWord.Activedocument.InlineShapes.AddPicture(@ScriptDir&"\1.jpg",0,1,$range)

$oWord.Activedocument.SaveAs($filename)

$oWord.Application.Quit

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