+ Reply to Thread
Results 1 to 7 of 7

Excel with outlook - attachments

Hybrid View

  1. #1
    Registered User
    Join Date
    02-17-2008
    Posts
    32

    Excel with outlook - attachments

    Hey guys,

    Just having some trouble with some syntax. Basically my excel spreadsheet is sending an email out and I want the clickable button when clicked to ask which attachment should be added, so an open file dialog box.

    I have written this code for the file dialog:

    Sub Get_Data()
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Excel Files *.xls (*.xls),")
    ''
    If FileToOpen = False Then
    
    MsgBox "No file specified.", vbExclamation, "Duh!!!"
    
    Exit Sub
    
    Else
    
    Workbooks.Open Filename:=FileToOpen
    
    End If
    
    End Sub
    And the part of my Code that adds the recipient and attachments is:

    With olMail
            .To = ActiveSheet.Range("A2")
            .Subject = "General and Specific comments attached (Word 2007 Format)"
            .Body = InputBox("Please enter the Full name of the recipient of this email", "Recipient name") & "attached to thi email are your comments regarding your coursework and exam for the module: Database Administration and management."
            .Attachments.Add (Get_Data)
            .Display '.Send
        End With
    Basically, the .Attachments.Add is where I am stuck, you can put a destination path in quotes and that works fine, but I want to run my other sub so that the user can select the file to attach.

    Thanks in advance for any replies, info always greatly appreciated! If anyone has an alternative method I'm open to that also!

    Liam

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Excel with outlook - attachments

    Hello dntel123,

    You need to pass the file string to the Attachments collection object. You can do this buy changing your Sub into a Function.
    Function Get_Data() As String
    
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Excel Files *.xls (*.xls),")
    ''
    If FileToOpen = False Then
    
    MsgBox "No file specified.", vbExclamation, "Duh!!!"
    
    Exit Sub
    
    Else
    
    Workbooks.Open Filename:=FileToOpen
    GetData = FileToOpen
    
    End If
    
    End Function
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    02-17-2008
    Posts
    32

    Excel with outlook - Choosing attachments

    Hiya,

    I have been coding a spreadsheet that can take information from within the sheet and place it into an outlook mail item.

    My Code goes as follows:

    Sub btnSend1_Click()
    
    Dim olApp As Outlook.Application
        Dim olMail As MailItem
        Dim CurrFile As String
    
        Set olApp = New Outlook.Application
        Set olMail = olApp.CreateItem(olMailItem)
    
        ActiveWorkbook.Save
    
        CurrFile = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
    
        With olMail
            .To = ActiveSheet.Range("B2")
            .CC = "test@test.com"
            .Subject = "Enter your Subject here"
            .Body = ActiveSheet.Range("G2").Text & vbCrLf
            .Attachments.Add "path"
            .Display '.Send
        End With
    
        Set olMail = Nothing
        Set olApp = Nothing
        
    End Sub
    This code creates an outlook message and displays it to send. My problem is that when the code gets to:

       .Attachments.Add "path"
    I want it to be a selection instead of typing the path to the file (mainly so that the spreadsheet can be moved from PC to PC).
    So I need an open file dialog for which this code is written:

    Sub Get_Data()
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Excel Files *.xls (*.xls),")
    ''
    If FileToOpen = False Then
    MsgBox "No file specified.", vbExclamation, "Duh!!!"
    Exit Sub
    Else
    Workbooks.Open Filename:=FileToOpen
    End If
    End Sub
    This works fine but what I want to do is integrate it into the above code so that the user is asked to select the attachment to add.

    So Does anybody know how to integrate my 'Get_Data' Sub procedure into the attachment.add command. or another way of doing this?

    Thanks in advance!

  4. #4
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Excel with outlook - Choosing attachments

    Hi,

    do you want the existing excel file to be sent in outlook email or attach someother file?
    VIjay

    If you find any of the post useful to your question, please add to the contributor's reputation by clicking the blue scales icon in the blue bar of the post.

  5. #5
    Registered User
    Join Date
    02-17-2008
    Posts
    32

    Re: Excel with outlook - Choosing attachments

    Hi, thanks for your reply

    I want to attach a new file, such as a word document.

    I'm trying to stay away from:

    .Attachments.Add CurrFile
    Which would add current sheet, instead want the user to be able to browse for whichever file they want, be it on desktop or in my documents

  6. #6
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: Excel with outlook - Choosing attachments

    I dont think, you will need extra code to attach other files.
    once you run the macro and when the outlook is opened, you can just click on the attachment button to attach a file.
    Try the below code in your sheet and once the outlook is opened, just click the attachment option and see what happens.
    Sub SendWithAtt()
        Dim olApp As Outlook.Application
        Dim olMail As MailItem
        Dim CurrFile As String
    
        Set olApp = New Outlook.Application
        Set olMail = olApp.CreateItem(olMailItem)
    
        
        With olMail
            .To = "Your Email Id"
            .CC = "Optional Email ID"
        End With
    
        Set olMail = Nothing
        Set olApp = Nothing
     End Sub

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Excel with outlook - Choosing attachments

    dntel123, do not post duplicates.

    I have merged your two threads on this occasion.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1