使用VBA在Excel中打开Microsoft Word文档

时间:2016-07-18 17:52:29

标签: excel vba excel-vba ms-word

我试图在Excel中打开Word文档,以便将大量信息转储到其中。我使用以下代码:

   'Declare a variable as a FileDialog object and create a FileDialog object as a File Picker dialog box
    Dim iFileSelect As FileDialog
    Set iFileSelect = Application.FileDialog(msoFileDialogFilePicker)

    'Declare a variable to contain the path of each selected item
    Dim vrtSelectedItem As Variant


    'Use the Show method to display the File Picker dialog box
    'The user pressed the action button
     If iFileSelect.Show = -1 Then

     For Each vrtSelectedItem In iFileSelect.SelectedItems
     'vrtSelectedItem contains  path of each selected item
     MsgBox "The path is: " & vrtSelectedItem
     Next vrtSelectedItem

     End If

    'Set object variable to Nothing
    Set iFileSelect = Nothing

我有以下问题:

  1. 我不知道该文件是否实际开启。
  2. 如果我将信息转储到文件中,如何关闭并保存文件?

1 个答案:

答案 0 :(得分:0)

如果您在Excel中打开了工作簿,则可以使用其FollowHyperlink方法打开文档。

Sub OpenDoc()
    Dim iFileSelect As FileDialog
    Set iFileSelect = Application.FileDialog(msoFileDialogFilePicker)

    Dim vrtSelectedItem As Variant

    If iFileSelect.Show = -1 Then
        For Each vrtSelectedItem In iFileSelect.SelectedItems
            ActiveWorkbook.FollowHyperlink vrtSelectedItem
        Next vrtSelectedItem
    End If

    Set iFileSelect = Nothing
End Sub