Unlinking images in word document after mail merge

didijaba

Well-known Member
Joined
Nov 26, 2006
Messages
511
Hi,
I'm doing large mail merge with lots of pictures, and I used macropod advices (thanks macropod), but I have problem when I need to send created word document to someone. It has no images. I'm using Office 2016 (Win). Here are my VBA code and formula for placing images. Images, source excel workbook and word template are all in same folder (same path).
Thanks in advance for any advice.

2d0gx9f.jpg


Code:
Sub Merge_To_Individual_Files()
'Merges one record at a time to the folder containing the mailmerge main document.
' Sourced from: https://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Const StrNoChr As String = """*./\:?|"
Set MainDoc = ActiveDocument
With MainDoc
  StrFolder = .Path & Application.PathSeparator
  For i = 1 To .MailMerge.DataSource.RecordCount
    With .MailMerge
      .Destination = wdSendToNewDocument
      .SuppressBlankLines = True
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        If Trim(.DataFields("FIELD_1")) = "" Then Exit For
        'StrFolder = .DataFields("Folder") & Application.PathSeparator
        StrName = .DataFields("FIELD_1") & "_" & .DataFields("FIELD_2")
      End With
      .Execute Pause:=False
    End With
    For j = 1 To Len(StrNoChr)
      StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
    Next
    StrName = Trim(StrName)
    With ActiveDocument
    MailMergeToDoc
       .SaveAs FileName:=StrFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
       .SaveAs FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
      .Close SaveChanges:=False
    End With
  Next i
End With
Application.ScreenUpdating = True
End Sub

Private Sub MailMergeToDoc()
Application.ScreenUpdating = False
'ActiveDocument.MailMerge.Execute
Dim Fld As Field
For Each Fld In ActiveDocument.Fields
  If Fld.Type <> wdFieldHyperlink Then Fld.Unlink
Next
Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Your code is making a dog's breakfast of the merge. The MailMergeToDoc code you've tried to shoehorn into the process is for use where all the records are output to a single file. Simply use the original code, but with your:
StrName = .DataFields("FIELD_1") & "_" & .DataFields("FIELD_2")
and, assuming you don't have any hyperlinks in the output, before the first of that code's SaveAs lines, insert:
.Fields.Unlink
otherwise, insert:
Code:
  Dim k As Long
  For k = .Fields.Count To 1 Step -1
    If .Fields(k).Type <> wdFieldHyperlink Then .Fields(k).Unlink
  Next
PS: You don't need the \* MERGEFORMAT switches.
 
Last edited:
Upvote 0
Thanks for you quick replay. I did as you suggested, but .Fields.Unlink didn't work when used in macro. When applied to created word files it works as planned. As I need to review final documents and make smaller additions before considering them final, for now I'll just run .Fields.Unlink on each individual file manually. Thanks Macropod for very detail posts on how to do things in word, they are most helpful. Thanks again
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top