PDA

View Full Version : Outlook vbs script editting body



penfold1992
08-21-2014, 06:08 AM
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?

westconn1
08-21-2014, 02:27 PM
is the original body in html format?
can you delete the extra lines from the email manually?

penfold1992
08-21-2014, 07:50 PM
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)

westconn1
08-22-2014, 02:40 AM
try changing to
.body rather than .htmlbody

or use the word editor to modify the body

penfold1992
08-22-2014, 04:47 AM
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)

westconn1
08-23-2014, 01:10 AM
(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

penfold1992
08-26-2014, 07:17 AM
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?

westconn1
08-28-2014, 02:48 AM
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

gmayor
08-28-2014, 09:52 PM
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