Jump to content
Sign in to follow this  
Adams

What is not working in this script?

Recommended Posts

Adams

I assembled this script combining the various examples found in the help. The operation should be as follows:

1) start the instance of Excel
2) check if the spreadsheet exists in the specified path
2.1) I open the spreadsheet if it exist
2.2) if the spreadsheet does not exist I create it
3) I am writing a text string in cell A1
4.1) I "save" the spreadsheet if it exists
4.2) if the spreadsheet does not exist I "save as" it
5) I close the instance of Excel

Excel must be opened in invisible mode

Running the script in the absence of the spreadsheet "Excel Test.xlsx" is followed the road 1, 2, 2.2, 3, 4.2, 5 and the spreadsheet "Excel Test.xlsx" is created with the text string "Test" in the cell "A1" of "Sheet  1".
Running the script in the presence of the spreadsheet "Excel Test.xlsx" is followed the road 1, 2, 2.1, 3, 4.1, 5 but the result I get is that the spreadsheet does not contain sheet.

Where I'm wrong? I forgot something?
Thanks for any help or suggestions.

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

; *****************************************************************************
; Create application object or connect to an already running Excel instance
; *****************************************************************************
$oAppl = _Excel_Open(False)
If @error Then Exit MsgBox(16, "Excel UDF: Test", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

$sFilePath = @ScriptDir & "\Excel test.xlsx"
$iFileExists = FileExists($sFilePath)

If $iFileExists Then
    MsgBox($MB_SYSTEMMODAL, "", "The file exists." & @CRLF & "FileExist returned: " & $iFileExists)
    ; *****************************************************************************
    ; Open an existing workbook and return its object identifier.
    ; *****************************************************************************
    $sWorkbook = $sFilePath
    $oWorkbook = _Excel_BookOpen($oAppl, $sWorkbook, Default, False, True) ;Default, True)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Error opening '" & $sWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Workbook '" & $sWorkbook & "' has been opened successfully." & @CRLF & @CRLF & "Creation Date: " & $oWorkbook.BuiltinDocumentProperties("Creation Date").Value)
Else
    MsgBox($MB_SYSTEMMODAL, "", "The file doesn't exist." & @CRLF & "FileExist returned: " & $iFileExists)
    ;*****************************************************************************
    ; Create a new workbook
    ;*****************************************************************************
    $oWorkbook = _Excel_BookNew($oAppl, 1)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Error creating the new workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
        _Excel_Close($oAppl)
        Exit
    EndIf
EndIf

; *****************************************************************************
; Write a string with a line break to the active sheet in the active workbook
; *****************************************************************************
_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "Test")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Error writing to worksheet." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "String successfully written.")

If $iFileExists Then
    ; *****************************************************************************
    ; Save the workbook
    ; *****************************************************************************
    _Excel_BookSave($oWorkbook)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Error saving workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Workbook has been successfully saved as '" & $sFilePath &"'.")
Else
    ; *****************************************************************************
    ; Save the workbook
    ; overwrite an existing version
    ; *****************************************************************************
    $sWorkbook = $sFilePath
    _Excel_BookSaveAs($oWorkbook, $sWorkbook, Default, True)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Error saving workbook to '" & $sWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Workbook has been successfully saved as '" & $sFilePath &"'.")
    ShellExecute($sWorkbook)
EndIf

; *****************************************************************************
; Close the Excel instance opened by _Excel_Open
; *****************************************************************************
_Excel_Close($oAppl)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Error closing the Excel application." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Sleep(2000)
$aProcesses = ProcessList("Excel.exe")
MsgBox($MB_SYSTEMMODAL, "Excel UDF: Test", "Function ended successfully." & @CRLF & @CRLF & $aProcesses[0][0] & " Excel instance(s) still running.")

Adams

Share this post


Link to post
Share on other sites
MikahS

Hmm, have a look at

_Excel_SheetAdd

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
water

How many sheets has Workbook @ScriptDir & "Excel test.xlsx" before you open it? What is the active worksheet when opened manually?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Adams

 

Hmm, have a look at

_Excel_SheetAdd

 

@MikahS

I'll look at the function you're saying, but the first run of the script ("Excel Test.xlsx" does not exist), the spreadsheet is regularly created with only a sheet, without need to use the function _Excel_SheetAdd

 

How many sheets has Workbook @ScriptDir & "Excel test.xlsx" before you open it? What is the active worksheet when opened manually?

 

@water

after the first run of the script, the spreadsheet is created with a single sheet (as desired) and, of course, if I open it manually the active sheet is the only existing one.


Adams

Share this post


Link to post
Share on other sites
Adams

 

Hmm, have a look at

