2016-07-18 162 views
0

我試圖在Excel中打開一個Word文檔以便將一堆信息轉儲到其中。我使用下面的代碼:使用VBA在Excel中打開Microsoft 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. 如何關閉和保存。文件一旦我有傾倒的信息成嗎?

回答

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 
+0

很酷。但是,你將如何編輯並從VBA保存相同的內容?發送密鑰......只是在開玩笑:) – cyboashu