Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Problem auto generating e-mail to several recipients - argument not optional error message

Hello,

In a work sheet, I have a list of e-mail addresses. I am trying to
write the code for a button that sends an e-mail, with all the
addresses in the list as recipients. What the code basically does is
that a for statement runs through the list and adds the recipients via
the Set objRecipient command. I keep getting the "argument not
optional" error message, pointing to the row with the Set objRecipient
command. The only code I've written myself is the for statement, the
rest is ripped off this forum. Could anyone of you VBA gurus have a
look through the code and give me some suggestions as to how to fix
it. The for statement itself is probably not excellently written, so
if you have any input on how to improve it or make it neater, please
share.

Is it possible to send a hyperlink to the workbook (or even better to
a specific worksheet in the workbbok) in the body of the e-mail? The
application will be posted on a company intranet, so the link should
work.

Thanks alot
Nils

Private Sub CommandButton2_Click()
Dim objOutlook As Object
Dim objMailItem As Object
Dim objRecipient As Object
Dim objNameSpace As Object
Dim i As Integer
Dim cell As Range


Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNameSpace("MAPI")
objNameSpace.Logon , , True


Set objMailItem = objOutlook.CreateItem(0)

For i = 8 To 19 ' The e-mail
list is in the range D8-D19

Set cell = Range("D" & i)
cell.Select

If IsEmpty(ActiveCell) Then ' Checks if the
cell is empty
GoTo nextLine
End If

Set objRecipient = objMailItem.Recipients.Add.cell.Value '
This is where I get "Argument not optional"
objRecipient.Type = 1

nextLine:

Next i

objMailItem.Subject = "Subject"
objMailItem.Body = "Body" ' Is it possible to
attach a link to this workbook here?
objMailItem.Send

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Problem auto generating e-mail to several recipients - argument no

There is an erratum in your code. Replace the line

Set objRecipient = objMailItem.Recipients.Add.cell.Value

with this one

Set objRecipient = objMailItem.Recipients.Add(cell.Value)

" wrote:

Hello,

In a work sheet, I have a list of e-mail addresses. I am trying to
write the code for a button that sends an e-mail, with all the
addresses in the list as recipients. What the code basically does is
that a for statement runs through the list and adds the recipients via
the Set objRecipient command. I keep getting the "argument not
optional" error message, pointing to the row with the Set objRecipient
command. The only code I've written myself is the for statement, the
rest is ripped off this forum. Could anyone of you VBA gurus have a
look through the code and give me some suggestions as to how to fix
it. The for statement itself is probably not excellently written, so
if you have any input on how to improve it or make it neater, please
share.

Is it possible to send a hyperlink to the workbook (or even better to
a specific worksheet in the workbbok) in the body of the e-mail? The
application will be posted on a company intranet, so the link should
work.

Thanks alot
Nils

Private Sub CommandButton2_Click()
Dim objOutlook As Object
Dim objMailItem As Object
Dim objRecipient As Object
Dim objNameSpace As Object
Dim i As Integer
Dim cell As Range


Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNameSpace("MAPI")
objNameSpace.Logon , , True


Set objMailItem = objOutlook.CreateItem(0)

For i = 8 To 19 ' The e-mail
list is in the range D8-D19

Set cell = Range("D" & i)
cell.Select

If IsEmpty(ActiveCell) Then ' Checks if the
cell is empty
GoTo nextLine
End If

Set objRecipient = objMailItem.Recipients.Add.cell.Value '
This is where I get "Argument not optional"
objRecipient.Type = 1

nextLine:

Next i

objMailItem.Subject = "Subject"
objMailItem.Body = "Body" ' Is it possible to
attach a link to this workbook here?
objMailItem.Send

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Problem auto generating e-mail to several recipients - argument no

vbapro,

Yes, that did the trick. Thank you!

Any ideas how I can attach the link to this workbook in the body text?
Is there a command that captures the address to the active excel
workbook?

Thanks for your help

//Nils

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Problem auto generating e-mail to several recipients - argumen

What address (or kind of link) of the workbook you mean? Its full path on
your computer can be obtained from ActiveWorkbook.FullName. If you would like
the file to be accessed in a network or via Internet then you should add the
network address to the file name.

