Alarto
Alarto

Reputation: 23

Change CHART source link in Word VBA

I have a document in word with tables and charts linked to excel. I need to change the link of these once in a month. when i run the below link, tables source is changed to the new file, but the source link of the chart remain the same and doesn't change. Here the code:

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

Can anyone help me please?

Thanks

Upvotes: 0

Views: 714

Answers (1)

macropod
macropod

Reputation: 13505

Try processing the tables & charts as shapes and inlineshapes:

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

Upvotes: 2

Related Questions