Jump to content

What is not working in this script?


Adams
 Share

Go to solution Solved by water,

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

  • Solution

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

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

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