none
Mail Merge VBA help RRS feed

  • Question

  • Hi All,

    I have the following peice of code and was wondering is there anyway i can get each item to be saved from one the fields within each document? I have an email address field in the mail merge document that i would like to be used as the document name when saved. Here is my code so far. I no i have to change the saveas part to point to something in the document but not sure how to get it to look at each mail merge document and pull our the email address field and use it as part of the saveas line...Thanks for looking.


    Sub SaveRecsAsFiles()
    ' Convert all sections to Subdocs
    AllSectionsToSubDoc ActiveDocument
    'Save each Subdoc as a separate file
    SaveAllSubDocs ActiveDocument
    End Sub


    Sub AllSectionsToSubDoc(ByRef doc As Word.Document)
    Dim secCounter As Long
    Dim NrSecs As Long
    NrSecs = doc.Sections.Count
    'Start from the end because creating
    'Subdocs inserts additional sections
    For secCounter = NrSecs - 1 To 1 Step -1
    doc.Subdocuments.AddFromRange _
      doc.Sections(secCounter).Range
    Next secCounter
    End Sub


    Sub SaveAllSubDocs(ByRef doc As Word.Document)
    Dim subdoc As Word.Subdocument
    Dim newdoc As Word.Document
    Dim docCounter As Long
    docCounter = 1
    'Must be in MasterView to work with
    'Subdocs as separate files
    doc.ActiveWindow.View = wdMasterView
    For i = 1 To oblist.Tables(1).Rows.Count
    Set DocName = oblist.Tables(1).Cell(i, 1).Range
    DocName.End = DocName.End - 1

    For Each subdoc In doc.Subdocuments
    Set newdoc = subdoc.Open
    'Remove NextPage section breaks
    'originating from mailmerge
    RemoveAllSectionBreaks newdoc
    With newdoc
    .SaveAs FileName:="MergeResult" & CStr(docCounter)
    .Close
    End With
    docCounter = docCounter + 1
    Next subdoc
    End Sub

    Sub RemoveAllSectionBreaks(doc As Word.Document)
    With doc.Range.Find
    .ClearFormatting
    .Text = "^b"
    With .Replacement
    .ClearFormatting
    .Text = ""
    End With
    .Execute Replace:=wdReplaceAll
    End With
    End Sub

    • Moved by Youen Zen Friday, September 7, 2012 5:03 AM To provide better support (From:Visual Basic Language)
    Thursday, September 6, 2012 9:32 AM

Answers

  • Hi dips

    In that case

    1. Go to the Developer tab and insert a plain text content control

    2. Click Properties and give it a Title. Activate the option to protect the control from deletion.

    3. Click inside the content control and insert the merge field.

    4. Execute the mail merge to "edit individual documents".

    You should see that the merge field information is in the content controls in every result document.

    There are various ways to read the information from content controls. From your code sample (it seems very familiar!), it appears you are working within the separated (sub) document. So there will be only the one content control in this document. Thus something like the following should work:

      Dim emailAddress as String
      emailAddress = newdoc .ContentControls(1).Range.Text


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 7, 2012 2:33 PM
    Moderator

All replies

  • Anyone?
    Thursday, September 6, 2012 3:18 PM
  • Sorry to tell you, but this Forum is for VB.NET only. For VBA questions please ask here: http://social.msdn.microsoft.com/Forums/en-GB/isvvba/threads

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Thursday, September 6, 2012 3:26 PM
  • Hi Dips44,

    We have a forum Word for Developers for this issue. In order to provide better support, I will move this thread.

    Thanks for understanding!


    Shanks Zen
    MSDN Community Support | Feedback to us

    Friday, September 7, 2012 5:02 AM
  • Hi dips

    Which version of Word are you using?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 7, 2012 8:56 AM
    Moderator
  • Word 2007
    Friday, September 7, 2012 9:02 AM
  • Hi dips

    In that case

    1. Go to the Developer tab and insert a plain text content control

    2. Click Properties and give it a Title. Activate the option to protect the control from deletion.

    3. Click inside the content control and insert the merge field.

    4. Execute the mail merge to "edit individual documents".

    You should see that the merge field information is in the content controls in every result document.

    There are various ways to read the information from content controls. From your code sample (it seems very familiar!), it appears you are working within the separated (sub) document. So there will be only the one content control in this document. Thus something like the following should work:

      Dim emailAddress as String
      emailAddress = newdoc .ContentControls(1).Range.Text


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 7, 2012 2:33 PM
    Moderator