Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Outlook freezes after creating e-mails from word doc
 

Outlook freezes after creating e-mails from word doc

Hello All,

This problem has been kicking my butt for two days now.  I've searched everywhere I know and have yet to find a solution.  The closest I've found was a note from Sue Mosher in a thread that says creating mails this way will not release the resources then there was a brief explenation on what to do about it, but the explanation didn't make any sense to me.  I'm using Vis Stud 2008,
Word and Outlook 2007

I've added a For..Next loop to loop through and create several e-mails for testing.  The actuall program loops through several records in a dataset and populates other parts of the word document based on that data, but when I strip it down to basics (below) it appears to be working fine, but 4 or 5 minutes after the process complets, my e-mail freezes up and won't start working again until I open task manager and force outlook to close.

I've tried setting "theMail" to MailItem, Outlook.MailItem and Word.MailItem  all to no avail.  Any help on this would be greatly appreciated!!!


     
For x = 0 To 10

 

 

            Dim wordApp As Word.Application

            Dim wordDoc As Word.Document

 

            wordApp = New Word.Application

            wordApp.Visible = True

 

            wordDoc = wordApp.Documents.Add(Template:=theFile)

 

 

            wordDoc.Bookmarks("theMonthDate").Range.Text = DateAndTime.Day(Now()) & " " & MonthName(Month(Now())) & ", " & Year(Now())

            wordDoc.Bookmarks("theMonth").Range.Text = MonthName(Month(Now()))

            wordDoc.Bookmarks("theMonth2").Range.Text = MonthName(Month(Now()))

 

            ''''other stuff that puplulates a table here based on a Recordset removed for testing purposes''''''''''

 

 

            Dim theMail As Outlook.MailItem

 

            theMail = wordDoc.MailEnvelope.Item

 

            With theMail

                .To = "a-jimp"

                .Subject = "test" & Now()

                .Save()

            End With

 

 

            wordDoc.Close(SaveChanges:=False)

            wordDoc = Nothing

 

            wordApp.Quit()

 

        Next

 

MijRetrop  Thursday, October 15, 2009 10:55 PM

Hello,

I am not sure that the com objects is released properly in your said. In my said, I wrote a testforreleasing comobjects in for loop. Howeverit works fine.To your code, every time in the loop, it starts tolaunch a Wordapplication again, the coderuns very slowly.Here, my suggestion is to define the wordApp outside of for loop, and release the com object inside/outside of for loop.

In my side, I have made a slight change with the code as below.

 Dim wordApp As Word.Application
        wordApp = New Word.Application
        For x = 0 To 10
            Dim wordDoc As Word.Document
            wordApp.Visible = True
            Dim theFile As Object = "C:\Temp\text1.docx"
            wordDoc = wordApp.Documents.Add(Template:=theFile)
            wordDoc.Bookmarks("theMonthDate").Range.Text = DateAndTime.Day(Now()) & " " & MonthName(Month(Now())) & ", " & Year(Now())
            'wordDoc.Bookmarks("theMonth").Range.Text = MonthName(Month(Now()))
            'wordDoc.Bookmarks("theMonth2").Range.Text = MonthName(Month(Now()))
            Dim theMail As Outlook.MailItem
            theMail = wordDoc.MailEnvelope.Item
            With theMail
                .To = "a-jimp"
                .Subject = "test" & Now()
                .Save()
            End With
            wordDoc.Close(SaveChanges:=False)
            wordDoc = Nothing
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()
        Next
        wordApp.Quit()
        Marshal.ReleaseComObject(wordApp)
Useful links:
Release object.
VSTO and Office objects disposing.

Best regards,
Bessie

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bessie Zhao  21 hours 41 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Paragraph Alignment when Creating a Word Document- VB.NET
• Problem with finding Outlook contact item by CustomerID value
• Excel 2007 entering header and footer cells via C#
• Excel Add-In won't load? help!
• Outlook Addin to Interact With External Application
• A program is trying to access e-mail addresses you have stored in Outlook
• VSTO 3 - Word Document Template Project
• What is good way for handling about .pst(schedule, task)?
• Selection.End(Excel.XlDirection.xlDown).select()
• Setting CurrentThread.CurrentCulture From Browser Creates Bad Japanese In Excel Document Created Using PIA