Break link to picture programatically in MS Word 2010

3

1

I have a Word document with pictures that are linked. I want to embed them to the word document, but I do not want to do it manually.

Manually going to: File --> edit links to files --> selecting and hitting "break link" does exactly what I want to do.

I found some VBA code for breaking links to fields, but that does not help to break the links to my pictures. Here is the code that I tried:

For Each objField In ActiveDocument.Fields
  If Not objField.LinkFormat Is Nothing Then
    objField.LinkFormat.Update
    objField.LinkFormat.BreakLink
    ActiveDocument.UndoClear
  End If
Next

When I hit Alt + F9 in my document to display fields nothing happends to my pictures, but other fields (links etc) is expanded/displayed. I therefore assume that these picture links are something else than "fields".

How can I break these picture links in VBA?

Edit Note The word document is basically a html file. That is what you get by opening a .html file in Word.

exalt

Posted 2013-09-12T08:34:11.100

Reputation: 41

What type of field does your images appear as before and after that macro is run? – Adam – 2013-09-13T12:29:58.960

@Adam: As far as I can see, no type of filed. I tried the Alt + F9 to see the field type, but nothing happens to my pictures – exalt – 2013-09-15T11:16:55.853

What does the context menu look like when you right click on the image? Are there any references to packages or buttons such as "Activate contents" or "Convert"? Otherwise, you might just have a regular image embedded in your document with no link to start with :) – Adam – 2013-09-16T05:00:36.023

@Adam it looks "normal", unfortunatly I cant post pictures yet (due to repudiation), but there is no "activate contents" "convert" etc. Although I know the image is not embedded. The size is 10kb, after breaking 1 link is is typically 50kb. Also, moving the document results in the images not being displayed as the links are relative. – exalt – 2013-09-16T06:48:28.303

Sounds strange. Can you try running the following code to determine if they are fields at all? (remove all other content temporarily). For Each Field In ActiveDocument.Fields MsgBox (Field.Type) Next Field Resolved field types by looking up WdFieldType Enumeration

– Adam – 2013-09-17T12:15:35.613

@Adam Thanks for the tip, as a result I was able to fix it! The code you provided did not result in any output at first try. Then I saved the document as .doc and tried again, this time getting an id. I then tried to run the breakLinks macro and it worked. The reason (I guess) is that the Word document was in fact a html-file. So without saving it first Word hadn't added the necessary metadata to identify the fields. – exalt – 2013-09-18T12:33:49.013

Well done. You can post this as the answer to your own question and accept it. – Adam – 2013-09-18T12:59:32.340

Answers

1

As the word document was in fact a html document opened in Word it did not include word specific markup to identify the fields. As a result the GUI functionality worked, but not the vba solution as detailed in the question. The solution was to first save the word document with a new name (thus generating word markup), and then run the breakLinks macro.

Macro for saving the document:

Sub saveAsDoc()
    Dim newName As String
    newName = ActiveDocument.Path & "\" & "fix_" & ActiveDocument.Name
    ActiveDocument.SaveAs2 FileName:=newName, FileFormat:=wdFormatDocument
End Sub

Macro for breaking links:

Sub breakLinks()
For Each objField In ActiveDocument.Fields
  If Not objField.LinkFormat Is Nothing Then
    objField.LinkFormat.Update
    objField.LinkFormat.BreakLink
    ActiveDocument.UndoClear
  End If
Next
End Sub

Main macro that should be executed from a extrenal script. The displayAlerts is to make sure there are no pop-us.

Sub theTrick()
    Application.DisplayAlerts = False
    Call saveAsDoc
    Call breakLinks
    ActiveDocument.save
    Application.DisplayAlerts = True
End Sub

exalt

Posted 2013-09-12T08:34:11.100

Reputation: 41

This answer helped me a lot, but for some reason I had to break the links on the .InlineShapes instead of .Fields. – wcoenen – 2014-11-06T00:22:52.140