none
Outlook add-in overwrites Word RRS feed

  • Question

  • I have an Outlook add-in that creates an instance of Word in order to use if to count the pages and characters in an email.  This works fine if only Outlook is running.  The instance of Word shows up in Tasks manager processes.  If I start Word and then start Outlook two instances of Word are created and everything works OK.  However, if I start Outlook and then open an instance of Word my add-in overwites the open word document with the text of the email.

    Below is the code of the sub that initially creates the Word instance.  All the defensive code is because the sub can be called elsewhere in the program if there is a "RPC server unavailable" error.

    Dim strAction As String
            If wdApp Is Nothing Then
                strAction = "Creating wdApp"
                wdApp = CreateObject("Word.Application")
            End If
            Try
                strAction = "Creating document"
                wdApp.Documents.Add()
            Catch ex As Exception
                'Execution comes here if this sub is called a second time because the RPC server is unavailable
                Marshal.ReleaseComObject(wdApp)
                GC.Collect()
                strAction = "Recreating wdApp"
                wdApp = CreateObject("Word.Application")
                wdApp.Documents.Add()
            End Try

    Any ideas what I can do?


    Mike VE

    Tuesday, August 21, 2012 4:48 PM

Answers

  • If you use Outloook 2007/2010 then you can rely on the fact that MS Word is the default editor of Outlook.

    here is how you can get access to the body of your emal as a word document.

    Dim wrdEditor As Object = mailItem.GetInspector().WordEditor()

    Dim wrdDoc As Document

    wrdDoc = CType(wrdEditor, Document)

    then you can querty the document for the word stats.

    Thanks,

    Michael

    • Marked as answer by Mike VE Friday, August 24, 2012 12:41 PM
    • Unmarked as answer by Mike VE Friday, August 24, 2012 12:42 PM
    • Marked as answer by Mike VE Friday, August 24, 2012 12:42 PM
    Tuesday, August 21, 2012 9:40 PM

All replies

  • You probably should be grabbing a running instance of Word rather than using CreateObject() in all cases.
     
    BTW, usually if you get that RPC server unavailable message there's a mismatch in user permissions of the applications. For example if an application started with user or asInvoker permissions is called by another application started with admin permissions.
     
    The following snippet is in c#, but you can adapt it for what you want.
     

                    System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WORD");
                    int collCount = processes.Length;
                    if (collCount != 0)
                    {
                        // already running, hook into the Word instance
                        wordApp = Marshal.GetActiveObject("Word.Application") as Word.Application;
                    }
     
                    if (wordApp != null)
                    {
                        canQuit = false;
                    }
                    else
                    {
                        // Word not already running, start it
                        wordApp = new Word.Application();
                        canQuit = true;
                    }

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Mike VE" <=?utf-8?B?TWlrZSBWRQ==?=> wrote in message news:6215baa1-ab20-4177-a338-464f307f147d...

    I have an Outlook add-in that creates an instance of Word in order to use if to count the pages and characters in an email.  This works fine if only Outlook is running.  The instance of Word shows up in Tasks manager processes.  If I start Word and then start Outlook two instances of Word are created and everything works OK.  However, if I start Outlook and then open an instance of Word my add-in overwites the open word document with the text of the email.

    Below is the code of the sub that initially creates the Word instance.  All the defensive code is because the sub can be called elsewhere in the program if there is a "RPC server unavailable" error.

    Dim strAction As String
            If wdApp Is Nothing Then
                strAction = "Creating wdApp"
                wdApp = CreateObject("Word.Application")
            End If
            Try
                strAction = "Creating document"
                wdApp.Documents.Add()
            Catch ex As Exception
                'Execution comes here if this sub is called a second time because the RPC server is unavailable
                Marshal.ReleaseComObject(wdApp)
                GC.Collect()
                strAction = "Recreating wdApp"
                wdApp = CreateObject("Word.Application")
                wdApp.Documents.Add()
            End Try

    Any ideas what I can do?


    Mike VE


    Ken Slovak MVP - Outlook
    Tuesday, August 21, 2012 5:01 PM
    Moderator
  • usually office apps try to re-use processes as much as possible, so different word docs will still be handled by single process and that what what you are seeing. Try to start word by using ProcessStartInfo parring parameter /safe , it should start new word process, just for you.
    Tuesday, August 21, 2012 6:39 PM
  • If you use Outloook 2007/2010 then you can rely on the fact that MS Word is the default editor of Outlook.

    here is how you can get access to the body of your emal as a word document.

    Dim wrdEditor As Object = mailItem.GetInspector().WordEditor()

    Dim wrdDoc As Document

    wrdDoc = CType(wrdEditor, Document)

    then you can querty the document for the word stats.

    Thanks,

    Michael

    • Marked as answer by Mike VE Friday, August 24, 2012 12:41 PM
    • Unmarked as answer by Mike VE Friday, August 24, 2012 12:42 PM
    • Marked as answer by Mike VE Friday, August 24, 2012 12:42 PM
    Tuesday, August 21, 2012 9:40 PM
  • Thanks for the input everyone.  I think Office2007 Developer's suggestion is the one I will use for now.  It does not even start an instance of Word.  I think it will be acceptable to have the calculation of pages as an option for those who use OL 2007/2010.

    Mike VE

    Friday, August 24, 2012 12:45 PM