【问题标题】:Open File dialog box to get Excel打开文件对话框以获取 Excel
【发布时间】:2015-06-10 23:15:15
【问题描述】:

我编写了一些 Word VBA,它采用 Excel 文件并更新 Word 文件中的标签(ActiveX 控件)。唯一的问题是这个 Excel 文件每个月都会更改路径和文件名。我如何添加一个打开文件对话框以便用户选择要使用的 Excel 文件,而不是每个月编辑 2 个变量?

这是我现在拥有的:

Sub Update()
    Dim objExcel As New Excel.Application
    Dim exWb As Excel.Workbook

    PathWork = "C:\My Documents\2015-05 Report\"
    CalcFile = "May2015-data.xlsx"

    Set exWb=objExcel.Workbooks.Open(FileName:=PathWork & CalcFile)
    ThisDocument.date.Caption=exWb.Sheets("Data").Cells(1,1)
End Sub

【问题讨论】:

    标签: excel vba ms-word


    【解决方案1】:

    这是一个简化的宏,它允许用户仅选择启用宏的 Excel。我无法评论之前的答案,因为我没有获得足够的声誉来评论答案。请注意。

    Public Sub GetCaptionFromExcel()
        Dim objExcel As New Excel.Application, exWb As Workbook
        With Application.FileDialog(msoFileDialogFilePicker)
            .Title = "Select Macro-Enabled Excel Files"
            .Filters.Add "Macro-Enabled Excel Files", "*.xlsm", 1
            If .Show <> -1 Then Exit Sub
    
            Set exWb = objExcel.Workbooks.Open(.SelectedItems(1))
            '*** Use the values from excel here***
            MsgBox exWb.Sheets("Data").Cells(1, 1)
            '*** Close the opened Excel file
            exWb.Close
        End With
    End Sub
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的

      PathWorkCalcFile 替换为Dialogbox

      With Dialogs(wdDialogFileOpen)
          If .Display Then
              If .Name <> "" Then
                  Set exWb = Workbooks.Open(.Name)
                  sPath = exWb.Path
              End If
          Else
              MsgBox "No file selected"
          End If
      End With
      

      完整的 CODE 应该是这样的

      Option Explicit
      
      Sub Update()
          Dim objExcel As New Excel.Application
          Dim exWb As Excel.Workbook
          Dim sPath As String
      
          '// Dialog box here to select excel file
          With Dialogs(wdDialogFileOpen)
              If .Display Then
                  If .Name <> "" Then
                      Set exWb = Workbooks.Open(.Name)
                      sPath = exWb.Path
                  End If
                  Set exWb = objExcel.Workbooks.Open(FileName:=sPath)
                  ActiveDocument.Date.Caption = exWb.Sheets("Data").Cells(1, 1)
              Else
                  MsgBox "No file selected"
              End If
          End With
          Set objExcel = Nothing
          Set exWb = Nothing
      End Sub
      

      【讨论】:

      • 谢谢奥马尔!但是我如何将它与我展示的代码集成呢?这是我的主要 Word VBA 宏,除了有很多行 .Caption 链接到我的 Excel 对象 exWb
      • @wongnog 查看更新的答案,我还没有测试,但我会尽快测试。
      • 谢谢奥马尔,我想它快到了!您的代码需要一个 Word 文档,但当我尝试通过 Excel 文件时不喜欢(给出运行时错误“5792”:文件似乎已损坏)。你能帮我过滤一下启用宏的 Excel 文件 (.xlsm) 吗?谢谢!
      • 它让我现在可以打开任何文件类型,但现在我得到一个Run-time error '1004': "myfile.xlsm" could not be found. Check the spelling of the file name, and verify that the file location is correct. 我猜路径/文件名有语法错误?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多