Sending an email from Word using VBA

T

trayl

I have used the suggestion option 2 from
//www.word.mvps.org/FAQs/InterDev/SendMail.htm
to create an email from the content of the form in Word and send an email
based on the contents of this form. Is there any way that once it has sent
the email it can close Word and not save the active document?

The form is filled in by the user and then they click on Send (command
button) that takes the content of that form and sends it off in an email.
However, I’d like Word to then automatically close that form and not save the
changes – is this at all possible?

Many thanks
 
G

Graham Mayor

Add the line
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
immediately before End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

trayl

brilliant thanks!

i have another question now though! i have code in my form that copies the
results in the form fields to put into the email as follows:
strbody = "NBSC Attendee(s): " &
ActiveDocument.FormFields("Attendees").Result

with strbody being the body of the email. This gives each line in the email
a heading (NBSC attendee(s):) then takes the entry in the formfield and puts
that next to it. However, I also have another part of the document that is a
bookmark called Booking Reference which is created from AutoNew to give each
document a sequential number (as per
http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm). As this isn't a
FormField but just a bookmark, the above code doesn't work.

My question is : how do I write the code under strbody to put the bookmark
created for the sequential number into the email. I've managed to get the
title "Booking Reference" into my email but it doesnt give the sequential
number that has been generated. Is there any way to do this?

I assume is something like strbody = "Booking reference : " &
ActiveDocument.bookmarks
but then i'm stuck.

many thanks
 
G

Graham Mayor

The content of the bookmark can be read with

ActiveDocument.Bookmarks("Order").Range

Order being the name of the bookmark from the quoted web page.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

trayl

thanks, i've tried that though and all i get is a blank, it still doesnt read
the number. Is that because the bookmark is generated by the automatic
number. does anyone know if there is any way of doing this?
 
G

Graham Mayor

I see the problem. The macro you have used writes the number next to the
bookmark and not in it. A simple solution is to insert a space at the place
the macro is to write the number and bookmark that - let's assume 'Order' as
that's waht the original macro used. Then for your numbering macro, you need
a minor mod., to write the number *in* the bookmark. You can then read the
bookmark as I originally suggested.

Sub AutoNew()
Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Order") = Order

With Selection
.GoTo What:=wdGoToBookmark, Name:="Order"
.MoveLeft Unit:=wdCharacter, Count:=1
.TypeText Format(Order, "00#")
End With

'MsgBox ActiveDocument.Bookmarks("Order").Range

ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")
End Sub
 
G

Graham Mayor

Ooops! It's a form, so you'll need the extra code to unlock and relock it
thus:

Sub AutoNew()
Dim bProtected As Boolean

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If

Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Order") = Order

With Selection
.GoTo What:=wdGoToBookmark, Name:="Order"
.MoveLeft Unit:=wdCharacter, Count:=1
.TypeText Format(Order, "00#")
End With
'MsgBox ActiveDocument.Bookmarks("Order").Range
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Russ

Trayl,
<http://word.mvps.org/faqs/macrosvba/WorkWithBookmarks.htm>
I think you created a placeholder bookmark, so the number was inserted after
the bookmark. One way to handle this is to begin by making a bookmark of a
space character to make an enclosing bookmark, then the number will be
inserted at the beginning of the bookmark and the Trim() function will
delete the space when you use the number:
Trim(ActiveDocument.Bookmarks("Order").Range.Text)
Or
Expand the placeholder bookmark after the number is inserted with:
ActiveDocument.Bookmarks("Order").Range.Expand
'(default is wdWord)
'then use
ActiveDocument.Bookmarks("Order").Range.Text
 
T

trayl

thank you so so much! i had to change my placeholder bookmark to an
enclosing bookmark (once i'd figured out i had to do that) and it now works
wonderfully!! thank you very very much - you are a genius!
Tracey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top