Consulting

Results 1 to 9 of 9

Thread: Outlook vbs script editting body

  1. #1

    Outlook vbs script editting body

    Hello All,

    I am trying to use VBS to send 3 emails (using VBS so I can create a bat file that I can run automatically regularly)
    I have a template for the mail in OFT file format (and in RTF mailing format), inside the body I want to change some text and then send the email.

    With this script below I get almost all the required result however, the body of the text appears to now have large line spacing between each line of the body, I am not exactly sure why this happens or how to get around it...

        Set ol = CreateObject("Outlook.Application") 
        Set msg = ol.CreateItemFromTemplate([location of template file]
       
     With msg
          .HTMLBody = Replace(msg.HTMLBody, "insert text", "testing")
          .Display
     End With
    so this preserves the email formatting and allows me to have everything the way I want it to be apart from the increased spacing...
    Does anyone know why?

  2. #2
    is the original body in html format?
    can you delete the extra lines from the email manually?

  3. #3
    The original body is saved in RTF and i dont think I can change it to HTML and save in order to use HTML formatting but I am not sure.

    I am unable to remove the spacing manually because they are not carriage returns but instead appear to be line spacing (but only for the message, not for my signature that is automatically placed at the bottom of the mail)

  4. #4
    try changing to
    .body rather than .htmlbody

    or use the word editor to modify the body

  5. #5
    using .body changes the text but loses all formatting which I want to maintain.

    I am not really sure how to use the word editor to modify the body (I am unsure if this will effect the formatting too)

  6. #6
    (I am unsure if this will effect the formatting too)
    with the word editor, you should have full control to change whatever formatting you want
    you can record macros in word to generate some sample code for editing the document (email body)

    the basics are
    Set msg = ol.CreateItemFromTemplate([location of template file])
    set doc = msg.GetInspector.WordEditor
    if not doc is nothing then
       'use word's properties and methods to edit message
    doc.save
    doc.close
    end if

  7. #7
    so... I still cant get the replace to work..

    within word you can use the following
        With Selection.Find
            .Execute "insert date", , , , , , , , , DateValue(Now() - Weekday(Now(), vbMonday) - 2), wdReplaceAll
        End With
    but when I try to implement as follows:
     Set doc = msg.GetInspector.WordEditor 
     
     If Not doc Is Nothing Then 
      doc.application.selection.find.execute _
        "insert date", , , , , , , , , DateValue(Now() - Weekday(Now(), vbMonday) - 2), wdReplaceAll
     End If
    it just highlights the text rather than replace. any thoughts?

  8. #8
    i found this
    Always use the Selection.Find.ClearFormatting method to clear any previously used option to search for text with specific formatting. The example above is the simplest implementation of Find.Execute to search forward in a document for specific text. The Find.Execute method also supports many optional parameters, which you can look up in the object browser, including those that allow you to replace text or control the direction of the search.
    for further information see msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx#Outlook2007ProgrammingCh17_UsingWordEditor

  9. #9
    The following will replace the text with the date format in your message

    Dim olEmail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    
        On Error Resume Next
        Set olEmail = CreateItemFromTemplate("Template path and name")
        With olEmail
            .BodyFormat = olFormatHTML
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            With oRng.Find
                Do While .Execute(FindText:="InsertDate")
                    oRng.Text = DateValue(Now() - Weekday(Now(), vbMonday))
                    oRng.collapse 0
                Loop
            End With
            .Display
        End With
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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