Starting new thread for user forms...

M

Mike

I have developed a user form using Word and the Forms
toolbar.

I would like to insert a macro button in order to send the
completed form back to me. Do I choose the button from
the VB Toolbar (i.e. Command button)? If so, once the
command button is added and I go in to view codes it has a
default code of...

Private Sub CommandButton1_Click()

End Sub

I know that all the users of this form have Outlook.
Where specifically do I add the code within the above code?

I have tried just to copy/paste the code in between the
lines above and the VB Editor drop down windows on top
switch from the Command Button1 and Click, to General and
Declarations. I have selected Outlook library under Tools
Preferences.

What more do I need to do and/or what am I doing wrong.

Mike C.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mike > écrivait :
In this message, < Mike > wrote:

|| I have developed a user form using Word and the Forms
|| toolbar.
||
|| I would like to insert a macro button in order to send the
|| completed form back to me. Do I choose the button from
|| the VB Toolbar (i.e. Command button)? If so, once the
|| command button is added and I go in to view codes it has a
|| default code of...
||
|| Private Sub CommandButton1_Click()
||
|| End Sub
||
|| I know that all the users of this form have Outlook.
|| Where specifically do I add the code within the above code?
||
|| I have tried just to copy/paste the code in between the
|| lines above and the VB Editor drop down windows on top
|| switch from the Command Button1 and Click, to General and
|| Declarations. I have selected Outlook library under Tools
|| Preferences.
||
|| What more do I need to do and/or what am I doing wrong.

Specifically, what is going "wrong"?
e.g.: You click on the button and nothing happens?
Some code executes, but an error is generated?
The code does not compile?

Can you post the code you are using?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Mike

