2016-08-01 115 views
0

我不是一個好的程序員,所以所有的幫助是值得歡迎的。微軟Word vba宏插入文件鏈接到一個文件

我想創建一個簡單的腳本。當我插入圖像時,我想自動添加鏈接到文件的選項。

我創建了一個腳本,但問題是我需要在Word插入它之前選擇文件兩次(它實際上只添加一個文件)。 任何想法我做錯了什麼?

Sub InsertLinkToFile() 
' 
' InsertLinkToFile Macro 
' 
' 
    Dim strPicName As String 
    Dim vShape As InlineShape 

    With Application.FileDialog(msoFileDialogFilePicker) 'Dialogs(wdDialogInsertPicture) 
    .AllowMultiSelect = False 
    .Title = "Select the File that you want to insert" 
    .Show 
    .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.png", 1 
    FiletoInsert = .SelectedItems(1) 

     If .Show = -1 Then 
      strPicName = .SelectedItems(1) 
      With ActiveDocument 

       Set vShape = .InlineShapes.AddPicture(FileName:=strPicName, LinkToFile:=True, SaveWithDocument:=False) 

      End With 
     End If 
    End With 

End Sub 
+0

你有兩行以'.Show' - 剛落,第一個。 –

回答

0

修改後的腳本:

Sub InsertLinkToFile() 
    ' 
    ' InsertLinkToFile Macro 
    ' 
    ' 
     Dim strPicName As String 
     Dim vShape As InlineShape 

    With Application.FileDialog(msoFileDialogFilePicker) 'Dialogs(wdDialogInsertPicture) 
     .AllowMultiSelect = False 
     .Title = "Select the File that you want to insert" 
     ' .Show ' remove this line 
     .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.png", 1 

    If .Show = True Then 
     FiletoInsert = .SelectedItems(1) 
     strPicName = .SelectedItems(1) 
     Set vShape = ActiveDocument.InlineShapes.AddPicture(strPicName, True, False)    
    End If 
    End With 
End Sub