TheDcoder

Cannot save word document - Strange/Unknown error

19 posts in this topic

#1 ·  Posted (edited)

Hello, I am trying to save an word document but it won't work :(, This is my REALLY simple script:

#include <Word.au3>

Global $oWord = _Word_Create(False, True)
_Word_DocSaveAs($oWord)
MsgBox(0, @error, @extended)

And my MsgBox:

vEonaJW.png

I know that its a COM error because @error is set 2... but what about the COM error code? I cannot find anything related to -2147352570!

Edited by TheDcoder
Appended Title

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites



#3 ·  Posted

@Jfish Oh! :o, Thanks for pointing it out :thumbsup:, Here is my modified code:

#include <Word.au3>

Global $oWord = _Word_Create(False, True)
Global $oDoc = _Word_DocAdd($oWord)
_Word_DocSaveAs($oDoc)
MsgBox(0, @error, @extended)

But... I am still getting the same error :(


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Try this as the second line after you create the app object

Local $oDoc = _Word_DocOpen($oWord, @TempDir & "\_Word_Test.doc")

Then try to save it ... 

P.S. can you get the sample in the help file to run?  That is where I got the above.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#5 ·  Posted

I doubt that there would be a "_Word_Test.doc" in @TempDir ready for me to open... I am guessing that you copy & pasted that from the example? ;)

 

By the way, I am trying to create an empty word document using the Word UDF.


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Yes.  I copied it.  You need to do something like that when you create the doc object or use a path parameter on the save so save knows where to put it .. (I think) 

Edit - that is not the case ... oops

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#7 ·  Posted

@Jfish It should create a Doc1.doc in the @ScriptDir, from the helpfile for the $sFilename parameter:

Quote
The full path name for the document (default = Current folder and current file name).
If the document has never been saved, the default name is used (for example, Doc1.doc)

 


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#9 ·  Posted

Snap, Oops happen :>


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#11 ·  Posted

Oh yes! It works @Jfish! :thumbsup:

I am reporting this in the Bugtracker

1 person likes this

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#12 ·  Posted

Link to the Bug's ticket for reference: https://www.autoitscript.com/trac/autoit/ticket/3325

1 person likes this

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#13 ·  Posted

I defer 100% to @water on the fix but this seems to work - especially when you have multiple docs open:

; #FUNCTION# ====================================================================================================================
; Author ........: water (based on the Word UDF written by Bob Anthony)
; Modified ......:
; ===============================================================================================================================
Func _Word_DocSaveAs($oDoc, $sFilename = Default, $iFileFormat = Default, $bReadOnlyRecommended = Default, $bAddToRecentFiles = Default, $sPassword = Default, $sWritePassword = Default)
    ;If $sFilename = Default Then $sFilename = "" 
    If $sFilename = Default Then $sFilename = $oDoc.FullName&".doc" ; this works
    If $iFileFormat = Default Then $iFileFormat = $WdFormatDocument
    If $bReadOnlyRecommended = Default Then $bReadOnlyRecommended = False
    If $bAddToRecentFiles = Default Then $bAddToRecentFiles = 0
    If $sPassword = Default Then $sPassword = ""
    If $sWritePassword = Default Then $sWritePassword = ""
    If Not IsObj($oDoc) Then Return SetError(1, 0, 0)
    $oDoc.SaveAs($sFilename, $iFileFormat, False, $sPassword, $bAddToRecentFiles, $sWritePassword, $bReadOnlyRecommended)
    If @error Then Return SetError(2, @error, 0)
    Return 1
EndFunc   ;==>_Word_DocSaveAs

 


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#14 ·  Posted

Seems to be a bug with MS Word. SaveAs does not use default values as describe in MSDN. I always get "Word did not save the document".
So, for the time being, you need to provide a folder and filename.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2016-12-04 - Version 1.2.2.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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

It is not a problem with the path but with the fileformat. Depending on the version of Word the default filformat is either .doc or .docx.
I suggest to provide the full path as parameter.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2016-12-04 - Version 1.2.2.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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#17 ·  Posted

