Mailmerge problem RRS feed

  • Question

  • Hello,

    I am trying to do a mailmerge of a Word document with a datatable. I have made a function and here is what I do : The values are
    replaced normally but I am missing something.

    I have the following problems :
    1) After each line there is a Enter that is pressed, and it is not the case with the original document (and it is not the case when I do
    a mailmerge with the mailmerge method)
    2) The pictures do not work (it is a include picture of a mergefiled) and the mergefield gives a path to the picture
    3) When I do a CTRL+A and after that F9 (to refresh all fields) all the values I entered disapear. with the normal mailmerge method
    nothing happens in the normal fields and the picture appears).

    Can you please tell me what I am missing?

    Thank you

    PrivateSub DoMailMerge (aWordDoc AsObject, adtbSource AsDataTable)       Dim fieldText As String         Dim fieldName As String         Try             For Each mergeField As Object In aWordDoc.Fields                 If (mergeField.Type = 59) Then 'WdFieldType.wdFieldMergeField                     fieldText = mergeField.Code.Text                     fieldName = fieldText.Substring(fieldText.IndexOf("MERGEFIELD") + 10)                     If fieldName.IndexOf("\*") >= 0 Then                         fieldName = fieldName.Substring(0, fieldName.IndexOf("\*")).Trim                     Else                         fieldName = fieldName.Trim                     End If                     mergeField.Result.Text = (adtbSource.Rows(0).Item(fieldName))                                  End If             Next         Finally         End Try

    • Edited by Ciprian Duduiala Monday, March 19, 2012 7:47 AM title
    • Moved by Ciprian Duduiala Monday, March 19, 2012 7:47 AM not in French (From:Développement .NET (en général))
    • Moved by Mike Feng Tuesday, March 20, 2012 2:30 AM Word (From:Visual Basic General)
    Friday, March 16, 2012 11:05 AM

All replies

  • Hi Marc,

    I have moved this thread to a dedicate forum for better support.

    Best regards,

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 20, 2012 2:31 AM
  • Hi Marc Allard,

    Thank you for posting.

    I have read your posts and understand your problem properly. But I found difficult to  reproduce the problem on my side. It will be nice if you can show us the detailed reproducible steps list or upload your project to the skydirve: and share it with me so that we can reproduce and analyze the problem more quickly.

    As for your problems, I can give you some suggestions according your description:

    1. I guess the original document may hide the enter mark, you can try to add the statement to your code:

    aWordDoc.ActiveWindow.View.ShowAll = False which can hide the enter mark.

    2. Have your checked that the path of the pictures are right? I guess the path may be wrong, try to access the file through the path

    3. I am not sure why your fields appeared, how about using the code: ActiveDocument.Fields.Update or the following macro:

    Sub UpdateAll()
    Dim oStory As Range
    Dim oField As Field
    For Each oStory In ActiveDocument.StoryRanges
    For Each oField In oStory.Fields
    Next oField
    Next oStory
    End Sub

    Besides, what is the version of your word application?

    Best Regards,

    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 21, 2012 7:20 AM
  • Hello,

    Thank you for your time.

    I have put a project with the mailmerge. While doing this project I saw where the problem lies but I can't find a way around it.

    When you launch the program you can see one button. You click on it and it will create the word document with the mailmerge.

    A temporary file will be created (release directory ) : merged.doc : it is the merged file and it is correct.

    After that I add the file merged doc in a new document.

    ObjWord.Selection.InsertFile(FileName:=str_PathExe & "merged.doc", Range:=String.Empty, ConfirmConversions:=False, Link:=False, Attachment:=False)

    And it is this line that makes the problem : the new document seems to lose some parameters (you will immediately see the difference between the temporary file merged.doc and the created file.

    When I do a normal mailmerge (with the mailmerge object of word there is no problem).

    For the CTRL+A and F9 in the code I use the


    and I have the problem (every text that had been replaced is again filled by the original variable. In the project I didn't put this line of code.

    For the picture I saw my mistake and I used another method.Perhaps you can tell me if it is a correct one (if it will keep the style, anchor or not)

    The position of the project is  (there is only one file!120&sc=documents

    Thanks again


    Wednesday, March 21, 2012 9:31 AM
  • Hi Marc,

    Sorry for the late reply. I downloaded your project and test on my side, I can reproduced your scenario on my side:

    1. After each line there is a Enter that is pressed.
    2.After updating the fields, all the fields disappeared.

    However, I see that the picture works well.

    Besides, I also tried the docx format, the Enter mark does not appear in the document if using this format.
    But the problem about
    updating the fields still happens. And I haven't get a workaround about this issue.

    So, I will help you involve others to help you. There might be some delay about the response. Appreciate your patience.

    Best Regards,

    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Tuesday, March 27, 2012 2:43 PM
    Tuesday, March 27, 2012 2:37 PM
  • Hello,

    Thank you very much. I will also try to use the Office 2007 format as the output format (force the user to use Office 2007 format).
    I hope you will find an answer for the update of the fields.


    Tuesday, March 27, 2012 2:48 PM