Break links after updating link in word

Hello,

I am trying to create a template in word which updates with information from excel. After it updates, I would like it to break the links from excel automatically and the user to be able to save it as a different document with the updated information and no macros. Right now I have the code below I found on the net ina module under the  normal template  but this it is definitely not a good fix. When I try to paste it to the document module it is not automatic or the excel charts do not update.

'Sub AutoOpen()
'Selection.WholeStory
'Selection.Fields.Unlink
'Selection.HomeKey Unit:=wdStory
'End Sub

I just learned how to use macros this week, please help.

Answer
Answer

The first problem is that a macro like this one, stored in a module in the Normal.dotm template, will affect every document you open, not just ones based on your template with Excel links. You should instead store the macro in the template with Excel links, so it will affect only documents based on that template. Having macros in the template does not save macros into documents that are based on the template -- the macros stay only in the template.

The second problem is that, because the macro name is AutoOpen, it runs only when you open an existing document. That isn't when you want it to run, but I'm not clear on exactly when you do want it to run. Probably the best time is when the user chooses to save the document, but another choice would be when the links are updated. Unfortunately, either of these is going to involve more complicated programming than what you posted. For the save choice, you would want a DocumentBeforeSave event handler. For the link-update choice, you would need a manually launched macro that does the update, breaks the links, and offers the Save As dialog.

A third problem is that your macro shouldn't unlink every field in the body of the document, which is what happens with the macro you posted. In fact, whether or not the thing in the document that looks like part of an Excel worksheet may or may not be a field, depending on how you inserted it. If you used the Paste Special dialog and inserted "as HTML", for example, it is a LINK field; but if you inserted "as Excel Worksheet Object" it will be a Shape. In a macro, these are handled very differently. Your macro might need to handle both, something like this:

   Dim myShape As Shape
   Dim myField As Field
   For Each myShape In ActiveDocument.Shapes
      If Not (myShape.LinkFormat Is Nothing) Then
         myShape.LinkFormat.BreakLink
      End If
   Next
   For Each myField In ActiveDocument.Fields
      If myField.Type = wdFieldLink Then
         myField.Unlink
      End If
   Next

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated May 22, 2023 Views 1,229 Applies to: