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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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

    • Skeletor
      By Skeletor
      Hi Guys,
      Can anyone point me in the right direction. I'm trying to accomplished conditional formatting with arrows, but the code I have is wrong... 
      .Range("=$A3:$A4000").FormatConditions.Add = (xlCellValue, xlGreater, "=$A:$A") .IconSet(xl3Arrows) ;ActiveWorkbook.iconsets I also tried it like this:
      .Range("=$A3:$A4000").FormatConditions _ .Add(xlCellValue, xlGreater, "=$A:$A") .IconSet(xl3Arrows) ;ActiveWorkbook.iconsets  
    • Skeletor
      By Skeletor
      Hi Guys,
      How would you use _Excel_FilterSet to filter the excel sheet from largest number to smallest number?
      _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">20", 1, "<40") ; OR _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">", 1, "<") Excel Sheet attached. Trying to filter on the last column "I2"
      Inventory.xlsx
    • Skeletor
      By Skeletor
      Hi All,
      Has anyone come across this before?
      Code below:
      $SheetList = _Excel_SheetList($oWorkbook) _FileWriteFromArray("C:\" & $SheetListOutput, $SheetList, 1) Result:
      ResultABC| ResultDEF| ResultGHI| ResultJKL| ResultMNO| It created these "|" vertical bars?
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
×