+ Reply to Thread
Results 1 to 2 of 2

Macro that will prompt for file attachemnt

Hybrid View

romyjames Macro that will prompt for... 09-08-2011, 04:19 PM
mudraker Re: Macro that will prompt... 09-08-2011, 08:19 PM
  1. #1
    Registered User
    Join Date
    08-18-2011
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    1

    Question Macro that will prompt for file attachemnt

    Hi,
    I have a command button on a excel sheet. When i press this button, It copies and pastes the excel sheet onto outlook. However before i send the mail. I want teh code to prompt the user for an attachment. When it does this i want it to open a Insert file dialog box, so user can select the file to attach. Been looking for a while.
    My complete code is as follows:-

    This is a working code:-- I want to add the attachment propmt to this if possible. the attachement Prompt should appear after this worksheet is pasted in outlook.


    Sub SendMailPinpad()
    'Copy excell and send to outlook'
    Worksheets("Pinpad Order").Activate
    Application.ScreenUpdating = False
    Dim OutApp As Object
    Dim OutMail As Object
    Dim FSO As Object
    Dim HTMLcode As String
    Dim HTMLfile As Object
    Dim MyApp As Boolean
    Dim olApp As Object
    Dim Rng As Range
    Dim TempFile As String
    Dim Wks As Worksheet


    Const ForReading As Long = 1
    Const olMailItem = 0
    Const olFormatHTML = 2
    Const UseDefault As Long = -2

    Sheets("Pinpad Order").Visible = True

    If IsMissing("A1:B26") Then
    Set Rng = Selection
    Else
    Select Case TypeName("A1:B26")
    Case Is = "Range"
    Set Rng = Range_To_Send
    Case Is = "String"
    Set Rng = Evaluate("A1:B26")
    Case Else
    MsgBox "Your Selection is Not a Valid Range."
    GoTo Cleanup
    End Select
    End If
    Set Wks = Rng.Parent
    TempFile = Environ("Temp") & "\Email.htm"
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    'Convert the Message worksheet into HTML
    With ActiveWorkbook.PublishObjects.Add( _
    SourceType:=xlSourceRange, _
    FileName:=TempFile, _
    Sheet:=Wks.Name, _
    Source:=Rng.Address, _
    HtmlType:=xlHtmlStatic)
    .Publish (True)
    End With

    'Read the HTML file back as a string
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set HTMLfile = FSO.GetFile(TempFile).OpenAsTextStream(ForReading, UseDefault)
    HTMLcode = HTMLfile.ReadAll
    HTMLfile.Close

    'Clean up the HTML code
    HTMLcode = Replace(HTMLcode, "align=LEFT x:publishsource=", _
    "align=LEFT x:publishsource=")
    Set OutMail = OutApp.CreateItem(olMailItem)
    Sheets("Pinpad order").Visible = False
    On Error Resume Next
    With OutMail
    .from = ""
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = ""
    .BodyFormat = olFormatHTML
    .HTMLBody = HTMLcode
    .Display
    End With
    Cleanup:
    'Delete the Temp File
    If Dir(TempFile) <> "" Then Kill TempFile
    'Delete the Publish Object
    With ActiveWorkbook.PublishObjects
    If .Count <> 0 Then .Item(.Count).Delete
    End With
    'Free memory resources
    Set olApp = Nothing
    Set olEmail = Nothing
    Set FSO = Nothing
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    End Sub
    Last edited by romyjames; 09-13-2011 at 04:51 PM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Macro that will prompt for file attachemnt

    Welcome to the Forum..

    Please read forum rules & add Code Tags to your post
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ 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