Results 1 to 1 of 1

Thread: How to Open a new message in Outlook with a file attached

  1. #1

    Thread Starter
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Arrow How to Open a new message in Outlook with a file attached

    You are free to use all this code any way you want.

    If you have any questions post a reply to this thread
    i have included the llistbox functions as well so you can
    choose a file from a list and send it.. YOu will have to adapt
    it a little for your own purposes.........

    'Collect the Data
    'NOTES:
    'You will need references to the outlook 9 or 10 object library

    '1. Create a form and name it FrmEmailConfig

    2. Add a command button called CmdSendAttached

    3. 'Add a common dialog named Cmdialog
    if the common dialog is unavailabel in the toolbox right click on
    the toolbox and Click on components ...make sure that the
    Selected Items Only Checkbox is "Cleared"
    Scroll down and find microsoft common dialog control and
    select it by setting the check to selected. Clcik on Apply Then
    OK..
    '4. Add a listbox named LstFilesSaved

    ' 5. Add the FxShared bas module to your project (it is in the zip file)

    '6 paste the code below into the general section of your form.


    'Note
    FxShared bas is a bas file i found somewhereor other and have added to over the months... 80% of it is my own work

    VB Code:
    1. Private Sub CmdSendAttached_Click()
    2. 'If any temporary files exist or if the function or program
    3. 'crashes, the temporary files are deleted at startup also.
    4. 'Initialize the send parameters
    5.  
    6. On Error GoTo err_Attach
    7. Dim _
    8. strFname As String, _
    9. strTemp As String, _
    10. i As Integer, _
    11. strFilter As String
    12.  
    13. 'i want to kill outlook
    14. TerminateEXE "OUTLOOK.EXE"
    15.  
    16. 'get the file from the cmdialog
    17. 'i was going to use an API cmdialog but
    18. 'i can't be bothered DOH!
    19.  strFilter = "Archived Order Files (*.txt)|*.txt|" _
    20.             & "HTML Template Files (*.html)|*.html|" _
    21.             & "All Files (*.*)|*.*|" _
    22.             & "HTM Template Files(*.htm)|*.htm|" _
    23.             & "Template Files(*.tpl)|*.tpl|" _
    24.             & "Log Files(*.Log)|*.Log"
    25. 'Open the dialog
    26. strFname = Locate_OpenFile("Please Select a a File To Attach to This Mail.", strFilter, DefOutPutFolder)
    27.  
    28. 'in my temporary files i put an underscore because outlook
    29. 'hates spaces in its attach command line switch
    30.  
    31. If Len(strFname) <> 0 Then
    32.     'Replace the spaces in the filename with the
    33.     'underscore character
    34.     For i = 1 To Len(strFname)
    35.         If Mid(strFname, i, 1) <> Chr(32) Then
    36.             strTemp = strTemp & Mid(strFname, i, 1)
    37.         Else
    38.             strTemp = strTemp & "_"
    39.         End If
    40.     Next i
    41.    
    42.     'Check if the file exists
    43.         If Len(Dir(strTemp)) = 0 Then
    44.             'Create a temporary file
    45.             FileCopy strFname, strTemp
    46.         Else
    47.             'if the temporary file pre-exists delete it.
    48.             Kill strTemp
    49.             'Create a NEW temporary file
    50.             FileCopy strFname, strTemp
    51.         End If
    52.        
    53.     'Open a new email  message with the file attached
    54.     Open_Out_Folder "/a " & strTemp, 1
    55.  
    56. End If
    57. Exit_Attach:
    58. Exit Sub
    59. err_Attach:
    60. MsgBox Err.Description
    61. Resume Exit_Attach
    62. End Sub

    'Add this to your forms general section
    'and Call this at Startup to do what you want

    VB Code:
    1. Sub loadFileList()
    2. On Error GoTo ErrorHandler
    3. 'Image1.Picture = Image2.Picture
    4. Dim SearchPath As String, FindStr As String
    5. Dim FileSize As Long
    6. Dim NumFiles As Integer, NumDirs As Integer
    7. Screen.MousePointer = vbHourglass
    8. SearchPath = DefOutPutFolder
    9. FindStr = "*.txt"
    10.  
    11. 'Locate all the files for the list.
    12. FileSize = FindFiles(SearchPath, FindStr, NumFiles, NumDirs)
    13. Exit_LoadFile:
    14. Screen.MousePointer = vbDefault
    15. Exit Sub
    16.  
    17. ErrorHandler:
    18. MsgBox Err.Description
    19. Resume Exit_LoadFile
    20. End Sub


    This is the FindFiles function called from the sub above
    'what it does is sort thru the folder in the passed varialbes
    'adding filenames found to a list box.

    'P.S i don't feel like indenting this at the correct places

    VB Code:
    1. Public Function FindFiles(path As String, SearchStr As String, _
    2. FileCount As Integer, DirCount As Integer)
    3. Dim FileName As String
    4. Dim DirName As String
    5. Dim dirNames() As String
    6. Dim nDir As Integer
    7. Dim i As Integer
    8. On Error GoTo sysFileERR
    9. If Right(path, 1) <> "\" Then path = path & "\"
    10. nDir = 0
    11. LstFilesSaved.Clear
    12. ReDim dirNames(nDir)
    13. DirName = Dir(path, vbDirectory Or vbHidden)
    14. Do While Len(DirName) > 0
    15.  
    16. If (DirName <> ".") And (DirName <> "..") Then
    17. If GetAttr(path & DirName) And vbDirectory Then
    18. dirNames(nDir) = DirName
    19. DirCount = DirCount + 1
    20. nDir = nDir + 1
    21. ReDim Preserve dirNames(nDir)
    22. End If
    23. sysFileERRCont:
    24. End If
    25. DirName = Dir()
    26. Loop
    27. FileName = Dir(path & SearchStr, vbNormal Or vbHidden Or vbSystem _
    28. Or vbReadOnly)
    29. While Len(FileName) <> 0
    30. FileCount = FileCount + 1
    31.  
    32. 'in my temporary files i put an underscore because outlook
    33. 'hates spaces in its attach command line switch
    34. If InStr(FileName, "_") = 0 Then
    35.  
    36. FindFiles = FindFiles + FileLen(path & FileName)
    37. LstFilesSaved.AddItem FileName
    38. Else
    39.   Kill path & FileName
    40.    
    41. End If
    42. FileName = Dir()
    43.  
    44. Wend
    45. If nDir > 0 Then
    46. For i = 0 To nDir - 1
    47. FindFiles = FindFiles + FindFiles(path & dirNames(i) & "\", _
    48. SearchStr, FileCount, DirCount)
    49. Next i
    50. End If
    51. AbortFunction:
    52. Exit Function
    53. sysFileERR:
    54. If Right(DirName, 4) = ".sys" Then
    55. Resume sysFileERRCont
    56. Else
    57. MsgBox "Error: " & Err.Number & " - " & Err.Description, , _
    58. "Unexpected Error"
    59. 'Resume Next
    60. Resume AbortFunction
    61. End If
    62. End Function





    bindu
    Attached Files Attached Files
    Last edited by binduau; Oct 18th, 2003 at 12:50 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width