Changing Word link source via VBA resets Update method to "Automatic"

theDrew

Board Regular
Joined
May 6, 2003
Messages
104
Hi all,

FYI, the below question regards Word VBA and links to Excel. I am having no luck finding the answer on Word forums, so I am trying here.

I have a Word document with several hundred links to an Excel sheet. I have the below Word macro to allow the user to select a new Excel file via the file open dialog box and change the link source on all links to the newly selected document. It works fairly well, but it resets the Update method to "Automatic" on all of the links. How can I either stop it from doing this, or set the method back to "Manual" via code? I can't figure that out, and I can't have the document updating all links every time the Excel document is changed since there are so many connections. Help?

Remember this is Word VBA code, not Excel.

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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi, do you know how to change the source link for charts as well? Currently it's only changing the 'worksheet' and not 'Chart' Types. Could anyone please help me on this? Thanks in advance!
3.JPG
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top