5 Replies Latest reply on Nov 3, 2015 11:36 AM by Ivana Kolin

    Email from SW

    Hrituc Alexandru


           I try to make a macro to send mail from sw with outlook...

      How can I add text there?? ...with:  myItem.body = "" disappear signature







      Option Explicit



      Dim swModel As Object

      Dim Model As Object

      Dim swApp As Object

      Dim Msg As String

      Dim URL As String



      Sub Main()

      Dim strSigName As String

        Dim myOlApp As Outlook.Application

         Dim myItem As Outlook.MailItem

          Dim objItem As Object

          Dim objInsp As Outlook.Inspector

          ' requires a project reference to the

          ' Microsoft Word library

          Dim objDoc As Word.Document

          Dim objSel As Word.Selection

          ' requires a project reference to the

          ' Microsoft Office library

          Dim objCB As Office.CommandBar

          Dim objCBP As Office.CommandBarPopup

          Dim objCBB As Office.CommandBarButton

          Dim colCBControls As Office.CommandBarControls

          On Error Resume Next


          Set objInsp = Application.ActiveInspector


          Set swApp = CreateObject("SldWorks.Application")

          Set Model = swApp.ActiveDoc


          If Not objInsp Is Nothing Then

              Set objItem = objInsp.CurrentItem

              If objItem.Class = olMail Then  ' editor is WordMail

                  If objInsp.EditorType = olEditorWord Then

                      ' next statement will trigger security prompt

                      ' in Outlook 2002 SP3

                      Set objDoc = objInsp.WordEditor

                      Set objSel = objDoc.Application.Selection

                      If objDoc.Bookmarks("_MailAutoSig") Is Nothing Then

                          objDoc.Bookmarks.Add Range:=objSel.Range, Name:="_MailAutoSig"

                      End If

                      objSel.GoTo What:=wdGoToBookmark, Name:="_MailAutoSig"

                      Set objCB = objDoc.CommandBars("AutoSignature Popup")

                      If Not objCB Is Nothing Then

                          Set colCBControls = objCB.Controls

                      End If

                  Else ' editor is not WordMail

                      ' get the Insert | Signature submenu

                      Set objCBP = Application.ActiveInspector.CommandBars.FindControl(, 31145)

                      If Not objCBP Is Nothing Then

                          Set colCBControls = objCBP.Controls

                      End If

                  End If

              End If

              If Not colCBControls Is Nothing Then

                  For Each objCBB In colCBControls

                      If objCBB.Caption = strSigName Then

                          objCBB.Execute ' **** see remarks

                          Exit For

                      End If


              End If

          End If


          Set objInsp = Nothing

          Set objItem = Nothing

          Set objDoc = Nothing

          Set objSel = Nothing

          Set objCB = Nothing

          Set objCBB = Nothing


         Set myOlApp = New Outlook.Application

         Set myItem = myOlApp.CreateItem(olMailItem)

         'myItem.SentOnBehalfOfName = "Edson"

         myItem.To = "AAAAAAAAAAAAAAAA"

         myItem.CC = "BBBBBBBBBBBBBBBB"

         myItem.Subject = "CCCCCCCCCCC  " & Model.GetTitle









      End Sub

        • Re: Email from SW
          Ivana Kolin



          myItem.body = "my text"  & vbnewline & myItem.body

            • Re: Email from SW
              Hrituc Alexandru

              if i use myItem.body , my signature disappear

                • Re: Email from SW
                  Simon Turner

                  Here is some .Net code that I use to add signatures to emails. You might be able to convert it to VBA:



                                      If myMessage.Class = Microsoft.Office.Interop.Outlook.OlObjectClass.olMail Then  ' editor is WordMail

                                          Dim objInsp As Microsoft.Office.Interop.Outlook.Inspector


                                          objInsp = oApp.ActiveInspector

                                          If objInsp IsNot Nothing AndAlso objInsp.EditorType = Microsoft.Office.Interop.Outlook.OlEditorType.olEditorWord Then

                                              Dim objDoc As Microsoft.Office.Interop.Word.Document

                                              Dim objSel As Microsoft.Office.Interop.Word.Selection

                                              Dim myRange As Microsoft.Office.Interop.Word.Range

                                              Dim myRangeObject As Object


                                              objDoc = objInsp.WordEditor

                                              objSel = objDoc.Application.Selection

                                              myRange = objDoc.Content


                                              With myRange.Find

                                                  .Forward = True

                                                  .MatchWholeWord = True

                                                  .MatchCase = True

                                                  .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue


                                              End With


                                              myRangeObject = myRange

                                              objDoc.Bookmarks.Add("_MailAutoSig", myRangeObject)

                                              myRange.GoTo(What:=Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark, Name:="_MailAutoSig")

                                          End If

                                      End If

                  • Re: Email from SW
                    Ivana Kolin

                    no guess


                    myItem.To = "AAAAAAAAAAAAAAAA"
                    myItem.CC = "BBBBBBBBBBBBBBBB"
                    myItem.Subject = "CCCCCCCCCCC  " & Model.GetTitle
                    Set objInsp = myItem.GetInspector
                    Set objDoc = objInsp.WordEditor
                    objDoc.Characters(1).InsertBefore "my text"
                • Re: Email from SW
                  Daniel Seaman

                  Hello Hrituc,

                  First let me suggest that the SOLIDWORKS API forum is not the best place to get help with Microsoft Office APIs. You'd probably find this topic discussed in depth within an MS Office forum.


                  It seems that the signature block is considered to be part of the MailItem::Body property. When you add the body of you email you need to append the signature you require. https://msdn.microsoft.com/EN-US/library/office/ff865304.aspx 


                  However, it may make more sense for you to use the MailItem::HtmlBody property. https://msdn.microsoft.com/en-us/library/office/ff868941.aspx


                  For example, this looks like it might be a good solution: http://stackoverflow.com/a/18455148

                  ... among many others on the same discussion thread.


                  I hope one of these works for you.