Jump to content

Cannot save word document - Strange/Unknown error


Recommended Posts

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

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@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 :(

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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)

 

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

  • 1 month later...

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.

 

Link to comment
Share on other sites

  • 2 years later...

Just had a look at AutoIt's BugTracker and noticed a still open Word related ticket.
After playing for some time I found the following solution that works great for Word 2016.

Can you please test for older/newer versions of MS Word (Word 2003 or Word 2007)?
If it works I will fix the UDF :)

#include <Word.au3>

Global $oWord = _Word_Create() ; False, True)
Global $oDoc = _Word_DocAdd($oWord)
_Word_DocSaveAsEx($oDoc)
ConsoleWrite("@error = 0x" & Hex(@error ) & ", @extended = " & "0x" & Hex(@extended) & @CRLF)

; #FUNCTION# ====================================================================================================================
; Author ........: water (based on the Word UDF written by Bob Anthony)
; Modified ......:
; ===============================================================================================================================
Func _Word_DocSaveAsEx($oDoc, $sFileName = Default, $iFileFormat = Default, $bReadOnlyRecommended = Default, $bAddToRecentFiles = Default, $sPassword = Default, $sWritePassword = Default)
    ; Error handler, automatic cleanup at end of function
    Local $oError = ObjEvent("AutoIt.Error", "__Word_COMErrFuncEx")
    #forceref $oError

    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.SaveAs2($sFileName, $iFileFormat, False, $sPassword, $bAddToRecentFiles, $sWritePassword, $bReadOnlyRecommended) ; Try to save for >= Word 2010
    If @error = 0x80020006 Then $oDoc.SaveAs($sFileName, $iFileFormat, False, $sPassword, $bAddToRecentFiles, $sWritePassword, $bReadOnlyRecommended) ; COM error "Unknown Name" hence save for <= Word 2007
    If @error Then Return SetError(2, @error, 0)
    Return 1
EndFunc   ;==>_Word_DocSaveAsEx

; #INTERNAL_USE_ONLY#============================================================================================================
; Name...........: __Word_COMErrFunc
; Description ...: Dummy function for silently handling COM errors.
; Syntax.........:
; Parameters ....:
; Return values .:
;
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func __Word_COMErrFuncEx($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   ;==>__Word_COMErrFuncEx

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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