I have tried several times. Most of the time nothing
happens. But a couple of times I received an error which
highlighted the first line in the code (which is the
default code when I added the command button from the VB
toolbar. The code I am using is this....it is from the
website you provided. Knowing that all the users of this
form have Outlook, I chose this method for a test.

Sub SendDocumentInMail()

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
'Set the recipient for the new email
.To = "(e-mail address removed)"
'Set the recipient for a copy
.CC = "(e-mail address removed)"
'Set the subject
.Subject = "New subject"
'The content of the document is used as the body for
the email
.Body = ActiveDocument.Content
.Send
End With

If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing

End Sub

Do you need any other info?

Mike C.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mike > écrivait :
In this message, < Mike > wrote:

| I have tried several times. Most of the time nothing
| happens. But a couple of times I received an error which
| highlighted the first line in the code (which is the
| default code when I added the command button from the VB
| toolbar. The code I am using is this....it is from the
| website you provided. Knowing that all the users of this
| form have Outlook, I chose this method for a test.

The sub declaration line should be:

Private Sub CommandButton1_Click()

Insert the button in the document, access its code window, and between

Private Sub CommandButton1_Click()

End Sub

paste the code you got from the web site, minus the "Sub" line and the "End
Sub" line:

'_______________________________________
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
'Set the recipient for the new email
.To = "(e-mail address removed)"
'Set the recipient for a copy
.CC = "(e-mail address removed)"
'Set the subject
.Subject = "New subject"
'The content of the document is used as the body for
the email
.Body = ActiveDocument.Content
.Send
End With

If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
'_______________________________________

Try that and see how it goes. It should work.
Don't forget to switch off the design mode before testing. It should work
even if the form is protected.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Mike

I did as you instructed. The first time I received an
undeliverable message...could not
find "(e-mail address removed)". So I inserted my email address
in this space in the code. I tried it again and this time
received an email with nothing in the body, just a blank
page.

Also, I noticed in the code that there is an option for
CC. What if I do not want that option? Do I eliminate
that line of code?

Let me know if you need any other info.

Thanks again for your patience.

Mike C.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mike > écrivait :
In this message, < Mike > wrote:

| I did as you instructed. The first time I received an
| undeliverable message...could not
| find "(e-mail address removed)". So I inserted my email address
| in this space in the code. I tried it again and this time
| received an email with nothing in the body, just a blank
| page.

What was the content of the document?
Had it been saved?

| Also, I noticed in the code that there is an option for
| CC. What if I do not want that option? Do I eliminate
| that line of code?

Yes.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Mike

What was the content of the document?
What do you mean? The document is the user form is it not?
Had it been saved?
At first I did not save it. But I have done it several
times after saving it with the same result.

What I have been doing is emailing the user form to myself
as an attachment. I then open up the attachment, fill out
the user form, hit save, then click on the button to send
it. I them receive the email with a blank page.

Mike C.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mike > écrivait :
In this message, < Mike > wrote:

||| What was the content of the document?
|| What do you mean? The document is the user form is it not?

Just asking in case you were texsting on a different document.
By the way, if you are dealing with a protected document that has textboxes
an such fields for the user to enter data, this is called an in-line form,
not a userform. A userform is like a dialog box.
||
||| Had it been saved?
|| At first I did not save it. But I have done it several
|| times after saving it with the same result.
||
|| What I have been doing is emailing the user form to myself
|| as an attachment. I then open up the attachment, fill out
|| the user form, hit save, then click on the button to send
|| it. I them receive the email with a blank page.

Ah! We are getting there!
I see the problem. You are sending the document as the body of the email,
but it is a protected form... No can do!

You have to send it as an attachment, and, also, if the user does not save
it, you will get your form as an attachment back, but none of the user's
answers will be there. The web page where you got your code also has some
code to send the current document as an attachment, but in your case, you
have to make sure that it is saved first.
So here is the code with the save business taken care of. If you want to get
fancy, you could ask the user where he wants to save it and let him/her do
it. In this example, it gets saved automatically in a hidden folder under
the Temporary Internet File folder (Usually the OLK2C folder, but on a
network in a corporation it could be somewhere else); that is when you open
the attachment directly and try to send it as you did in your tests.
It would be better if the user saved the attachment to a folder before
opening it. You might want to recommend that they do that when you send the
attachment. This way, their answers will be saved where ever they decided to
save the attachment before opening it.

'_______________________________________
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

ActiveDocument.Save

Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "(e-mail address removed)"
.Subject = "New subject Test"
'Add the document as an attachment, you can use the .displayname
property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _
DisplayName:="Document as attachment"
.Send
End With

If bStarted Then
oOutlookApp.Quit
End If

Set oItem = Nothing
Set oOutlookApp = Nothing
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Mike

I copied the code. I emailed the document to myself. I
saved the document to my desktop. I then opened it on the
desktop, filled it out and saved it again. I then clicked
the "send button" and the Microsoft Visual Basic Window
opened with the message..."Compile Error: Syntax Error"
the word "property" in the coded is highlighted....(line 4)

..To = "(e-mail address removed)"
.Subject = "New subject Test"
'Add the document as an attachment, you can use
the .displayname
property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName,
Type:=olByValue, _
DisplayName:="Document as attachment"
.Send

After clicking OK in the VB window the very first line is
highlighted in yellow with a yellow arrow on the left
margin pointing to the first line....

Private Sub CommandButton1_Click()

This brings a question to mind....Will this code save and
then send the completed in-line form as an attachment? If
so, would it be better to use the routing method?

Mike C.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mike > écrivait :
In this message, < Mike > wrote:

| I copied the code. I emailed the document to myself. I
| saved the document to my desktop. I then opened it on the
| desktop, filled it out and saved it again. I then clicked
| the "send button" and the Microsoft Visual Basic Window
| opened with the message..."Compile Error: Syntax Error"
| the word "property" in the coded is highlighted....(line 4)

This is because the word "property" was wrapped to a new line in your email
program. It should be at the end of the preceding line. Before
saving/running the code, go to the Debug menu and run "Compile" to make sure
all the code is OK.

|
| .To = "(e-mail address removed)"

Aaaaaarghhhh! I just realized that I had forgotten to change my email
address to something anonymous before posting my last message!
/"$/$/?%/$/$%?$%??*"$"%*& to say the least!
<wishful thinking>
Hope there were no robots around...
<end wishful thinking>

| .Subject = "New subject Test"
| 'Add the document as an attachment, you can use
| the .displayname
| property
| 'to set the description that's used in the message
| .Attachments.Add Source:=ActiveDocument.FullName,
| Type:=olByValue, _
| DisplayName:="Document as attachment"
| .Send
|
| After clicking OK in the VB window the very first line is
| highlighted in yellow with a yellow arrow on the left
| margin pointing to the first line....
|
| Private Sub CommandButton1_Click()
|
| This brings a question to mind....Will this code save and
| then send the completed in-line form as an attachment? If

Yes.

| so, would it be better to use the routing method?
|

After doing all this work, you want to throw it away and start over :-(
Just kidding!

Honestly, I do not know the impact of Routing on a protected form just open
directly from Outlook and that has been modified.
You may want to test both options and choose the one you like best according
to your environment and needs.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Mike

In my haste to try it out, I inadvertently sent you a
completed in-line form. Well, I changed the email and it
worked. I believe this method will work better than the
Routing method.

Thank you for your patience and not giving up on me even
when I wanted to.

Mike C.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Mike > écrivait :
In this message, < Mike > wrote:

| In my haste to try it out, I inadvertently sent you a
| completed in-line form. Well, I changed the email and it

Yeah, I saw that. My fault for being a nitwit and posting code with an
actual email in it!

| worked. I believe this method will work better than the
| Routing method.

You're the boss!

|
| Thank you for your patience and not giving up on me even
| when I wanted to.
|

Glad you kept at it. You have to whip Word into shape, not let Word whip you
around!
Good luck!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

SMac

Jean,

I copied the following code because I wanted to do the same thing as Mike.
When I tested the button nothing happen so I went into the code and compiled
it and it doesn't like: oOutlookApp As Outlook.Application, it gave me the
error "Compile Error: User-defined type not defined".
Does it not like Outlook.Application? Any help would be MUCH appreciated.

Thanks!
 
D

Doug Robbins

Later post answered.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

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