3

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.

7
  • What type of field does your images appear as before and after that macro is run?
    – Adam
    Sep 13, 2013 at 12:29
  • @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
    Sep 15, 2013 at 11:16
  • 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
    Sep 16, 2013 at 5:00
  • @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
    Sep 16, 2013 at 6:48
  • 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
    Sep 17, 2013 at 12:15

1 Answer 1

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
1
  • This answer helped me a lot, but for some reason I had to break the links on the .InlineShapes instead of .Fields.
    – wcoenen
    Nov 6, 2014 at 0:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .