Automate a Text Form Field

J

jlo

I have created a template using the Forms toolbar. I have a text form field
that the user will type their email address in. When the user is done typing
in their email address, is there a way for this field to launch Outlook and
populate the email address in the To: box so the form can be emailed?

Thanks!
 
G

Graham Mayor

Are you saying that you want the user to mail the document to himself ...or
to you ... or to someone else?
The basic code is:

Sub SendDocumentAsAttachment()
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
If Len(ActiveDocument.Path) = 0 Then 'Document has not been saved
ActiveDocument.Save 'so save it
End If
'see if Outlook is running and if so turn your attention there
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then 'Outlook isn't running
'So fire it up
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Open a new e-mail message
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem 'and add the detail to it
.To = "(e-mail address removed)" 'send to this address
.Subject = "New subject" 'This is the message subject
.Body = "See attached document" ' This is the message body text
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue
.Send
'**********************************
'If you want to view the message before it goes
'change the line above from .Send to .Display
'Otherwise the message is sent straight to the Outbox
'and if you have Outlook set to send mail immediately,
'it will simply be Sent
'with no obvious sign that Outlook has operated.
'Apart from the copy in the Outlook Sent folder
'**********************************
End With
If bStarted Then 'If the macro started Outlook, stop it again.
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub

which will work to send the document to you, if you put your address in the
send to line. If you want to send it to the address the user enters then you
need a couple of extra lines of code

After the line
Dim oItem As Outlook.MailItem
enter
Dim eAdd As String
eAdd =
ActiveDocument.FormFields("Bookmark_Name_of_form_field_with_the_address").Result

and replace
(e-mail address removed)
with
eAdd

(no quotes around aAdd)

You can run the macro on exit from the form field. The only problem you will
have is getting the user to accept the macro. There is no way to force a
user to run macros.

http://www.gmayor.com/installing_macro.htm


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jlo

Another user

Graham Mayor said:
Are you saying that you want the user to mail the document to himself ...or
to you ... or to someone else?
The basic code is:

Sub SendDocumentAsAttachment()
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
If Len(ActiveDocument.Path) = 0 Then 'Document has not been saved
ActiveDocument.Save 'so save it
End If
'see if Outlook is running and if so turn your attention there
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then 'Outlook isn't running
'So fire it up
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Open a new e-mail message
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem 'and add the detail to it
.To = "(e-mail address removed)" 'send to this address
.Subject = "New subject" 'This is the message subject
.Body = "See attached document" ' This is the message body text
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue
.Send
'**********************************
'If you want to view the message before it goes
'change the line above from .Send to .Display
'Otherwise the message is sent straight to the Outbox
'and if you have Outlook set to send mail immediately,
'it will simply be Sent
'with no obvious sign that Outlook has operated.
'Apart from the copy in the Outlook Sent folder
'**********************************
End With
If bStarted Then 'If the macro started Outlook, stop it again.
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub

which will work to send the document to you, if you put your address in the
send to line. If you want to send it to the address the user enters then you
need a couple of extra lines of code

After the line
Dim oItem As Outlook.MailItem
enter
Dim eAdd As String
eAdd =
ActiveDocument.FormFields("Bookmark_Name_of_form_field_with_the_address").Result

and replace
(e-mail address removed)
with
eAdd

(no quotes around aAdd)

You can run the macro on exit from the form field. The only problem you will
have is getting the user to accept the macro. There is no way to force a
user to run macros.

http://www.gmayor.com/installing_macro.htm


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jlo

Thank you Graham for the code. When it tries to run I get the following
error: User-defined type not defined on the line:

Dim oOutlookApp As Outlook.Application
 
G

Graham Mayor

Oops - my deliberate mistake. You must check the Outlook object library in
vba Tools > references.
Apologies for the oversight :(

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jlo

It works!!! Thank you so much!!! How in the world you know this stuff is
beyond me. I appreciate you sharing!!
 

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