2

My goal is to somehow be able to read bookmarks in an outlook .msg file, then replace them with a different text. I want to do this with C#.

I know how to access the body and change the text, but was wondering if there was a way to access directly the list of all the bookmarks and its location so that i can easily replace them, instead going through the whole body text, splitting it up, etc etc...

edit: this is how a bookmark window looks like from this window one can assign bookmarks, but it should be possible to obtain this list via c#.

enter image description here

Any relevant info is appreciated. Thanks in advance.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
Mana
  • 1,925
  • 6
  • 39
  • 55

1 Answers1

0

Since Outlook most often uses Word as it's body editor - you need to add a project reference to Microsoft.Office.Interop.Word.dll and then access to the Outlook Inspector's WordEditor during the Inspector.Activate event. Once you have access to the Word.Document - it's trivial to load up the Bookmarks and access/modify their values.

Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
((Outlook.InspectorEvents_10_Event)inspector).Activate += () =>
{   // validation to ensure we are using Word Editor
    if (inspector.EditorType == Outlook.OlEditorType.olEditorWord && inspector.IsWordMail())
    {
        Word.Document wordDoc = inspector.WordEditor as Word.Document;
        if (wordDoc != null)
        {
            var bookmarks = wordDoc.Bookmarks;
            foreach (Word.Bookmark item in bookmarks)
            {
                string name = item.Name; // bookmark name
                Word.Range bookmarkRange = item.Range; // bookmark range
                string bookmarkText = bookmarkRange.Text; // bookmark text
                item.Select(); // triggers bookmark selection
            }
        }
    }
};
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • I have referenced as you said to microsoft.office.interop.word.dll, but when i write the first line, i cant find Globals.ThisAddIn..... – Mana May 16 '13 at 06:37
  • What version of Visual Studio and Office are you using? [Globals.ThisAddIn is automatically generated by Visual Studio](http://msdn.microsoft.com/en-us/library/vstudio/bhczd18c.aspx) for an Office plugin project. If you are not using an Outlook Plugin - you won't have Globals.ThisAddIn. – SliverNinja - MSFT May 16 '13 at 14:04
  • im using vs2005 and office 2007 i have also the outlook interop referenced. – Mana May 21 '13 at 04:58