HI,
I am unable to launch an Excel file using the following code:
var excel As New ExcelApplication
Var book As ExcelWorkbook
var f as folderitem
var lPth as string
lPth = “D:\Desktop\Test\Test1.xlsm”
f = GetFolderItem(lPth)
if f.exists then
Try
book = excel.Workbooks.open(lPth)
book.Activate
Catch eErr As oleException
MsgBox"Error opening file: " + eErr.Message
End Try
f = nil
end if
Can anyone let me know what is wrong?
Thanks
Geoff
Hello Geoffrey,
Your code is close, and are missing a few declarations and such. Here is an example from my Xojo-Excel book at Excel 2019 with Xojo API2
Sub Pressed() Handles Pressed
Var excel As New ExcelApplication //Create a new object
Var book As ExcelWorkbook //Create a variable called book
Var f As FolderItem // Create a variable to select a file
f = New FolderItem("C:\test\TestExcelWorkbook.xlsx", FolderItem.PathModes.Native)
book = Excel.workbooks.open(f.NativePath) //Load the file into the Excel Workbook
excel.visible = True //Make it visible
excel = Nil //set all objects to Nil
book = Nil
f = Nil
Exception err As OLEException //Check for an error
MessageDialog.Show err.message //Display the error
End Sub
If you adjust the path to your file, does this work on your computer?
Eugene
HI Eugene,
Great using NativePath worked however I now get an
error trying to open a Password Protected Workbook ie.
var excel As New ExcelApplication
Var book As ExcelWorkbook
var f as folderitem
var lPswd as string
var lPth as variant
lPswd = “ok”
lPth = “D:\Desktop\Test\Test2.xlsm”
f = New FolderItem(lPth, FolderItem.PathModes.Native)
book = Excel.workbooks.open(f.NativePath,lPswd)
excel.visible = True
Apologies: the code should read:
book = Excel.workbooks.open(f.NativePath,lPswd)
The Reply is removing the commas: the code should read,
srting apostraphes added to the Reply.
“book = Excel.workbooks.open(f.NativePath,lPswd)”
HI Eugene, the Reply is still removing the commas between the Open Path and the Password. I am adding 4 commas between the Path and the Password text.
Thanks for your patience.
Geoff
Hello Geoff,
To help posting code in this forum in the future, wrap your code with three accents in the beginning and end. For example, here is the code from my book to open a password protected Excel sheet.
//opens a password protected Excel file
excel.Workbooks.Open("C:\test\TestSesameWorkbook.xlsx", Nil, Nil, 1, "Sesame", Nil, Nil, Office.xlWindows, Nil, Nil, Nil, Nil, Nil, Nil, Nil)
HI Eugene,
Thank you biggly for your excellent reply. Works perfectly. Will definitely buy both your books on Excel.
All the best
Geoff