autofaurus Posted June 23, 2017 Share Posted June 23, 2017 I am using an input box to allow myself or another to enter the name of the excel file they would like to use for the program in order to create a workbook object. My issue is that I can't get it to work with spaces. I have found multiple solutions for if the file path is written out, but i have the name of the file assigned to a variable. Below are the relevant lines: Global $filename = InputBox("Reference Filename", "What is the name of the file where your references are stored?") Global $excel = _Excel_Open() Global $oWorkbook = @MyDocumentsDir & "\" & $filename & ".xlsx" Global $workbook = _Excel_BookOpen($excel, $oWorkbook) Running this gives me error code 2 if $filename has a space in it. This would mean that the file path doesn't exist, but it definitely does. Also, please let me know if there is a better way to create a workbook object when the filename can be different each time. I am very new to AutoIt and not a very experienced programmer in general so any help is appreciated. Thank you. Link to comment Share on other sites More sharing options...
water Posted June 23, 2017 Share Posted June 23, 2017 Will check over the weekend My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Subz Posted June 23, 2017 Share Posted June 23, 2017 (edited) Works fine for me, try running the following in Scite to make sure the file path is being set correctly: #include <Excel.au3> Global $hSelect = InputBox("Reference Filename", "What is the name of the file where your references are stored?") Global $oExcel = _Excel_Open() Global $sWorkbook = @ScriptDir & "\" & $hSelect & ".xlsx" ConsoleWrite($sWorkbook & @CRLF) Global $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook) ConsoleWrite(@error & @CRLF) Edited June 23, 2017 by Subz Link to comment Share on other sites More sharing options...
water Posted June 23, 2017 Share Posted June 23, 2017 Did you check that there a no leading or trailing spaces in the user input? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
cbruce Posted June 25, 2017 Share Posted June 25, 2017 The problem could easily be what water described ... I don't have Excel, so can't test how _Excel_BookOpen($oExcel, $sWorkbook) evaluates the $sWorkbook string ... But normally, a full file name with spaces needs to be wrapped in quotes to be evaluated as a valid filename. So you would need: Global $oWorkbook = '"' & @MyDocumentsDir & "\" & $filename & ".xlsx" & '"' Link to comment Share on other sites More sharing options...
water Posted June 25, 2017 Share Posted June 25, 2017 The Excel UDF uses FileExists to check the file and returns @error = 2 if the file could not be found. So you could do this check in your script as well to make sure the file exists. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - 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