Consulting

Results 1 to 5 of 5

Thread: Outlook macro - why does the msgbox disappear - and why does it make my script work?

  1. #1
    VBAX Newbie
    Joined
    May 2016
    Posts
    3
    Location

    Outlook macro - why does the msgbox disappear - and why does it make my script work?

    New user, standard apologies, do not code in vba very often.
    I have a macro in Outlook. The purpose of the macro is to paste in the contents of a file and format it nicely as the original text file is in lucida console. The script works, except for some strange behavior.
    The msgbox at the end of the script flashes briefly and disappears.
    I put a debug trace on the msgbox line and ran the script - the paste into the message did not happen, the text did not appear in the message body.
    I added a second msgbox line. The first msgbox disappeared, the second msgbox appeared and I dismissed it, but against the paste into the message did not happen, the text did not appear in the message body.

    (sorry - do not see a 'vba' box as seen in the FAQ)
    code as follows;

    Sub SmokeTestStart()
    Dim OutapP As Object
    Dim OutmaiL As Object
    Dim WorDFi As Object
    On Error Resume Next
    Set wordApp = CreateObject("word.Application")
    Set WorDFi = wordApp.documents.Open("c:\temp\SmokeStart.txt")
        wordApp.Visible = False 'Change it later
        wordApp.Selection.WholeStory
        wordApp.Selection.Font.Name = "Calibri"
        wordApp.Selection.Font.Size = 11
        WorDFi.Content.Copy
    WorDFi.Close SaveChanges:=wdDoNotSaveChanges
    wordApp.Quit
    
    
    Set OutapP = CreateObject("Outlook.Application")
    Set OutmaiL = Application.ActiveInspector.CurrentItem
    With OutmaiL
        strTemp = .Subject
        .Subject = strTemp & "  --- The smoketest has started"
        .Display
        SendKeys " "
        .Display
        SendKeys "+{INSERT}", False
        .Display
    End With
    
    
    Set OutapP = Nothing
    Set OutmaiL = Nothing
    
    
    MsgBox " smoke start message macro completed "
    Last edited by SamT; 05-06-2016 at 12:28 PM. Reason: Added CODE Tags with Editor's # Icon

  2. #2
    If you are working in Outlook as your message indicated, you don't need to create an outlook object as you are already working in one.

    If you want to insert a text file and format it in the body of a message, then opening it in Word is slow and ponderous. The following is much faster. You can then format the range directly after insertring the text e.g.

    Option Explicit
    
    Sub SmokeTestStart()
    Dim OutMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oLink As Object
    Dim oRng As Object
    Dim strFilename As String: strFilename = "c:\temp\SmokeStart.txt"
    Dim strFileContent As String
    Dim iFile As Integer: iFile = FreeFile
    
        Open strFilename For Input As #iFile
        strFileContent = Input(LOF(iFile), iFile)
        Close #iFile
    
        Set OutMail = CreateItem(olMailItem)
        With OutMail
            .To = "someone@somewhere.com"
            .subject = "This is the subject"
            .BodyFormat = olFormatHTML
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            .Display 'This line is required!
            'Insert the text file content
            oRng.Text = strFileContent
            'Format the range
            oRng.Font.Name = "Calibri"
            oRng.Font.Size = 11
        End With
    lbl_exit:
        Set OutMail = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    May 2016
    Posts
    3
    Location
    Thank you so much! That code works fine! Except one very slight issue. This will always be a reply email so I had to change
    Set OutMail = CreateItem(olMailItem)
    to
    Set OutMail = Application.ActiveInspector.CurrentItem

    And when I run the updated code the paste happens just fine except with one issue, the "splitter" line (I'm not sure what you call it) - the line that divides the message from the reply is erased.

    So how can I avoid overwriting that "splitter" line?

  4. #4
    If the message is a reply to the current item then you need the following instead, which creates the reply to the current item:
    Option Explicit
    
    Sub SmokeTestStart()
    Dim olItem As Outlook.MailItem
    Dim OutMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oLink As Object
    Dim oRng As Object
    Dim strFilename As String: strFilename = "c:\temp\SmokeStart.txt"
    Dim strFileContent As String
    Dim iFile As Integer: iFile = FreeFile
        
        On Error GoTo lbl_exit
        Set olItem = ActiveExplorer.Selection.Item(1)
        Open strFilename For Input As #iFile
        strFileContent = Input(LOF(iFile), iFile)
        Close #iFile
    
        Set OutMail = olItem.Reply
        With OutMail
            .BodyFormat = olFormatHTML
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            .Display    'This line is required!
            'Insert the text file content
            oRng.Text = strFileContent
            'Format the range
            oRng.Font.Name = "Calibri"
            oRng.Font.Size = 11
        End With
    lbl_exit:
        Set olItem = Nothing
        Set OutMail = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Newbie
    Joined
    May 2016
    Posts
    3
    Location
    Thanks! That does it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •