Jump to content

DirCreate & _Excel_BookSaveAs with Variable


 Share

Recommended Posts

Hi all. I'm close but I just can't figure out why its not saving the file. It's creating the directory correctly, but that is it.

#include <Excel.au3>
#include <MsgBoxConstants.au3>


While 1
   Sleep (50)

WEnd


Func MakeDirSaveFile()
      Local $sWorkbook = "c:\Excel\AsphaltEstimating.xlsm" ; Connect to AsphaltEstimating sheet
      Local $oExcel = _Excel_Open()
      $oWorkbook = _Excel_BookAttach($sWorkbook) ; Attach to Workbook
      $fName = ($oWorkbook.Worksheets(4).Range("J20").Value) ; Customers name
      $cAddress = ($oWorkbook.Worksheets(4).Range("I30").Value) ; customers address
      $dirName = $fName & " " & "(" & $oWorkbook.ActiveSheet.Name & ")" & " " & $cAddress
      $sFilePath = "\\MYCLOUDEX2ULTRA\Bids\2016 Bids\Marks Bids\" & $fName & " " & "(" & $oWorkbook.ActiveSheet.Name & ")" & " " & $cAddress
      If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Fail", "Error Try Again" & @CRLF & "@error = " & @error & ", @extended = " & @extended)
      DirCreate ("\\MYCLOUDEX2ULTRA\Bids\2016 Bids\Marks Bids\" & $fName & " " & "(" & $oWorkbook.ActiveSheet.Name & ")" & " " & $cAddress)
      Sleep (750)
      _Excel_BookSaveAs($oWorkbook, $sFilePath, $xlWorkbookDefault, True)

EndFunc

My save script by itself works just fine (Slow as it may be).

Func Savefile()
      Local $sWorkbook = "c:\Excel\AsphaltEstimating.xlsm" ; Connect to AsphaltEstimating sheet
      Local $oExcel = _Excel_Open()
      $oWorkbook = _Excel_BookAttach($sWorkbook) ; Attach to Workbook
      $fName = ($oWorkbook.Worksheets(4).Range("J20").Value) ; Customers name
      $cAddress = ($oWorkbook.Worksheets(4).Range("I30").Value) ; customers address
      $sFilePath = "\\MYCLOUDEX2ULTRA\Bids\2016 Bids\Marks Bids\" & $fName & " " & "(" & $oWorkbook.ActiveSheet.Name & ")" & " " & $cAddress
      If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Fail", "Error Try Again" & @CRLF & "@error = " & @error & ", @extended = " & @extended)
      _Excel_BookSaveAs($oWorkbook, $sFilePath, $xlWorkbookDefault, True)
      If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Fail!", "Error saving workbook to '" & $sFilePath & "'." & @CRLF & "@error = " & @error & ", @extended = "       & @extended)
      MsgBox($MB_SYSTEMMODAL, "Success!", "Workbook successfully saved as '" & $sFilePath & "'.")

EndFunc

 

Link to comment
Share on other sites

What's the value of @error and @extended after calling _Excel_BookSaveAs?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

This is the error I get and what the current code is getting the error

HotKeySet ("^!+{S}", "MakeDirSaveFile")     ; Saves the active Excel Asphalt Workbook to server

#include <Excel.au3>
#include <MsgBoxConstants.au3>

While 1
   Sleep (50)

WEnd

Func MakeDirSaveFile()
      Local $sWorkbook = "c:\Excel\AsphaltEstimating.xlsm" ; Connect to AsphaltEstimating sheet
      Local $oExcel = _Excel_Open()
      $oWorkbook = _Excel_BookAttach($sWorkbook) ; Attach to Workbook
      $fName = ($oWorkbook.Worksheets(4).Range("J20").Value) ; Customers name
      $cAddress = ($oWorkbook.Worksheets(4).Range("I30").Value) ; customers address
      $dirName = $fName & " " & "(" & $oWorkbook.ActiveSheet.Name & ")" & " " & $cAddress
      $sFilePath = "\\MYCLOUDEX2ULTRA\Bids\2017 Bids\Mark\" & $fName & " " & "(" & $oWorkbook.ActiveSheet.Name & ")" & " " & $cAddress
      If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Fail", "Error Try Again" & @CRLF & "@error = " & @error & ", @extended = " & @extended)
      DirCreate ("\\MYCLOUDEX2ULTRA\Bids\2017 Bids\Mark\" & $fName & " " & "(" & $oWorkbook.ActiveSheet.Name & ")" & " " & $cAddress)
      Sleep (750)
      _Excel_BookSaveAs($oWorkbook, $sFilePath, $xlWorkbookDefault, True)
      If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Fail!", "Error saving workbook to '" & $sFilePath & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
      MsgBox($MB_SYSTEMMODAL, "Success!", "Workbook successfully saved as '" & $sFilePath & "'.")

EndFunc

 

error.png

It does however correctly make the directory.

Edited by qwiqshot
Link to comment
Share on other sites

_Excel_BookSaveAs    @error #4    =    4 - File exists but could not be deleted

Try deleting the existing file by hand before running.

looks like having trouble overwriting existing file...

Link to comment
Share on other sites

_Excel_BooksSaveAs uses FileDelete to remove an existing file. But this is not always possible according to the help file: "Some file attributes can make the deletion impossible, if this is the case look at FileSetAttrib() to change the attributes of a file."

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Understood, but if you run it a second and third time ( as in testing ) the file from the last test should still be there, no?

And for some reason _Excel_BookSaveAs is giving you @error 4 which plainly states:    "File exists but could not be deleted"

Link to comment
Share on other sites

Correct. it Should still be there, but it never is/was. Every time I run it, I have waited different amounts of time to interact with the folder thinking maybe it's a windows thing. It has me baffled. Tho I'm still new to Autoit, so that's easy to do, lol

Edited by qwiqshot
Link to comment
Share on other sites

After trying a little and reading up on this I think the problem is that _ExcelBookSaveAs does not react like a normal Save As in Windows which normally means "save a new copy to a different place or under a different name" This seems to only save the file with a different extension.

If that's true the fix is relatively easy, after saving the excel file with new data and closing excel object, use FileMove or FileCopy to do the rest.

Link to comment
Share on other sites

After trying a little and reading up on this I think the problem is that _ExcelBookSaveAs does not react like a normal Save As in Windows which normally means "save a new copy to a different place or under a different name" This seems to only save the file with a different extension.

If that's true the fix is relatively easy, after saving the excel file with new data and closing excel object, use FileMove or FileCopy to do the rest.

Edit: this also explains the File Exists deleting problem, it was trying to delete the original file.

Edited by l3ill
Link to comment
Share on other sites

I think I'm on to something. I have it down to a COM error with this fix:

Local $sFilePath = "C:\Excel\Bids\2017Bids\Mark\"
    DirCreate($sFilePath)
    Local $sWorkbook = $sFilePath & "AsphaltEstimating.xlsm"
    ConsoleWrite($sWorkbook)

    _Excel_BookSaveAs($oWorkbook, $sWorkbook)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Fail!", "Error saving workbook to '" & $sFilePath & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    MsgBox($MB_SYSTEMMODAL, "Success!", "Workbook successfully saved as '" & $sFilePath & "'.")

play around with this and your structure.

Maybe water knows what this means:

capture_13102016_233252.jpg

Link to comment
Share on other sites

Could you try to save the Workbook to a local drive e.g. C:\temp?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

When saving xlsm files you need to provide parameter $iFormat as well if you do not use xls or xlsx.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

According to the help file and referenced in ExcelConstants.au3 I think it should be $xlOpenXMLWorkbookMacroEnabled,

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...