Microsoft Word vba宏插入带有文件链接的文件

时间:2016-08-01 13:39:38

标签: vba ms-word

我不是一个优秀的程序员,所以欢迎所有的帮助。

我想创建一个简单的脚本。当我插入图像时,我想自动添加选项链接到文件。

我创建了一个脚本,但问题是我需要在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

1 个答案:

答案 0 :(得分: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