Share via

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

Anonymous
2011-04-15T21:08:41+00:00

Hey all,

I have a Word document with several hundred links to an Excel sheet. I copied the below Word macro from another forum and then tweaked it for my own use 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. I have to manually set the Update method on all links back to "Manual." My users are not going to remember to do that, and they are likely to have both the Word and the linked Excel documents open simultaneously.

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?

[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
[/code]

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2011-04-15T23:43:36+00:00

hi theDrew,

You're not doing yourself any favours posting code formatted like that. These forums don't need code tags and you can simply paste formatted code into the posting box.

To re-set each Excel link to manual update, try:

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 '

Dim x As Long

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

With ActiveDocument

  fieldCount = .Fields.Count

  For x = 1 To fieldCount

    With .Fields(x)

      'Debug.Print x '

      Debug.Print .Type

      If .Type = 56 Then

        'only update Excel links. Type 56 is an excel link

        .LinkFormat.SourceFullName = newFile '

        .Update

        .LinkFormat.AutoUpdate = False

        DoEvents

      End If

    End With

  Next x

End With

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

Was this answer helpful?

4 people found this answer helpful.
0 comments No comments

15 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-01-17T04:40:45+00:00

    Paul,

    This macro works really well when the linked object is not a chart object.  For these objects it does not change to source name.  Have you come accross a work around to this problem?

    Thanks,

    Chris

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-01-16T17:50:40+00:00

    Paul,

    Thanks for this great reply.  The code works really well.  Can you please advise how this can be modified to preserve formatting suffixes such a {\a \t  \* MERGEFORMAT}

    Thanks

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-11-01T20:44:32+00:00

    Thanks so much for the help Paul! I had the same issue, and used this code to solve it. Very well written.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-04-17T00:23:28+00:00

    Oops, sorry! I posted that right before I left work on Friday, and obviously didn't review the post afterward.  Thanks for correcting it.

    I'll give that a try when I get back to work on Monday. Thanks.

    Was this answer helpful?

    0 comments No comments