_Excel_SheetAdd

MikahS,

I had a look at the function _Excel_SheetAdd and I do not think this is the solution. When spreadshet does not exist, the script works correctly by creating the spreadshet with a sheet. When the spreadsheet exists, I want to update the existing sheet, not add a new one.


Adams

Share this post


Link to post
Share on other sites
MikahS

Definitely, I thought that when you opened your existing, it did not have "sheet" as you said. I thought that meant you never created this "sheet 1".


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
water

The problem is being caused by the _Excel_BookOpen function call. You open the workbook as invisible and later save it as invisible.

Either manually set all workbooks in Excel to visible again or try this:

$oWorkbook = _Excel_BookOpen($oAppl, $sWorkbook)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Adams

Thanks a lot water. Solved.
The problem was just in the command _Excel_BookOpen, at line 44. The EXE obtained from the script must run as a service, then Excel should not be opened. But I have misinterpreted the parameter $bVisible of _Excel_BookOpen believing he must put to false to not open Excel while, to do this, it is sufficient to use the parameter $bVisible of _Excel_Open.
Thanks again


Adams

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
Sign in to follow this  

  • Similar Content

    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • nooneclose
      By nooneclose
      I need to perform a subtotal in excel and I would like to automate this process using Autoit if possible like always any and all help will be greatly appreciated. 
      I can not find a good example but the two from Microsoft. Here is one of the two from msdn.microsoft.com/en-us/vba/excel-vba/articles/range-subtotal-method-excel
      I do not really understand how to translate this into AutoIt, but I gave it a try and here is what I have.
      $OpenRange      = "A1:E200" $xlSum          = -4157 $Added_Array[2] = [2, 3] $OpenRange.Subtotal("B1", $xlSum, $Added_Array, True, False, True) I just need to perform a subtotal on a range based on a header called department, and then perform a sum on the results.
    • nooneclose
      By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
       
      any and all help would be greatly appreciated.  Thank you!
    • Daniza
      By Daniza
      It works fine on my Excel 2007, but after I Emailed My Area Manager he told me after he Enable Macro Security nothing happen's, can someone test this to run on Excel 2016? My AM can't provide me more information 

      Thank You in Advance,
      Please see attachment.
      <snip>
    • tuffgong
      By tuffgong
      Good morning. I have a system I am trying to automate that works like this: user fills a column in an Excel spreadsheet with values they would like printed and saves it to a folder on their desktop, they start the script and it formats their data into a text file (adding a prefix) and sends the text file as a .bch file where it needs to go. This is working:
      #include <Array.au3> #include <Excel.au3> #include <File.au3> #include <MsgBoxConstants.au3> Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) barcodePrint() Func barcodePrint() $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) FileOpen($sFilename, $FO_OVERWRITE) ;Global $oWorkbook = _Excel_BookAttach($oExcel) Global $sSTCArray = _Excel_RangeRead($Read) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, "!StaticShelving1x3_ZPL," & $sSTCArray[$i]) Next _Excel_Close($oExcel) FileMove($sFilename, "***file path***\print.bch") EndFunc However, it only works if the user first formats the spreadsheet to text. I want to automate that. From what I have read it appears AutoIt does not like formatting cells that already have values. True? Can I pull the values from an un-formatted (default GENERAL format) spreadsheet and go straight to my text file? I have also considered opening a second spreadsheet, formatting it, and copying the values over. Like this:
        
      Global $sPrefix Global $oPath Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) Global $aArray Global $bExcel _Excel_BookNew($bExcel) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $aArray = IniReadSection("***File path***\barcode.ini", "stc/rvt/mgm") $sPrefix = $aArray[1][1] $oPath = $aArray[2][1] barcodePrint() EndSwitch WEnd Func barcodePrint() $oWorkbook = _Excel_BookOpen($bExcel, @DesktopDir & "\Labels\print.xlsx") $oWorkbook.ActiveSheet.Columns("A").NumberFormat = "@" Local $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) $oCopy = _Excel_RangeRead($sWorkbook) _Excel_RangeWrite($oWorkbook, Default, $oCopy) FileOpen($sFilename, $FO_OVERWRITE) $sSTCArray = _Excel_RangeRead($Read,"Default","Default",3) ;_ArrayDisplay($sSTCArray) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, $sPrefix & $sSTCArray[$i]) Next _Excel_Close($oExcel) ;FileMove($sFilename, $oPath) Exit EndFunc This does not like the formatting of $oWorkbook: "Variable must be of type 'Object'".  Do I need this second sheet? If so, how can I format it? Is there a better way to get the Excel values into a .txt file? Any ideas would be appreciated. Thanks!
×