Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Sending an email from Word using VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
trayl - 27 Jul 2007 10:58 GMT
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
Graham Mayor - 27 Jul 2007 11:09 GMT
Add the line
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
immediately before End Sub

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> I have used the suggestion option 2 from
> //www.word.mvps.org/FAQs/InterDev/SendMail.htm
[quoted text clipped - 9 lines]
>
> Many thanks
trayl - 27 Jul 2007 15:40 GMT
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

> Add the line
> ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
[quoted text clipped - 13 lines]
> >
> > Many thanks
Graham Mayor - 28 Jul 2007 07:18 GMT
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.

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> brilliant thanks!
>
[quoted text clipped - 50 lines]
>>>
>>> Many thanks
trayl - 30 Jul 2007 09:38 GMT
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?

> The content of the bookmark can be read with
>
[quoted text clipped - 56 lines]
> >>>
> >>> Many thanks
Graham Mayor - 30 Jul 2007 10:13 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> 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
[quoted text clipped - 70 lines]
>>>>>
>>>>> Many thanks
Graham Mayor - 30 Jul 2007 10:29 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> 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
[quoted text clipped - 70 lines]
>>>>>
>>>>> Many thanks
trayl - 30 Jul 2007 10:48 GMT
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

> Ooops! It's a form, so you'll need the extra code to unlock and relock it
> thus:
[quoted text clipped - 108 lines]
> >>>>>
> >>>>> Many thanks
Russ - 30 Jul 2007 10:35 GMT
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

> 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
[quoted text clipped - 60 lines]
>>>>>
>>>>> Many thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.