I have a PowerPoint macro that should open an Excel file.
Public Sub SortList()
Dim MyFile as String
Dim xlApp as Object
Dim xlWorkBook as Object
Set xlApp = CreateObject("Excel.Application")
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show
MyFile = SelectedItems(1)
End With
xlApp.Visible = True
xlApp.Workbooks.Open MyFile
Set xlWorkBook = xlApp.Workbooks.Open(MyFile)
This code was previously working with no errors. However, I have started receiving the error message
"Run-time error '-2147467259 (80004005)': Method 'Open' of object 'Workbooks' failed."
The error occurs when trying to run the "Set" line of code.
The issue is that I can see that the code is successfully opening the Excel file!
Things I have tried (in about every possible combination):
-Changing the code to this (I thought maybe the program was trying to open the file twice):
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open(MyFile)
-Adding ReadOnly:= True to both or either .Open command, at one point this gave me an "Automation error- unspecified error" message which I have never seen before...
-Changing the Set line to this (and variations thereof):
xlApp.Workbooks.Open MyFile
Set xlWorkBook = xlApp.Workbooks(Dir(MyFile))
-Ensuring Excel is completely closed prior to running the code
The frustrating aspect is that this code worked perfectly last week, so I'm also at a loss as to why it would suddenly stop working. Any assistance would be greatly appreciated.
Workbooks.Open can cause issues if you call it on an already open workbook.
So, don't open twice. Remove the following line:
xlApp.Workbooks.Open MyFile
Related
I'm using VB to open an excel file and I'm having trouble with encoding. Some characters are displayed as garbage. I discovered that when I open the file with excel then reopen it with utf8 encoding the characters display correctly. In my VB program when I try to use the reloadas method the reload appears to work but receive the error "object disconnected from it's clients" error message.
Is there a way I can avoid using the reloadas and open the file using utf8? If not how can resolve the error I'm getting?
Dim xlWorkbook As Excel.Workbook = xlapp.Workbooks.Open(SaveFN)
xlWorkbook.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8
xlWorkbook.ReloadAs(Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8)
Dim xlWorkSheet As Excel.Worksheet = xlWorkbook.Worksheets(1) 'error occurs here
thanks.
when I try to use the reloadas method the reload appears to work but
receive the error "object disconnected from it's clients" error
message.
This is because when the Excel reloads the Workbook, the existing Workbook object is destroyed and the reference held by xlWorkbook is no longer valid.
Based on the usage of WebOptions, the file is probably an Html file. However, Excel does not expose an open method that takes a file format argument. This leaves using Application.ActiveWorkBook or Application.WorkBooks collection as the only means to retrieve a reference to retrieve the Workbook.
Dim xlWorkbook As Excel.Workbook = xlapp.Workbooks.Open(SaveFN)
xlWorkbook.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8
xlWorkbook.ReloadAs(Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8)
xlWorkbook = xlapp.ActiveWorkbook ' *** add this statement
Dim xlWorkSheet As Excel.Worksheet = xlWorkbook.Worksheets(1)
I have a downloaded CSV file that I am trying to open and then save as an .xlsx file using code. I am very very new to VB, and have just modified code that I have found to make things work. Yesterday the code worked great, but today it stalls out on the "wb.SaveAs" line. The odd thing about the error is that the file actually saved to the folder. Any help would be appreciated.
Public Function Upload_New_Orders_Excel()
'the path to the excel workbook
Dim XcelFile As Excel.Application
Dim wb As Excel.Workbook
Dim strExcelPath As String
Set XcelFile = New Excel.Application
Set wb = XcelFile.Workbooks.Open("C:\Users\mpdav\downloads\orders_export_1.csv")
wb.SaveAs FileName:="C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx", FileFormat:=51
wb.Close
Set XcelFile = Nothing
Kill "C:\Users\mpdav\downloads\orders_export_1.csv"
strExcelPath = "C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx"
'import data from excel
Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel8, "temp_tbl_orders_export", "C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx", True, "A1:BP1000")
Kill "C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx"
End Function
edit2: I think I identified the problem: Another user had the file open, but when I manually opened it he must have had it closed. Now I wonder how to avoid this issue because I don't the macro to fail, much less so with some cryptic error message. I would be happy with read-only, but apparently it doesn't give me that option. Do I have to open every file read-only by default to not run into this issue again?
I've been working on some code and all of a sudden, a very basic line gets me an error message: "Run time error -2147467529 (80004005): Method 'Open' of object 'Presentations' failed. I recreated the problem in this piece of code:
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Sub MReport()
Dim DestinationTemplate As String, DestinationAmp As String
Dim PPAmp As PowerPoint.Presentation
Dim AmpName As String, PathAmp As String
DestinationTemplate = "G:\MReport\MReportTemplate.pptm"
PathAmp = "G:\MReport\Amp\MReportAmp\"
AmpName = Dir(PathAmp & "Amp*long*.*")
DestinationAmp = PathAmp & AmpName
Debug.Print DestinationAmp
Set PowerPointApp = New PowerPoint.Application
Set myPresentation = PowerPointApp.Presentations.Open(DestinationTemplate) 'works
Application.DisplayAlerts = False 'doesn't help
Set PPAmp = PowerPointApp.Presentations.Open(DestinationAmp) '<-- error
Application.DisplayAlerts = True
End Sub
The error occurs in the line Set PPAmp = PowerPointApp.Presentations.Open(DestinationAmp). It worked literally dozens of times before. Then I rearranged some code unrelated to this PowerPoint file and shifted it into separate procedures and the error happened. The error now happens in the new, re-arranged file and in the old one, which successfully ran for days.
I tried restarting my computer, MS PowerPoint 16.0 Object Library is also checked. The Debug.Print statement gives me the correct path, I can copy it into the file explorer and the right presentation opens.
The code is written in Excel, but when I copied it into PowerPoint I got the same error. I also tried declaring all the PowerPoint-related variables (PowerPointApp, myPresentation, PPAmp) as Object and then use Set PowerPointApp = CreateObject("PowerPoint.Application") without success.
edit: Typing PowerPointApp.Presentations.Open("Filepath") gives me the same error (I think): "Run time error '-2147467259 (80004005)': Automation error. Unknown error."
I am getting error Run-time error 3125 'Import$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long when I am attempting to import an Excel file to an Access Table. I only receive this error when other Excel files are open otherwise the Excel file is loaded to the table as expected. Here is some of the code I am working with:
Dim f As FileDialog, str As String
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.Show
On Error GoTo Err1
str = f.SelectedItems(1)
Dim xl As Excel.Application
Set xl = New Excel.Application
xl.Visible = True
Dim xlWB As Excel.Workbook
Set xlWB = xl.Workbooks.Open(str)
'manipulate Excel file
'load spreadsheet
On Error GoTo Err2
DoCmd.TransferSpreadsheet acImport, 8, "JobCode", str, True, "Import!"
I originally thought and tried to change the filename, but was unsuccessful. It seems like the code is opening a new version of the import Excel file as Read-Only which might be confusing the Access database on which worksheet to import. But I am not sure why it's doing this and also why it only does this when another Excel file is open. Is this the problem? Does anyone know how to fix this?
Thank you!
This VBScript errs as "unknown runtime error", on the VBScript line that calls the application object's RUN method, which I'm quite sure I'm using correctly, syntactically.
With only one workbook open under the application object, I probably don't even need to prefix it with the Workbookname.
It appears the previous line (which sets the wb variable to the application object's Workbooks.Open method, does not open. Nor does it err, nor does it "eventually" open. The path is correct, the filename is correct, there is no On Error Resume Next in my VBScript.
How can the workbook not open and not error?
I hope I am wrong in thinking there is a "wait/pause" issue. Acting directly on the Excel object model, it seems like the VBScript code waiting until the excelapp object has opened the workbook should be a given?
dim excelapp, wb
set excelapp = createobject("excel.application")
excelapp.enableevents=true
excelapp.visible=true
set wb = excelapp.workbooks.open("C:\Users\John\Desktop\Scheduled Jobs from Isaac\Availability.xlsb")
'MSGBOX "SHOULD BE OPEN"
excelapp.windowstate = -4137 'value for constant xlMaximized acccording to msdn
excelapp.caption = "Running AVAILABILITY - Please WAIT..."
excelapp.run "Availability.xlsb!ChangeAndCopyFile"
final vbscript code i used, for better or worse:
dim excelapp, wb
'set excelapp = createobject("excel.application")
'excelapp.enableevents=true
'excelapp.visible=true
'set wb = excelapp.workbooks.open("C:\Users\John\Desktop\ScheduledJobsfromIsaac\Availability.xlsb")
'although I don't know why the above line is silently failing, i'm going to use an alternate method I read on S.O.:
set wb = GetObject("C:\Users\John\Desktop\ScheduledJobsfromIsaac\Availability.xlsb")
set excelapp = getobject(,"excel.application")
excelapp.enableevents=true
excelapp.visible=true
excelapp.windowstate = -4137 'value for constant xlMaximized acccording to msdn
excelapp.caption = "Running AVAILABILITY - Please WAIT..."
excelapp.run "Availability.xlsb!ChangeAndCopyFile"
wb.close(true)
excelapp.caption = "10 seconds to close - please WAIT..."
wscript.sleep 10000
excelapp.displayalerts=false
excelapp.quit
probably, it helps if you can describe where and how you execute your vbscript above? is this coded in a .vbs file and execute with wscript.exe or cscript.exe, or embedded in a HTML or HTA?
If this is scripted in HTML, what browser are you using to view that? Please take note that almost all modern browsers will just ignore the vbscript silently.