Hi, I need some help in updating my chart sources from Excel worksheet. Actually this code was from another thread, but I needed an added functionality.
Right now the below code opens up a dialogue box when the macro is run and prompts the user to select an excel file. After selection, the links change to the selected 'source' excel file.
Right now, only the tables in the word document get updated and not the charts. I didn't paste the charts using 'Special Link' and that was intentional. I inserted it using the option 'Keep Source Formatting and Link Data'. The below screenshot depicts the different File Type for the links in Word. So the current code updates the 'Worksheet' but not the 'Chart' File Type.
Thank you in advance!
Right now the below code opens up a dialogue box when the macro is run and prompts the user to select an excel file. After selection, the links change to the selected 'source' excel file.
Right now, only the tables in the word document get updated and not the charts. I didn't paste the charts using 'Special Link' and that was intentional. I inserted it using the option 'Keep Source Formatting and Link Data'. The below screenshot depicts the different File Type for the links in Word. So the current code updates the 'Worksheet' but not the 'Chart' File Type.
Thank you in advance!
VBA Code:
Public Sub changeSource()
Dim dlgSelectFile As FileDialog 'FileDialog object
'Dim thisField As Field
Dim selectedFile As Variant 'must be Variant to contain filepath of selected item
Dim newFile As Variant
Dim fieldCount As Integer
'On Error GoTo LinkError
'create FileDialog object as File Picker dialog box
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Clear 'clear filters
.Filters.Add "Microsoft Excel Files", "*.xls, *.xlsb, *.xlsm, *.xlsx" 'filter for only Excel files
'use Show method to display File Picker dialog box and return user's action
If .Show = -1 Then
'step through each string in the FileDialogSelectedItems collection
For Each selectedFile In .SelectedItems
newFile = selectedFile 'gets new filepath
Next selectedFile
Else 'user clicked cancel
Exit Sub
End If
End With
Set dlgSelectFile = Nothing
'update fields
fieldCount = ActiveDocument.Fields.Count
For x = 1 To fieldCount
'Debug.Print x
'Debug.Print ActiveDocument.Fields(x).Type
If ActiveDocument.Fields(x).Type = 56 Then 'only update Excel links. Type 56 is an excel link
ActiveDocument.Fields(x).LinkFormat.SourceFullName = newFile
'DoEvents
End If
Next x
MsgBox "Source data has been successfully imported."
Exit Sub
LinkError:
Select Case Err.Number
Case 5391 'could not find associated Range Name
MsgBox "Could not find the associated Excel Range Name for one or more links in this document. " & _
"Please be sure that you have selected a valid Quote Submission input file.", vbCritical
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
End Select
End Sub