PDA

View Full Version : Email from userform



terryvanduze
01-13-2009, 12:48 PM
Hello
I have a userform that I placed a button on (actually a label dressed up to look like a command button).

In the Click code, I would like it to bring up the default email client on the users' computer.

I do not need specific subject, but the email address will be hard-coded.

Any suggestions?

Thank you
Terry

lucas
01-13-2009, 12:53 PM
Is it specifically outlook?

terryvanduze
01-13-2009, 12:59 PM
It could be outlook, outlook express or any other email client.
I am creating a VBA resume, I could not find a way to have the email hyperlink placed on specific text in a textbox, so I created a button to click.

The objective is to have a prospective employer click the button and email directly from the userform. So the email client used would be the default email client on that person's computer.

Thank you
Terry

CreganTur
01-13-2009, 01:22 PM
This is some pretty advanced code- it makes use of the WIN API ShellExecute() method to execute the MailTo method, which will launch the default e-mail app on the user's machine.

To send the e-mail you need to call the SendEmail sub and provide values for the parameters. Here's an example of how to call the sub:

SendEmail "you@hotmail.com", "This is the subject line", "This is the e-mail body."



Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _

ByVal lpOperation As String, _

ByVal lpFile As String, _

ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_SHOW As Long = 5


Sub SendEmail(strTo As String, strSubject As String, strBody As String)
Dim rc As Long
Dim strFile As String
'build the lpFile argument
strFile = "Mailto:" & strTo
strFile = strFile & "?subject=" & strSubject
strFile = strFile & "&body=" & strBody
rc = ShellExecute(0, "open", strFile, "", "", SW_SHOW)
End Sub


HTH:thumb

lucas
01-13-2009, 01:32 PM
I think you will run into problems sending a document with macro's to prospective employers. Most offices will not allow macro's to be run by their own office people, let alone one that has arrived in the inbox......

Dragon71
03-23-2009, 02:26 PM
Is there a trick to taking this code and adding it to a command button??
Tried just adding it in the below to link to a command button,but it does not seem to work??



Private Sub Email_Click()
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _


ByVal lpOperation As String, _

ByVal lpFile As String, _

ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_SHOW As Long = 5



Sub SendEmail(strTo As String, strSubject As String, strBody As String)
Dim rc As Long
Dim strFile As String
'build the lpFile argument
strFile = "Mailto:" & strTo
strFile = strFile & "?subject=" & strSubject
strFile = strFile & "&body=" & strBody
rc = ShellExecute(0, "open", strFile, "", "", SW_SHOW)

End Sub

fumei
03-23-2009, 03:40 PM
"I do not need specific subject, but the email address will be hard-coded."

If this is correct, then there is no need for all that elaborate coding. Here is the entire coding for a Label_Click.

Private Sub lblEmailMe_Click()
ActiveDocument.FollowHyperlink _
Address:="mailto:gerry.knight@yaddayadda.whatever.ca"
End Sub


That's it. No need to Shell and all that other stuff. It does not matter what the client is. Whatever is the default, that is what MailTo will bring up.

You can of course append anything as the address string...it IS a string.


Private Sub lblEmailMe_Click()
ActiveDocument.FollowHyperlink _
Address:="mailto:" & some_string_variable
End Sub

fumei
03-23-2009, 03:44 PM
"Is there a trick to taking this code and adding it to a command button??"

Have you changed something? Originally, you wrote:

"I have a userform that I placed a button on (actually a label dressed up to look like a command button)."

Have you changed this to a real commandbutton? Not that it matters really...

fumei
03-23-2009, 03:47 PM
As for your question regarding the placement of the other code - if you wish to use it - the answer is you have the chunks all out of order. The _Click event should call Sub SendEmail.

fumei
03-23-2009, 03:54 PM
As a possibly amusing aside here is a wee procedure for displaying an inputbox asking for what you would like to search Google for. Type something in, hit Enter, and voila you get your default browser with the result of that Google search. Right from Word.
Sub Google()
' keyboard shortcut = Alt-g
Dim strIn As String
strIn = InputBox("Search for?", "Open a Google Search")
If Documents.Count = 0 Then Documents.Add
ActiveDocument.FollowHyperlink _
Address:="http://www.google.com/search?hl=en&q=" & _
strIn, NewWindow:=True
End Sub
I have this using Alt-g as the keyboard shortcut. So, Alt-g, type something, hit Enter and...a browser with the Google result.

Again, there is no need to use API, or a ShellExecute. VBA and Windows understand basic hyperlink address usage. MailTo means execute the default mail client. HTTP means execute the default browser.

NOTE: you MUST, repeat MUST, have a document existing (Documents count can not be 0). Thus the line

If Documents.Count = 0 Then Documents.Add

How else could one use ActiveDocument...unless there IS an active document.

Dragon71
03-23-2009, 04:28 PM
How would i get it to save and attach the current document to the email??

fumei
03-24-2009, 11:26 AM
This is an entirely different issue. You did not mention doing an attachment. A plain mailto will not work. That just brings up a default message container. You will need to work with the email client.

Do a search, there are a number of places that cover this, at least if it is Outlook.