2016-05-13 50 views
0

如何使用宏自動化更新鏈接到Publisher中的OLEObjects的過程?我發現了一些關於如何做到這一點的討論(主要在PowerPoint中),並且在嘗試將這個應用於2010 Publisher文檔時遇到了一些問題。在發佈服務器上使用vba更新文件鏈接

首先,當我在消息框中顯示.LinkFormat.SourceFullName時,在我通過菜單選項編輯指向文件的鏈接時,沒有任何Excel工作表或對象的引用出現在對話框中。

其次,當我試圖簡單地更改顯示爲.SourceFullName的文件名時,我收到一條編譯錯誤消息,說我無法分配給只讀屬性。

任何幫助,這將不勝感激。

當我應用以下代碼時,在嘗試分配新鏈接名稱時出現編譯錯誤。如果我將.SourceFullName = newlinkname行註釋掉,我會看到一些消息框,告訴我有Edit和Open ObjectVerbs可用,並且顯示的鏈接名稱僅包含「C:\ Desktop \ Projects \ old.xlsx」。當我通過功能區使用「編輯鏈接到文件」命令時,它不顯示我看到鏈接到對象的工作表和圖表引用。在那裏,對話框實際顯示:!

old.xlsx H_A_CurrStat_byYrDxBar [old.xlsx] H_A_CurrStat_byYrDxBar圖1

我想使用宏來源的名字從old.xlsx改爲new.xlsx和然後更新鏈接的對象。

Option Explicit 

Sub links() 

Dim linkname As String 
Dim newlinkname As String 
Dim shpShape As Shape 
Dim intCount As Integer 

newlinkname = "C:\Desktop\Projects\new.xlsx" 

    For Each shpShape In ActiveDocument.Pages(1).Shapes 
     shpShape.OLEFormat.Activate 

       With shpShape.OLEFormat 
        For intCount = 1 To .ObjectVerbs.count 
        MsgBox .ObjectVerbs(intCount) 
        Next 
       End With 

      With shpShape.LinkFormat 
       linkname = .SourceFullName 
       '.SourceFullName = newlinkname 
       MsgBox linkname 
      End With 

     shpShape.LinkFormat.Update 
    Next 

End Sub 

回答

0

https://msdn.microsoft.com/en-us/library/office/ff940537.aspx?f=255&MSPPError=-2147217396

這對於激活所有對象的代碼可能會有所幫助:

Sub ActivateOLEObjects() 
      Dim shpShape As Shape 

      For Each shpShape In ActiveDocument.Pages(1).Shapes 
        If shpShape.Type = pbLinkedOLEObject Then 
          shpShape.OLEFormat.Activate 
        End If 
      Next 
    End Sub 

或者甚至更好,這個例子代碼更新鏈接: https://msdn.microsoft.com/en-us/library/office/ff939544.aspx

Sub FindOLEObjects() 
      Dim shpShape As Shape 

      For Each shpShape In ActiveDocument.Pages(1).Shapes 
        If shpShape.Type = pbLinkedOLEObject Then 
          shpShape.LinkFormat.Update 
        End If 
      Next shpShape 
    End Sub 

如果既沒有幫助,請提供一些更多信息,如示例文件或您當前的代碼

相關問題