none
Multiple Image insert at current insertion point RRS feed

  • Question

  • I'm attempting to add images to a mailitem at the current insertion point.  The problem I'm having is with setting the insertion point for the next image which should be after the prior image insert.  Currently, with the following code, the images are inserted in reverse order.

                                For Each g In grp
                                    Dim strLinkText As Object = g.linktext
                                    Dim strLink As Object = g.weburl
                                    Dim strImageFName As Object = g.imagefilename
                                    Dim strAltImage As Object = g.imagealttext
                                    Dim objSel As Microsoft.Office.Interop.Word.Selection = currentEmail.Windows(1).Selection
                                            objSel.InsertAfter("**insertimage**")
                                            Dim banner As String = String.Format("<p><div align=left><a href=""{0}"" ><img src=""{1}"" alt=""{2}"" hspace=0 align=top border=0></a></div></p>", strLink, strImageFName, strAltImage)
                                                myMailItem.HTMLBody = myMailItem.HTMLBody.Replace("**insertimage**", banner)
                                                currentEmail.Windows(1).Selection.MoveDown(Word.WdUnits.wdParagraph, Word.WdMovementType.wdMove)
                                                '   objSel.Select()
                                                '    objSel.Move(4, 1)
                                Next
    

    I've also tried insertbefore, objsel.select with insertafter, adding <br /> to the banner.   
    After the loop, the new insertion point should be after the last image insert on a new line.

    (Visual Studio 2008 VSTO, Outlook 2007)

    Thanks

    Tuesday, November 15, 2011 5:53 PM

Answers

  • Hi J,

     

    Thanks for posting in the MSDN Forum.

     

    As far as I know, if we use WordEditor we will hard to change anything via HTMLBody property of the item. This snippet will explain what happened. I would recommend use regex to approach your goal.

     

    Imports Microsoft.Office.Tools.Ribbon
    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Ribbon1
    
        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            Dim Item As Outlook.MailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem
            Dim Doc As Word.Document = Item.GetInspector.WordEditor
            Dim Selection As Word.Selection
            Dim Range As Word.Range
            Dim wdApp As Word.Application
    
            Selection = Doc.Application.Selection
            Range = Selection.Range
            Range.InsertParagraphAfter()
            Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            Range.Select()
            Range.InsertBefore("***Text***")
            Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            Range.Select()
            'Doc.Save()
            wdApp = New Word.Application()
            wdApp.Visible = True
            wdApp.Documents.Add().Range().Text = "Original:=>" & Item.HTMLBody
            Item.HTMLBody.Replace("***Text***", "<p><font color='Red'>Hello world!</font><p>")
            wdApp.Documents.Add().Range().Text = "New:=>" & Item.HTMLBody
        End Sub
    End Class
    


     

     

    If you have any questions, please feel free to let me know.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 17, 2011 10:20 AM
    Moderator
  • Hi J,

     

    I don’t think you need to take care of cursor position. If you replace the string via Regex there will not consider the cursor position. And it’s based on my experience it will be hard to approach your goal if you want to use Word Document Model to handle your issue.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, November 29, 2011 9:06 AM
    Moderator

All replies

  • Hi J,

     

    Thanks for posting in the MSDN Forum.

     

    As far as I know, if we use WordEditor we will hard to change anything via HTMLBody property of the item. This snippet will explain what happened. I would recommend use regex to approach your goal.

     

    Imports Microsoft.Office.Tools.Ribbon
    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Ribbon1
    
        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            Dim Item As Outlook.MailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem
            Dim Doc As Word.Document = Item.GetInspector.WordEditor
            Dim Selection As Word.Selection
            Dim Range As Word.Range
            Dim wdApp As Word.Application
    
            Selection = Doc.Application.Selection
            Range = Selection.Range
            Range.InsertParagraphAfter()
            Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            Range.Select()
            Range.InsertBefore("***Text***")
            Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            Range.Select()
            'Doc.Save()
            wdApp = New Word.Application()
            wdApp.Visible = True
            wdApp.Documents.Add().Range().Text = "Original:=>" & Item.HTMLBody
            Item.HTMLBody.Replace("***Text***", "<p><font color='Red'>Hello world!</font><p>")
            wdApp.Documents.Add().Range().Text = "New:=>" & Item.HTMLBody
        End Sub
    End Class
    


     

     

    If you have any questions, please feel free to let me know.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 17, 2011 10:20 AM
    Moderator
  • If I use regex, how do I control the cursor?  I want it to appear after the insert.  I decided to try your suggestion as follows:

                                Dim objSel As Microsoft.Office.Interop.Word.Selection = currentEmail.Windows(1).Selection
                                Dim Range As Word.Range = objSel.Range
    
                                Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                                Range.Select()
                                Range.InsertBefore("***insert***")
                                Range.Select()
                                myMailItem.HTMLBody = Regex.Replace(myMailItem.HTMLBody, "\*\*\*insert\*\*\*", "<div>" & sInsert & "</div>", RegexOptions.IgnoreCase)
    



    where sInsert is created via the Loop: 

    sInsert &= String.Format("<a href=""{0}"" ><img src=""{1}"" alt=""{2}"" hspace=0 align=top border=0></a><br />", strLink, strImageFName, strAltImage)

    Thanks.



    • Edited by J Wagner Monday, November 21, 2011 6:11 AM
    Monday, November 21, 2011 5:54 AM
  • Hi J,

    Do you solved your issue?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, November 25, 2011 6:35 AM
    Moderator
  • Hi Tom,

    No I didn't solve the issue.  I'm having problems with the Cursor positioning after the image insert, as mentioned in my previous message.

    Thanks

    Saturday, November 26, 2011 4:56 PM
  • Hi J,

     

    I don’t think you need to take care of cursor position. If you replace the string via Regex there will not consider the cursor position. And it’s based on my experience it will be hard to approach your goal if you want to use Word Document Model to handle your issue.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, November 29, 2011 9:06 AM
    Moderator