更改Word文档中所有链接的来源 – 范围错位

我使用此代码将所有链接的字段/图表/ Word模板中的源代码更改为从其启动的工作簿。

我有平常的领域图表 (这是存储在InlineShapes ),所以我有2个循环为每个模板。


这些循环有时会停留在For Each ,并在Fields / InlineShapes上循环(甚至不会增加索引…)而不停止。 (我为此添加了DoEvents ,似乎减less了这种情况发生的频率… 如果你有一个解释,这将是非常受欢迎的!

而且For i = ... to .Count ,现在它的工作原理非常完美, 除了Pasted Excel Range每次从A1开始)以及工作簿的活动工作表上更改为相同大小的范围


为了避免InlineShapes问题,我添加了一个testing来知道LinkFormat.SourceFullName是否可以访问,从而避免了一个会阻止进程的错误:

 Function GetSourceInfo(oShp As InlineShape) As Boolean Dim test As Variant On Error GoTo Error_GetSourceInfo test = oShp.LinkFormat.SourceFullName GetSourceInfo = True Exit Function Error_GetSourceInfo: GetSourceInfo = False End Function 

我注意到我的模板中有两种链接的InlineShapes

图表

粘贴为Microsoft Office Graphic Object.hasChart = -1 .Type = 12 .LinkFormat.Type = 8

范围

粘贴为Picture (Windows Metafile) .hasChart = 0 .Type = 2 .LinkFormat.Type = 0

这是我的InlineShapes循环:

 For i = 1 To isCt If Not GetSourceInfo(oDoc.InlineShapes(i)) Then GoTo nextshape oDoc.InlineShapes(i).LinkFormat.SourceFullName = NewLink DoEvents nextshape: Next i 

因为我只更新.SourceFullName ,它只描述path和文件,我不知道为什么或如何影响最初选定的范围…

问题概述: Pasted Excel Range ,将其更改为相同大小的范围,每次从A1开始,并在工作簿的活动工作表上

和任何其他投入如何更新Word链接将不胜感激!


正如Andrew Toomey的回答中所build议的那样,我使用了HyperLinks,但是在我的每个模板中,集合都是空的:

在这里输入图像说明


我已经尝试了很多不同的组合,下面是我清理的内容:

 Sub change_Templ_Args() Dim oW As Word.Application, _ oDoc As Word.Document, _ aField As Field, _ fCt As Integer, _ isCt As Integer, _ NewLink As String, _ NewFile As String, _ BasePath As String, _ aSh As Word.Shape, _ aIs As Word.InlineShape, _ TotalType As String On Error Resume Next Set oW = GetObject(, "Word.Application") If Err.Number <> 0 Then Set oW = CreateObject("Word.Application") On Error GoTo 0 oW.Visible = True NewLink = ThisWorkbook.Path & "\" & ThisWorkbook.Name BasePath = ThisWorkbook.Path & "\_Templates\" NewFile = Dir(BasePath & "*.docx") Do While NewFile <> vbNullString Set oDoc = oW.Documents.Open(BasePath & NewFile) fCt = oDoc.Fields.Count isCt = oDoc.InlineShapes.Count MsgBox NewFile & Chr(13) & "Fields : " & oDoc.Fields.Count & Chr(13) & "Inline Shapes : " & isCt For i = 1 to fCt With oDoc.Fields(i) '.LinkFormat.AutoUpdate = False 'DoEvents .LinkFormat.SourceFullName = NewLink '.Code.Text = Replace(.Code.Text, Replace(.LinkFormat.SourceFullName, "\", "\\"), Replace(NewLink, "\", "\\")) End With Next i For i = 1 To isCt If Not GetSourceInfo(oDoc.InlineShapes(i)) Then GoTo nextshape With oDoc.InlineShapes(i) .LinkFormat.SourceFullName = NewLink DoEvents 'MsgBox .LinkFormat.SourceFullName & Chr(13) & Chr(13) & _ "Type | LF : " & .LinkFormat.Type & Chr(13) & _ "Type | IS : " & .Type & Chr(13) & _ "hasChart : " & .HasChart & Chr(13) & Chr(13) & _ Round((i / isCt) * 100, 0) & " %" End With nextshape: Next i MsgBox oDoc.Name & " is now linked with this workbook!" oDoc.Save oDoc.Close NewFile = Dir() Loop oW.Quit Set oW = Nothing Set oDoc = Nothing MsgBox "All changes done.", vbInformation + vbOKOnly, "End proc" End Sub 

也许不是所有的字段/形状都链接在一起,并且字段/形状的原始插入导致不是在对象上创build的所有属性。

要推进您的代码并更详细地了解对象的情况,请尝试忽略并报告错误。 使用手表来检查物体。

例如:

 On Error Goto fieldError For Each aField In oDoc.Fields With aField .LinkFormat.AutoUpdate = False DoEvents .LinkFormat.SourceFullName = NewLink .Code.Text = Replace(.Code.Text, Replace(.LinkFormat.SourceFullName, "\", "\\"), Replace(NewLink, "\", "\\")) Goto fieldContinue fieldError: MsgBox "error: <your info to report / breakpoint on this line>" fieldContinue: End With Next aField 

Ps: DoEvents的目的是什么? 这将处理外部事件(Windows消息)。

我认为使用hyperlinks集合是解决问题的关键 – 除非你有特定的理由不要。 从Word文档到Excel工作簿的链接是外部链接,因此应该全部列在“ Hyperlinks集合中(无论它们是文本链接还是链接的InlineShapes)。

这是我的代码,可能有一些帮助。 为了简单,我已经硬编码的Word文档,因为这不是一个问题给你:

 Sub change_Templ_Args() WbkFullname = ActiveWorkbook.FullName 'Alternatively... 'WbkFullname = "C:\temp\myworkbook.xlsx" 'Application.Workbooks.Open Filename:=WbkFullname 'Get Document filename string MyWordDoc = "C\Temp\mysample.docx" Set oW = CreateObject("Word.Application") oW.Documents.Open Filename:=MyWordDoc Set oDoc = oW.ActiveDocument 'Reset Hyperlinks For Each HypLnk In oDoc.Hyperlinks HypLnk.Address = WbkFullname Next End Sub 

如果您确实需要使用FieldsInlineShapes尝试使用此代码。 我在For循环中使用了变体,并为“内容列表”或“交叉引用”字段的字段添加了对wdLinkTypeReference的检查 – 这些链接是文档内部的。

 'Reset links to InlineShapes For Each InShp In ActiveDocument.InlineShapes If Not InShp.LinkFormat Is Nothing Then InShp.LinkFormat.SourceFullName = WbkFullname End If If InShp.Hyperlink.Address <> "" Then InShp.LinkFormat.SourceFullName = WbkFullname End If Next 'Reset links to fields For Each Fld In ActiveDocument.Fields If Not Fld.LinkFormat Is Nothing Then If Fld.LinkFormat.Type <> wdLinkTypeReference Then Fld.LinkFormat.SourceFullName = WbkFullname End If End If Next