TheDcoder Posted October 7, 2016 Share Posted October 7, 2016 (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: 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 October 7, 2016 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 More sharing options...
Jfish Posted October 7, 2016 Share Posted October 7, 2016 Are you saving the application object when it should be a doc object? TheDcoder 1 Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
TheDcoder Posted October 7, 2016 Author Share Posted October 7, 2016 @Jfish Oh! , Thanks for pointing it out , 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 More sharing options...
Jfish Posted October 7, 2016 Share Posted October 7, 2016 (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 October 7, 2016 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 More sharing options...
TheDcoder Posted October 7, 2016 Author Share Posted October 7, 2016 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 More sharing options...
Jfish Posted October 7, 2016 Share Posted October 7, 2016 (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 October 7, 2016 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 More sharing options...
TheDcoder Posted October 7, 2016 Author Share Posted October 7, 2016 @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 More sharing options...
Jfish Posted October 7, 2016 Share Posted October 7, 2016 Agree - I modified my post but we crossed posts ... Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
TheDcoder Posted October 7, 2016 Author Share Posted October 7, 2016 Snap, Oops happen 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 More sharing options...
Jfish Posted October 7, 2016 Share Posted October 7, 2016 Works when I use a path parameter on the save ... maybe the default parameter is not working? TheDcoder 1 Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
TheDcoder Posted October 7, 2016 Author Share Posted October 7, 2016 Oh yes! It works @Jfish! I am reporting this in the Bugtracker Jfish 1 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 More sharing options...
TheDcoder Posted October 7, 2016 Author Share Posted October 7, 2016 Link to the Bug's ticket for reference: https://www.autoitscript.com/trac/autoit/ticket/3325 Jfish 1 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 More sharing options...
Jfish Posted October 7, 2016 Share Posted October 7, 2016 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 More sharing options...
water Posted October 8, 2016 Share Posted October 8, 2016 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 More sharing options...
Jfish Posted October 8, 2016 Share Posted October 8, 2016 @water you are much more knowledgeable but just wondering if the code I posted would be a fix since it basically uses the default name when a name is not provided? If $sFilename = Default Then $sFilename = $oDoc.FullName&".doc" Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
water Posted October 8, 2016 Share Posted October 8, 2016 (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 October 8, 2016 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 More sharing options...
BrewManNH Posted October 8, 2016 Share Posted October 8, 2016 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 GudeHow 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 More sharing options...
TheDcoder Posted October 8, 2016 Author Share Posted October 8, 2016 @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 More sharing options...
Madness Posted December 7, 2016 Share Posted December 7, 2016 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 More sharing options...
water Posted March 19, 2019 Share Posted March 19, 2019 (edited) 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 expandcollapse popup#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 March 19, 2019 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now