PDA

View Full Version : Solved: Command Button to send email...



junglej815
12-27-2007, 11:07 AM
Hello, I am aware that there are many posts just like this and I have searched all over this website and I found some info to help but not quite all the way there....

I have a Word Document in Word 2003, saved as a .doc file. It has a Command Button on it that is acting as a SUBMIT button. I would like to have the Word Document attached to a new mail message in Outlook with my email address inserted automatically when that command button is clicked.

I originally was able to make it so that the document was at least attached by using this code:

Private Sub CommandButton1_Click()
Application.options.ButtonFieldClicks = 1
Options.SendMailAttach = True
ActiveDocument.Sendmail
End Sub

While Looking through this forum site I did find code that will attach the document as well as insert my email address, this code being this:

Option Explicit
Sub eMailActiveDocument()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
Doc.Save
With EmailItem
.Subject = "Whatever the subject is"
.Body = "" & vbCrLf & _
"" & vbCrLf & _
""
.To = myemail@myemail.com
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
.Attachments.Add Doc.FullName
.Display
End With

Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub

How can I make it so that the document is attatched and my email inserted in a new mail message in Outlook by clicking on the Command Button?? The code above does technically work, but only if I go into Tools > Macros > Macros> Double click the macro. It attaches and inserts what I want...I just need to figure out/know how to make it so that the command button does it when clicked.

Thanks in advance for assistance.

fumei
12-27-2007, 11:34 AM
Unless I am not understanding something (always possible) just put the code that does work in the CommandButton_Click procedure.Private Sub CommandButton1_Click()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
...etc. etc.

OR, call the Sub that works, from the CommandButton.Private Sub CommandButton1_Click()
Call eMailActiveDocument

junglej815
12-27-2007, 11:47 AM
Hey Fumei,

Thanks for getting back to me on this. I have very little experience with VB coding....I was trying all different variations and it doesn't do anything when you go back to Word and Click the Command Button

lucas
12-27-2007, 12:02 PM
Here it is attached....using Gerry's suggestion......

why use a button though see here (http://slucas.virtualave.net/Wink/CustomMenuItem.htm).

junglej815
12-27-2007, 12:18 PM
Hey Everyone,

I've seen a lot of people looking for this type of thing where a Command Button attaches a document as well as inserts the Send To email address...After I don't know how many days and so much searching through forums I figuerd it out and thought others would like to know. The code for this is...

Public Sub commandbutton1_click()
Dim OL As Object
Dim EmailItem As Object
Dim Doc As Document
Application.ScreenUpdating = False
Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)
Set Doc = ActiveDocument
Doc.Save
With EmailItem
.Subject = "Whatever the subject is"
.Body = "" & vbCrLf & _
"" & vbCrLf & _
""
.To = "emailaddress@emailaddress.com"
.Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
.Attachments.Add Doc.FullName
.Display
End With
Application.ScreenUpdating = True
Set Doc = Nothing
Set OL = Nothing
Set EmailItem = Nothing
End Sub

fumei
12-27-2007, 01:09 PM
"I was trying all different variations and it doesn't do anything when you go back to Word and Click the Command Button"

And exactly what variations were those???

AND:

"I figuerd it out "

Well, did you now? Really? Good for you, although, ummm, seems like you did exactly what I suggested....

Steve...very cute demo.

junglej815
12-27-2007, 01:23 PM
Sorry fumei.....I didn't mean it to sound like i was taking credit away from you or anything...you certainly gave me all the coding that i needed and i thank you very much...i tried putting the code you gae me under the private sub commandbutton...and it didnt work, i tried the calleactivedocument and it didn't work, thats what i meant by trying it in different things...I looked in procedures in the VBA window and saw that there was a Public sub as well as a Private sub....the code wasn't working in anything that was labeled as a Private sub so I tried a Public sub and it worked. and yeah, i noticed lucas put a post on here with exactly what i needed right after i posted that correct coding....didn't mean to **** anyone off, geeze.

lucas
12-27-2007, 02:08 PM
Didn't seem to help much Gerry......guess jungle got what was needed so maybe he should mark his thread solved using the thread tools at the top of the page.

fumei
12-27-2007, 02:28 PM
jungle. I was not **** off, except to the point that you were not clear.

and...geeeze...if you look at Steve's code...it is Private, not Public. So your comment re: Private vs Public is not correct.

"...i tried putting the code you gae me under the private sub commandbutton...and it didnt work,"

well gosh jungle...it DOES work. It works as Private in Steve's example.

It would NOT work depending on where you had the code...but as you do not actually say anything about that, it is impossible to comment.

Further, when you make posts with:

"It has a Command Button on it that is acting as a SUBMIT button."

I would suggest you be a little more specific. When mentioning things like "commandbutton" it is very useful to us to understand exactly what KIND of commandbutton, and where it is. Steve made an assumption - that seems to be correct - that "commandbutton" meant an ActiveX control in the document itself. However, it could have meant a macrobutton. It could have mean a commandbutton on a userform. In which case the Private/Public issue would be quite viable.

Steve's code (as he assumed it was an ActiveX commandbutton) is in the ThisDocument module. Which is, generally, the proper place to put ActiveX control code. Which is why Private Sub CommandButton1_Click() works.

If that EXACT same code was in a standard module, it would not work. In fact, it would not work whether it was Private or Public.

It may help to read up on Scope. It is a subject that is sorely lacking in general understanding.

junglej815
12-27-2007, 02:30 PM
Thanks for your assistance Lucas. If i saw your response before I found the public sub in the vb window of word, you would have given me everything i exactly needed. Like I said, any help I can get is greatly appreciated...and thank you for telling me how to to mark that the thread was solved, I was wondering how to do so, since I am new to the forum.

Again, sorry if I upset anyone...I didn't mean it...sorry for being a newb with no experience with VB and not knowing anything really about activex controls or anything