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.
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments