在Word VBA中更改CHART源链接

时间:2019-01-24 14:36:00

标签: vba ms-word

我有一个文字文档,其中的表格和图表链接到excel。 我需要每月更改一次这些链接。 当我运行下面的链接时,表源更改为新文件,但图表的源链接保持不变,并且未更改。 这里的代码:

Sub Replace_Link()
Dim fieldCount As Integer, x As Long
With ActiveDocument
fieldCount = .Fields.Count
For x = 1 To fieldCount
With .Fields(x)
    Debug.Print .LinkFormat.SourceFullName;
    .LinkFormat.SourceFullName = "Q:\LINKNEWFILE"
    .Update
    .LinkFormat.AutoUpdate = False
    DoEvents
End With
Next x
End With
End Sub

有人可以帮我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

尝试将表格和图表处理为形状和内嵌形状:

Sub Replace_Links()
Dim x As Long: Const StrLnk As String = "Q:\LINKNEWFILE"
With ActiveDocument
  For x = .Shapes.Count To 1 Step -1
    With .Shapes(x)
      If Not .LinkFormat Is Nothing Then
        With .LinkFormat
          .SourceFullName = StrLnk
          .Update
          .AutoUpdate = False
        End With
      End If
    End With
  Next x
  For x = .InlineShapes.Count To 1 Step -1
    With .InlineShapes(x)
      If Not .LinkFormat Is Nothing Then
        With .LinkFormat
          .SourceFullName = StrLnk
          .Update
          .AutoUpdate = False
        End With
      End If
    End With
  Next x
End With
End Sub