excel - 无法通过 getOpenFilename 打开 excel 文件

标签 excel vba

我正在尝试通过对话框窗口在另一个应用程序中打开一个 excel 文件。
我在另一篇文章中找到了这段代码。但是我无法打开文件。
我认为问题在于这些代码行:If sFilePath = False ThenSet 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))
  • 例如,如果您想从 Word 打开一个 Excel 文件,您可以创建对 Excel 库的引用并开发您的宏,就像您在 Excel 中一样,例如Excel 中的所有对象都将在 Word 的 IntelliSense 中可用。
  • 要使用早期绑定(bind)引用 Excel 应用程序对象的新实例,您将使用:
    Set xlApp = New Excel.Application
    

  • 第二种解决方案(后期绑定(bind))
  • 如果您不想创建对 Excel 库的引用和/或您想将文件发送给某人并且您不想打扰他/她有关如何创建引用的详细信息,您将使用迟到当您无法访问 Excel 对象的 IntelliSense 时进行绑定(bind)。
  • 要使用后期绑定(bind)引用 Excel 应用程序对象的新实例,您将使用:
    Set xlApp = CreateObject("Excel.Application")
    

  • 一些细节
  • 无论 Excel 是否已打开,这些代码都会创建一个新实例,您在完成后将要关闭该实例。
  • 要正确执行此操作,您必须考虑使用错误处理例程完成的可能发生的错误。
  • 所以无论发生什么,最终,新的 Excel 实例都需要关闭。

  • 代码
    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/

    相关文章:

    vba - 减少从 Excel 粘贴到 Word 的图表的文件大小

    excel - 为什么我的 VBA 代码无法选择 "Download"来下载此特定网站的 .xls 文件?

    regex - vba 正则表达式只返回第一个匹配项

    excel - 通过更改类名进行抓取

    excel - Excel 通过 Outlook 发送电子邮件时如何处理错误?

    mysql - 从 CSV 文件导入数据到 MySql 产生不正确的值

    c# - 如何使用 ClosedXML 在同一 Excel 单元格中使用不同颜色的文本?

    excel - SSRS 导出到 Excel 未格式化

    vba - 获取第一行匹配字符串的列号。 Excel VBA

    python - 自定义matplotlib图像显示添加copy/paste