我正在尝试通过对话框窗口在另一个应用程序中打开一个 excel 文件。
我在另一篇文章中找到了这段代码。但是我无法打开文件。
我认为问题在于这些代码行:If sFilePath = False Then
和 Set wb = excelApp.Workbooks.Open(sFilePath, False)
我也试图打开一个文件 .xlsx
Sub OpenFile()
' Create Excel object
' On Error Resume Next
Dim excelApp As Excel.Application
Set excelApp = GetObject(, "Excel.Application")
' Prompt user to select Excel file to read from
' Alternatively, you could hard code the filepath like this:
Dim sFilePath As String
sFilePath = excelApp.GetOpenFilename(FileFilter:="Excel Files (*.xls*),*.xls*", Title:="Select excel file")
' ' Open Excel file invisibly
If sFilePath = False Then
Exit Sub
Else
Set wb = excelApp.Workbooks.Open(sFilePath, False)
End If
End Sub
最佳答案
从其他应用程序使用 Excel
错误
Set xlApp = GetObject(, "Excel.Application")
或 Set xlApp = Excel.Application
将引用现有的 Excel 实例。如果 Excel 未打开(没有实例),则会发生错误。 .GetOpenFileName
将返回文件路径(字符串)或 bool 值 False
.如果使用字符串变量,则 bool 值 False
将转换为字符串 "False"
因此您可以使用字符串来测试对话框是否被取消。 第一个解决方案(早期绑定(bind))
Set xlApp = New Excel.Application
第二种解决方案(后期绑定(bind))
Set xlApp = CreateObject("Excel.Application")
一些细节
代码
Option Explicit
Sub OpenFileUsingExcelReference()
' Needs a reference to 'VBE>Tools>References>Microsoft Excel 16.0 Object Library'
Const ProcName As String = "OpenFile"
Dim xlApp As Excel.Application
Dim wb As Workbook
On Error GoTo ClearError
Const FileFilterString As String = "Excel Files (*.xls*),*.xls*"
Const TitleString As String = "Select Excel file"
' Reference a new invisible Excel application object.
Set xlApp = New Excel.Application ' CreateObject("Excel.Application")
xlApp.Visible = True ' when done developing, out-comment this line
' Prompt user to select Excel file to read from
Dim sFilePath As String
sFilePath = xlApp.GetOpenFilename( _
FileFilter:=FileFilterString, Title:=TitleString)
If sFilePath = "False" Then
Err.Raise 1004, , "You canceled the dialog."
'MsgBox "You canceled."
End If
Set wb = xlApp.Workbooks.Open(sFilePath, False)
' Continue, e.g.:
Debug.Print "The workbook '" & wb.Name & "' has " _
& wb.Sheets.Count & " sheet(s)."
SafeExit:
' Close the workbook and quit.
If Not wb Is Nothing Then wb.Close SaveChanges:=False
If Not xlApp Is Nothing Then xlApp.Quit
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume SafeExit
End Sub
Sub OpenFileWithoutExcelReference()
' No reference needed (*** indicates the differences)
Const ProcName As String = "OpenFile"
Dim xlApp As Object '***
Dim wb As Object '***
On Error GoTo ClearError
Const FileFilterString As String = "Excel Files (*.xls*),*.xls*"
Const TitleString As String = "Select Excel file"
' Reference a new invisible Excel application object.
Set xlApp = CreateObject("Excel.Application") '***
xlApp.Visible = True ' when done developing, out-comment this line
' Prompt user to select Excel file to read from
Dim sFilePath As String
sFilePath = xlApp.GetOpenFilename( _
FileFilter:=FileFilterString, Title:=TitleString)
If sFilePath = "False" Then
Err.Raise 1004, , "You canceled the dialog."
'MsgBox "You canceled."
End If
Set wb = xlApp.Workbooks.Open(sFilePath, False)
' Continue
Debug.Print "The workbook '" & wb.Name & "' has " _
& wb.Sheets.Count & " sheet(s)."
SafeExit:
' Close the workbook and quit.
If Not wb Is Nothing Then wb.Close SaveChanges:=False
If Not xlApp Is Nothing Then xlApp.Quit
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume SafeExit
End Sub
关于excel - 无法通过 getOpenFilename 打开 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71085720/