none
mailmerge strange behavior saving documents RRS feed

  • Question

  • Hello,

    since we changed from Windows XP to Windows 7 64 bit strange behavior occured in the following mailmerge:

    step 1 runs the mailmerge to the printer

    step 2 runs the mailmerge to save each document separately

    step 3 check if all documents are saved

    step 1 and 2 are at once, step 2 will run if step 3 says that not all the documents are saved.

    We don't have any error for not saving to the server. In debug mode this cannot be reproduced.

    The brand new pc (i5/4GB 4570) runs the code more slowly then the olders ones(i5/4GB 2500).

    It takes several minutes to close winword (running in the background).

    the following code is used:

    Private Sub PRINT(ByVal badge, ByVal CHOICE_LETTER, ByVal CHOICE)

           

            If Microsoft.VisualBasic.Left(CHOICE_LETTER, 1) = "T" Then

                CHOICE_LETTER = "U" & Microsoft.VisualBasic.Right(CHOICE_LETTER, 1)

            End If

            Dim reader1 As New StreamReader("D:\template.txt")

            Dim template As String = Nothing

            template = reader1.ReadLine()

            reader1.Close()

             Dim oword As Object

            oword = CreateObject("word.application")

            Dim odoc As Object

            odoc = oword.documents

            oword.documents.open(template)

            oword.visible = True

            Dim source As String = "D:\" & CHOICE_LETTER & "_" & badge & ".docx"

            oword.activedocument.mailmerge.opendatasource(source)

           

            If Microsoft.VisualBasic.Left(CHOICE_LETTER, 1) = "U" Then

                CHOICE_LETTER = "T" & Microsoft.VisualBasic.Right(CHOICE_LETTER, 1)

            End If

            Dim LANG_CHOICE As String

            LANG_CHOICE = Microsoft.VisualBasic.Right(CHOICE, 1)

            Dim strSQL As String

            strSQL = "SELECT * FROM Table where lang_id = '" & LANG_CHOICE & "'"

            oword.ActiveDocument.MailMerge.DataSource.QueryString = strSQL

            'Choice Print and save or only save

            Dim msgboxlabel As String = ""

            If badge = "001" Or badge = "004" Or badge = "005" Then

                msgboxlabel = "PRINT?"

            Else

                msgboxlabel = "Imprimer?"

            End If

            Dim result1 As DialogResult = MessageBox.Show(msgboxlabel, _

                "PRINTMENU", _

                MessageBoxButtons.YesNoCancel, _

                MessageBoxIcon.Question)

            If result1 = Windows.Forms.DialogResult.Yes Then

                Dim printer As String

                printer = ComboBox1.SelectedItem

                oword.activeprinter = printer

                oword.activedocument.mailmerge.destination = 1 'wdsendtoprinter

                With oword.Options

                    .UpdateFieldsAtPrint = True

                    .UpdateLinksAtPrint = True

                End With

                With oword.ActiveDocument.PageSetup

                    .Orientation = 0 'wdOrientPortrait

                End With

     

                oword.activedocument.mailmerge.execute()

            End If

           

            Dim reader As New StreamReader("D:\number_" & LANG_CHOICE & ".txt")

            Dim count_lines As String

            Count_lines = reader.ReadLine()

            reader.Close()

            Dim path As String 

            For x As Integer = 1 To count_lines

                With oword.ActiveDocument.MailMerge

                    .destination = 0 'wdSendToNewDocument

                    .MailAsAttachment = False

                    .MailAddressFieldName = ""

                    .MailSubject = ""

                    .SuppressBlankLines = True

                    With .DataSource

                        .FirstRecord = x

                        .LastRecord = x

                    End With

                    .DataSource.ActiveRecord = x

                    path = oword.ActiveDocument.MailMerge.DataSource.DataFields("dosnumber").Value

                    path = pathtoserver & path & "_" & CHOICE_LETTER & ".doc"

                    .Execute(pause:=True)

                End With

                If oword.ActiveDocument.Saved = False Then oword.ActiveDocument.saveas(path)

      

                oword.ActiveDocument.Close()

     

      

            Next

      

            odoc.close(0)

            oword.quit()

        End Sub

    i added the following but it didn't resolve anything:

    GC.Collect()

    GC.WaitForPendingFinalizers()

    GC.Collect()

    GC.WaitForPendingFinalizers()

    Any help is welcome.

    Bert

    Sunday, October 26, 2014 8:22 AM

Answers

  • Hi,

    finally it was a problem on the file server. There are to many files and even after quitting the application WORD was still trying to put the file on the fileserver.

    I have created a temporary directory to send the files to and then move the files to the right directory.

    This solved the problem.

    Bert

    • Marked as answer by bert_paulus Friday, December 19, 2014 7:22 AM
    Friday, December 19, 2014 7:22 AM

All replies

  • Hi,

    What’s the detail version of your office?

    >> In debug mode this cannot be reproduced.

    Do you mean it takes less time to close the window?

    Please try to do mail merge through UI directly to check whether it takes too long time.

    On the other hand, please run that application in other machine to check the time.

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 27, 2014 9:12 AM
    Moderator
  • Hi Starain,

    it's office 2010 32 bit.

    I adapted the code(see below) but it takes still some time to close winword.exe * 32

    I tried another pc (i5/4GB 4570) and it took 5 minutes to close winword.exe *32 after saving  12 letters.

    NAR(odoc)

    oword.quit(0)

    NAR(oword)

    PrivateSubNAR(ByValo AsObject)

    Try

    While(System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)

    EndWhile

    Catch

    Finally

    o = Nothing

    EndTry

    Bert

    EndSub

    Monday, October 27, 2014 11:17 AM
  • Hi,

    I think you may need to release their references for Word objects by using Marshal.ReleaseComObject method when they are no longer needed.

    # Marshal.ReleaseComObject Method

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 28, 2014 8:34 AM
    Moderator
  • Hi,

    finally it was a problem on the file server. There are to many files and even after quitting the application WORD was still trying to put the file on the fileserver.

    I have created a temporary directory to send the files to and then move the files to the right directory.

    This solved the problem.

    Bert

    • Marked as answer by bert_paulus Friday, December 19, 2014 7:22 AM
    Friday, December 19, 2014 7:22 AM