I'm thinking that it's a problem in the fact that there's no document open, just an instance of Word opens, but not a document. Trying to save at that point doesn't work because there's no actual Word document to save, just an instance of Word.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#18 ·  Posted

@BrewManNH We are indeed adding an empty document, look at post #3:

Global $oDoc = _Word_DocAdd($oWord)

The above line attaches an empty/new document to the instance of Word.


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Thanks for starting this thread. It was very helpful to me. I was having a similar problem. It's very important that the format parameter matches the extension. I was trying to save out a docx file and getting the same error, and I had to call it like this... (just pay attention to the 3rd variable, which is a constant in "WordConstants.au3", which, on my system, is located here: C:\Program Files (x86)\AutoIt3\Include

_Word_DocSaveAs($oDocPart, $docxfileandpathpart, $WdFormatDocumentDefault)

I started with a Word document (docx) and I wanted to create a second document with part of it. I used 

Local $oDocPart = _Word_DocAdd($oWordApp)

to create the new blank document.

 

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

  • Similar Content

    • Duck
      Attempting to read each line of a word document (.doc, .docx) - Similarly to a FileReadLine()
      By Duck
      I'm attempting to read each line of a word document and assign the line to a variable. Similarly to how you can read a line from a text file (.txt or .csv) using FileReadLine(). So far i have been unsuccessful in reading from a .doc/.docx file, nor have i found any documentation that has helped.

      In searching for a solution i did find a function to convert the word doc to a text file, however my script is for (PCI) auditing purposes and i do not want to create a new file on the HDD.  I have also read through the _Word UDF help files... Unless im not understanding the _Word UDF correctly, I did not see anything that functions similarly to the FileReadLine function.
      Any help/advice is greatly appreciated!  
       
      Here is what i have been attempting to do(doesn't work): 
       
      #include <file.au3> #include <Array.au3> #include <LuhnCheck.au3> #include <Excel.au3> #include <Word.au3> Global $sPath = 'C:\Users\' Global $filePath Global $pii = @ScriptDir & '\pii_CreditCard.csv' Global $filesArray = _FileListToArrayRec($sPath , '*.txt;*.csv;*.doc;*.docx;*.xls;*.xlsx',1,1,0,2) For $i = 1 to $filesArray[0] ;Loop through file extensions and add files to the fileArray ;Assign the position in the filesArray to filePath (filePath is set to full path in FileListToArrayRec) $filePath = $filesArray[$i] readFile($filePath) Next Func readFile($file) If StringInStr($file, '.txt') Or StringInStr($file, '.csv') Then ; .txt file readTxtFile($file) ElseIf StringInStr($file, '.doc') Then ; .doc & .docx files ;============================================== part that does not work========================= Local $oWord = _Word_Create() ;$openFile = FileOpen($file, 0); While 1 Local $line = FileReadLine(_Word_DocOpen($oWord, $file, Default, Default, True)) If @error = -1 Then ExitLoop ;lookForCreditCardNumbers($line) MsgBox(0,0, $line) WEnd FileClose($openFile) ;============================================== part that does not work========================== EndIf EndFunc Func readTxtFile($fileToOpen) $openFile = FileOpen($fileToOpen, 0); open file for reading and assing it to the openFile variable While 1 Local $line = FileReadLine($openFile) If @error = -1 Then ExitLoop lookForCreditCardNumbers($line) WEnd FileClose($openFile) EndFunc Func lookForCreditCardNumbers($evaluateString) $aResult = StringRegExp($evaluateString, '[4|5|3|6][0-9]{15}|[4|5|3|6][0-9]{3}[-| ][0-9]{4}[-| ][0-9]{4}[-| ][0-9]{4}', $STR_REGEXPARRAYMATCH) If Not @error Then Local $newString1 = StringReplace($aResult[0], ' ', '') ;remove spaces Local $newString2 = StringReplace($newString1, '-', '') ;remove dashes Local $bool = _LuhnValidate($newString2) ; Check possible CC number against the Luhn algorithm If $bool = 'True' Then Local $piiCSV = FileOpen($pii, 1) ;open text file for appending/writing, 1 FileWriteLine($piiCSV, $filePath & ', ' & $newString2) FileClose($piiCSV) EndIf EndIf EndFunc  
    • MrCheese
      Merge word documents
      By MrCheese
      Hi Guys,
      Firstly, thanks for your  help in the past.
      I have a new activity I need to accomplish.
      In summary:
      * need to read a cell in excel (containing a file name)
      * open the file name in word (as its a word document
      *copy the word document
      *paste the word document into the master document
      *read next cell in excel
      ... and repeat until you reach the bottom of the column.
       
      I can read cells open workbooks etc.
      But as far as copying and pasting in word - where is the best place to start, and what functions should I be looking at. Or even if autoit is the right system to use?
      Thanks
       
       
       
    • kcvinu
      How to make a newline in Word with COM
      By kcvinu
      Hi all (Especially @water)
      I wonder how to do this task in word from autoit.
      Assume that i pasted some text into word with this code.
      Local $oWord = ObjGet("","Word.Application") Local $wRangeObj = _Word_DocRangeSet($oWord, 0) Local $data = ClipGet() $wRangeObj.Text = $data $wRangeObj.Font.Bold = True So far so good. Now, i need to enter a new line in word and turn the Font.Bold = False.  
      Currently, i did it with activating the word window and Send() function. But i would like to do it with the help of COM object. How can i do that ? 
    • aiter
      word com error trapping possible bug in udf
      By aiter
      I am testing intercepting com errors in Word and I have found a possible bug in the UDF, it might not be a bug it just means a workaround.
      I have found the com error bug in _Word_Create
      Func _Word_Create($bVisible = Default, $bForceNew = Default) Local $oAppl, $bApplCloseOnQuit = False If $bVisible = Default Then $bVisible = True If $bForceNew = Default Then $bForceNew = False If Not $bForceNew Then $oAppl = ObjGet("", "Word.Application") <------- here is the line which causees com error This happens in the following example
      #include <Word.au3> main() func main() local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $oWord = _Word_Create() $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Dear.docx", False, Default, True) $myrange = $oDoc.Range $myrange.Select sleep(3000) _Word_DocClose($oDoc) _Word_Quit($oWord) endfunc ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) ; Do anything here. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc This script causes a com error to occur and it has the following error
      test.au3 (53) : ==> COM Error intercepted ! err.number is: 0x80020006 err.windescription: Unknown name. err.description is: err.source is: err.helpfile is: err.helpcontext is: err.lastdllerror is: 0 err.scriptline is: 53 err.retcode is: 0x00000000 Line 53 is
      If Not $bForceNew Then $oAppl = ObjGet("", "Word.Application") as I mentioned above.
      When I force a new instance of Word, no error occurs eg
      #include <Word.au3> main() func main() local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $oWord = _Word_Create(True,True) $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Dear.docx", False, Default, True) $myrange = $oDoc.Range $myrange.Select sleep(3000) _Word_DocClose($oDoc) _Word_Quit($oWord) endfunc So it seems that if I want to open up  existing Word instance (use _Word_Create() ) I will have to create my own _Word_Open to bypass this problem.
      (If Word is already open the problem will not occur)
      Comment?
       
    • Ahile07
      Opening word in background
      By Ahile07
      Hello Guys,
      I'm trying to open a document with _WordDocOpen and replace some text in it with _Word_DocFindReplace and then print with _Word_DocPrint and finally close with _Word_DocClose and kill word with _Word_Quit. Works perfectly.
      My question is (can't find an answer anywhere): Can i do those commands in the background so i don't see word opening and replacing everything? Can't find anything in the proprieties of "WordDocOpen".
      Thank you.
      Flo