2011-05-13 4 views
15

J'ai écrit il y a quelques temps un ajout pour Outlook qui ajoute/supprime un slogan facultatif sous la signature dans un message Outlook. Ce complément fonctionne sans problèmes. Je suis en train d'écrire un second add-in qui doit potentiellement ajouter des informations en-dessous (que la signature optionnelle soit présente ou non) et je referme de nouveau le signet _MailAutoSig depuis l'éditeur de Word. Le problème que je rencontre est que ce signet ne semble plus apparaître, pas plus que le signet de mon autre add-in. Une différence dans les deux morceaux de code ci-dessous est que le premier a le MailItem étant converti à partir d'un objet passé par ItemSend, tandis que le second est traité AVANT l'événement ItemSend._MailAutoSig Signet manquant (Outlook 2010)

Voici le code de ce que je suis en train d'écrire:

  Word.Document toMsg = msg.GetInspector.WordEditor as Word.Document; 

     foreach (Word.Bookmark b in toMsg.Bookmarks) 
      Debug.Print(b.ToString()); 

     Word.Range r_toMsg; 

     try 
     { 
      string oBookmark = "_MailAutoSig"; // Outlook internal bookmark for location of the e-mail signature` 
      object oBookmarkObj = oBookmark; 
      if (toMsg.Bookmarks.Exists(oBookmark) == true) 
       Debug.Print("sigbookmark"); 
      r_toMsg = toMsg.Bookmarks.get_Item(ref oBookmarkObj).Range; 
     } 
     catch 
     { 
      string oOffsiteBookmark = "OffsiteBookmark"; 
      object oOffsiteBookmarkObj = oOffsiteBookmark; 

      if (toMsg.Bookmarks.Exists(oOffsiteBookmark) == true) // if the custom bookmark exists, remove it 
       Debug.Print("offsite bookmark"); 
     } 
     finally 
     { 
      r_toMsg = toMsg.Range(missing,missing); 
     } 

et voici le code de mon travail add-in:

void InsertOffsiteSig(Outlook.MailItem oMsg) 
{ 
    object oBookmarkName = "_MailAutoSig"; // Outlook internal bookmark for location of the e-mail signature 
    string oOffsiteBookmark = "OffsiteBookmark"; // bookmark to be created in Outlook for the Offsite tagline 
    object oOffsiteBookmarkObj = oOffsiteBookmark; 

    Word.Document SigDoc = oMsg.GetInspector.WordEditor as Word.Document; // edit the message using Word 

    string bf = oMsg.BodyFormat.ToString(); // determine the message body format (text, html, rtf) 

    // Go to the e-mail signature bookmark, then set the cursor to the very end of the range. 
    // This is where we will insert/remove our tagline, and the start of the new range of text 

    Word.Range r = SigDoc.Bookmarks.get_Item(ref oBookmarkName).Range; 
    object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd; 

    r.Collapse(ref collapseEnd); 

    string[] taglines = GetRssItem(); // Get tagline information from the RSS XML file and place into an array 


    // Loop through the array and insert each line of text separated by a newline 

    foreach (string taglineText in taglines) 
     r.InsertAfter(taglineText + "\n"); 
    r.InsertAfter("\n"); 

    // Add formatting to HTML/RTF messages 

    if (bf != "olFormatPlain" && bf != "olFormatUnspecified") 
    { 
     SigDoc.Hyperlinks.Add(r, taglines[2]); // turn the link text into a hyperlink 
     r.Font.Underline = 0; // remove the hyperlink underline 
     r.Font.Color = Word.WdColor.wdColorGray45; // change all text to Gray45 
     r.Font.Size = 8; // Change the font size to 8 point 
     r.Font.Name = "Arial"; // Change the font to Arial 
    } 

    r.NoProofing = -1; // turn off spelling/grammar check for this range of text 

    object range1 = r; 
    SigDoc.Bookmarks.Add(oOffsiteBookmark, ref range1); // define this range as our custom bookmark 


    if (bf != "olFormatPlain" && bf != "olFormatUnspecified") 
    { 
     // Make the first line BOLD only for HTML/RTF messages 

     Word.Find f = r.Find; 
     f.Text = taglines[0]; 
     f.MatchWholeWord = true; 
     f.Execute(); 
     while (f.Found) 
     { 
      r.Font.Bold = -1; 
      f.Execute(); 
     } 
    } 
    else 
    { 
     // otherwise turn the plain text hyperlink into an active hyperlink 
     // this is done here instead of above due to the extra formatting needed for HTML/RTF text 

     Word.Find f = r.Find; 


     f.Text = taglines[2]; 
      f.MatchWholeWord = true; 
      f.Execute(); 
      SigDoc.Hyperlinks.Add(r, taglines[2]); 
     } 
     r.NoProofing = -1; // disable spelling/grammar checking on the updated range 
     r.Collapse(collapseEnd); 
} 

Répondre

4

Le problème est que Microsoft convertit le « Office HTML "(Je ne suis pas sûr du bon terme) au HTML normal avant qu'il déclenche l'événement ItemSend(), ce qui entraîne la disparition du signet _MailAutoSig. La seule façon de récupérer le signet _MailAutoSig est d'annuler l'événement ItemSend(), puis de déclencher une minuterie pour exécuter une fonction qui va à son tour accéder à l'objet email et le manipuler comme vous voulez, ajouter un utilisateur propriété pour marquer l'e-mail a été traitée, puis envoyer à nouveau l'e-mail.

Par exemple:

 
Dim modItem As Object 'need to hold the item somewhere so the timer can access it 

Sub object_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    If Item.UserProperties.Item("isModded") Is Nothing Then 
     'User has composed a mail and hit "Send", we need to make our modifications to the signature though 
     modItem = item 
     Cancel = True 'cancel the Send so we can make the modifications 
     mytimer.Enabled = True 'fire off a timer to make the modifications 
     Exit Sub 
    Else 
     Item.UserProperties.Item("isModded").Delete 'this flag will keep the email from ping-ponging between ItemSend and the timer 
    End If 
End Sub 

'10 millisecond timer? I think the cancel is almost instant, but experiment 
Sub mytimer_Timer() 
    mytimer.Enabled = False 
    If Not modItem Is Nothing Then 
     modItem.HtmlBody = ...... the signature bookmark will be intact again, so make your modifications ...... 
     modItem.UserProperties.Add("isModded", olText) 'no ping-pong 
     modItem.Send 'send it again 
     modItem = Nothing 
    End If 
End Sub 

je devais faire quelque chose de similaire pour un projet où certains champs Outlook ne sont pas réglés jusqu'à ce que je en cas ItemSend(), donc je forçais l'e-mail à envoyer, obtenir mes informations, puis annuler l'envoi. Ça a bien marché.

Maintenant, cela a été écrit sur le dessus de ma tête, donc je suis sûr que le code ci-dessus ne sera pas parfait, mais il devrait vous donner l'idée de ce qui doit être fait.

+0

cette partie de mon projet est bien passée, mais le code est génial. quand je trouve le temps de le convertir en C# je vais vous dire comment ça se passe. –

+1

Cela peut convertir la plus grande partie en C#: http://www.developerfusion.com/tools/convert/vb-to-csharp/ – Brain2000

+0

Je l'ai utilisé, mais ce n'est pas toujours parfait. –