objMailItem.Body = ActiveWorkbook.FullName

Another way is to send the whole file, for doing this just add these lines
before sending:

ActiveWorkbook.Save
objMailItem.Attachments.Add ActiveWorkbook.FullName


" wrote:

vbapro,

Yes, that did the trick. Thank you!

Any ideas how I can attach the link to this workbook in the body text?
Is there a command that captures the address to the active excel
workbook?

Thanks for your help

//Nils


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Problem auto generating e-mail to several recipients - argumen

What address (or kind of link) of the workbook you mean? Its full path on
your computer can be obtained from ActiveWorkbook.FullName. If you would like
the file to be accessed in a network or via Internet then you should add the
network address to the file name.

objMailItem.Body = ActiveWorkbook.FullName

Another way is to send the whole file, for doing this just add these lines
before sending:

ActiveWorkbook.Save
objMailItem.Attachments.Add ActiveWorkbook.FullName


" wrote:

vbapro,

Yes, that did the trick. Thank you!

Any ideas how I can attach the link to this workbook in the body text?
Is there a command that captures the address to the active excel
workbook?

Thanks for your help

//Nils




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Problem auto generating e-mail to several recipients - argumen

vbapro,

Thanks for your time and speedy responses!

The file will be published on a private intranet and I think that the
ActiveWorkbook.FullName will suffice. I've tried it out and it works
fine. When the file is published on the intranet,
ActiveWorkbook.FullName captures the full network address.

Is it somehow possible to make the path into a hyperlink, just showing
the file name, and attach it to the e-mail?. I have managed to paste
the file name as a hyperlink in a worksheet by using the code below,
but I can't manage to get the link into the e-mail body text. Any
ideas?

Dim FName As Variant
Dim sStr As String
Dim iPos As Long

FName = ActiveWorkbook.FullName

iPos = InStrRev(FName, Application.PathSeparator)
sStr = Mid(FName, iPos + 1)

ActiveCell.Hyperlinks.Add _
Anchor:=ActiveCell, _
address:=FName, _
TextToDisplay:=sStr

//Nils

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Problem auto generating e-mail to several recipients - argumen

Instead of objMailItem.Body use the following two lines

objMailItem.BodyFormat = olFormatHTML
objMailItem.HTMLBody = "<HTML<BODY<A HREF=""file://" &
ThisWorkbook.FullName & """Catch the file :)!</A</BODY</HTML"

" wrote:

vbapro,

Thanks for your time and speedy responses!

The file will be published on a private intranet and I think that the
ActiveWorkbook.FullName will suffice. I've tried it out and it works
fine. When the file is published on the intranet,
ActiveWorkbook.FullName captures the full network address.

Is it somehow possible to make the path into a hyperlink, just showing
the file name, and attach it to the e-mail?. I have managed to paste
the file name as a hyperlink in a worksheet by using the code below,
but I can't manage to get the link into the e-mail body text. Any
ideas?

Dim FName As Variant
Dim sStr As String
Dim iPos As Long

FName = ActiveWorkbook.FullName

iPos = InStrRev(FName, Application.PathSeparator)
sStr = Mid(FName, iPos + 1)

ActiveCell.Hyperlinks.Add _
Anchor:=ActiveCell, _
address:=FName, _
TextToDisplay:=sStr

//Nils


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Problem auto generating e-mail to several recipients - argumen

I get an error message: "Invalid procedure call or argument" on the
line "objMailItem.BodyFormat = olFormatHTML "

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Problem auto generating e-mail to several recipients - argumen

If I skip objMailItem.BodyFormat = olFormatHTML, the code executes
perfectly!

Thank you vbapro!

//Nils

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compile error: Argument not optional ilia Excel Programming 0 December 13th 06 02:57 PM
Compile Error Argument Not optional [email protected] Excel Discussion (Misc queries) 1 August 16th 06 04:58 PM
Argument not optional Error 449! Need Help bad_boyu Excel Programming 3 July 26th 06 12:52 PM
Compile Error: Argument not optional Brett Smith[_2_] Excel Programming 1 January 19th 06 05:39 PM
error message: compile error, argument not optional Pierre via OfficeKB.com Excel Programming 3 September 5th 05 03:45 PM


All times are GMT +1. The time now is 10:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"