在Word中查找链接的对象

时间:2014-10-23 19:57:31

标签: vba ms-word word-vba

我想在Microsoft Word 2010文件中找到链接到外部文件的对象。例如,如果作者有一个链接到Excel的图形,我想在文档中找到该图形。我可以逐步浏览VBA中的InlineShape集合并检查属性,但是没有找到方法告诉我这些形状在文档中的哪个位置。换句话说,我想知道,例如,形状所在的页码。我想让作者向我发送图片背后的电子表格(通常),并且通常希望将大多数这些形状转换为图片。我可以批量转换为VBA中的图片,但我宁愿一次一个,所以我可以检查结果。在大型文档中手工搜索是乏味的!

我编写了以下代码来显示链接图片的一些信息,并将最后链接的图片复制到剪贴板。从那里我可以将它粘贴到废料文档中,这样我就知道我在寻找什么,但这是一种粗暴的方式来完成这项工作。


Sub Links_Finder()
' Find things that are linked elsewhere
' October 20, 2014
Dim oShape As InlineShape, n As Integer, strMsg As String
strMsg = "Found this many linked things in the document: "
n = 0
On Error Resume Next
For Each oShape In ActiveDocument.InlineShapes
    oShape.Select
    If oShape.Type = wdInlineShapeChart Then
        n = n + 1
        Debug.Print n
        Debug.Print oShape.LinkFormat.SourceFullName
        Debug.Print oShape.LinkFormat.SourcePath
        Debug.Print oShape.LinkFormat.Type
        oShape.Select
        Selection.Copy
        strMsg = strMsg & n & vbCrLf
        strMsg = strMsg & "Copied the last one found to the clipboard"
        strMsg = strMsg & vbCrLf & oShape.LinkFormat.SourceFullName
        strMsg = strMsg & vbCrLf & oShape.LinkFormat.SourcePath & vbCrLf & " Link type: " & oShape.LinkFormat.Type
        ' To break the link and insert as a picture, use this:
        'Selection.Copy
        'Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdInLine, DisplayAsIcon:=False
    End If
Next
MsgBox strMsg
End Sub

1 个答案:

答案 0 :(得分:0)

事实证明我非常接近回答我自己的问题。正如L42建议的那样,oShape.Activate已经在我的代码中,确实将页面带到了相关的形状。我添加了一些代码来在消息框中显示源代码,然后问自己是否要停在那里或继续寻找。消息框出现在相关形状上方的文档中。我修改后的代码如下。

Sub Links_Finder()
' Find things that are linked elsewhere
' October 24, 2014
Dim oShape As InlineShape, n As Integer, strMsg As String
Dim Msg, Style, Title, Response, MyString
strMsg = "Found this many linked things in the document: "
n = 0
On Error Resume Next
For Each oShape In ActiveDocument.InlineShapes
    oShape.Select
    If oShape.Type = wdInlineShapeChart Then
        n = n + 1
        Debug.Print n
        Debug.Print oShape.LinkFormat.SourceFullName
        Debug.Print oShape.LinkFormat.SourcePath
        Debug.Print oShape.LinkFormat.Type
        oShape.Select
        Selection.Copy
        strMsg = strMsg & n & vbCrLf
        strMsg = strMsg & "Copied the last one found to the clipboard"
        strMsg = strMsg & vbCrLf & oShape.LinkFormat.SourceFullName
        strMsg = strMsg & vbCrLf & oShape.LinkFormat.SourcePath & vbCrLf & " Link type: " & oShape.LinkFormat.Type

        Msg = "Source for this graphic: " & oShape.LinkFormat.SourceFullName & vbCrLf
        Msg = Msg & "Do you want to continue looking?"    ' Define message.
        Style = vbYesNo ' Define buttons.
        Title = "Looking for Linked Tables"    ' Define title.
        Response = MsgBox(Msg, Style, Title)
        If Response = vbYes Then    ' User chose Yes.
            MyString = "Yes"    ' Perform some action.
        Else    ' User chose No.
            Exit For
            MyString = "No"    ' Perform some action.
        End If

        ' To break the link and insert as a picture, use this:
        'Selection.Copy
        'Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdInLine, DisplayAsIcon:=False
    End If
Next
MsgBox strMsg
End Sub