• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 955
  • Last Modified:

Outlook and Visual Basic 6.0 PDF file

I need to create and send an email using VB 6.0 code through outlook. I have 2 standard message bodies that I have to send out depending on the criteria.
One message body is an overdue message the other is credit message.

So my question how do I use VB to create an outlook message then select one of the message texts to put in the body of the message. Both messages content has to be accessible to the user to edit. Can the 2 messages be contained in somewhere in Outlook, Business Contact Manager or as a Word Doc?

I also need to attach a pdf file.
  • 2
1 Solution
Chris Raisin(Retired Analyst/Programmer)Commented:
Stand By....

Chris Raisin(Retired Analyst/Programmer)Commented:
Is this the sort of thing you are after?

Create a folder on your PC called (say) "C:\Messages"

In the folder create two text files, one called "Msg_Credit.txt" and one called "Msg_Overdue.txt"

In these text files the following text appears:
                               This is a CREDIT message with one attachment!
                               <<subject=Credit Message


                             This is an Overdue Message with TWO attachmentS
                             <<subject=Overdue Message

(Notice that the order of the lines starting with "<<" do not matter)

Now place two PDF files in the "C:\Messages" folder and name them MyPDF1.pdf and MyPDF2.pdf (or change the code to reflect their real names).

Note that the files can reside anywhere but you will need to amend the code to point to their correct location.( I hope this all makes sense to you).

Then place the following code in your VBA Editor and run the macro:

A pop-up box should appear asking you to whether you want to send an "Overdue" or a "credit" email (it does not matter what you enter, as long as the first letter is either "O" or "C" and it does not matter whether it is upper-case or lower-case).

The best answer is simply "O" or "C" then press enter.

The code will create an email with the text from the appropriate msgfile in the body of the email and with any designated attachments attached.

Please note that at the moment there are two special "command" lines in the msgfile
files (both start with <<).

   "<<attach="  after the "equals" sign type in the full path and filename of the

  "<<subject=" after the "equals" sign type in the words you wish to appear in the "Subject"
                       line of the email.

The code will not attach the attachment if the attachment does not exist. (of course), but it also will not "fall over".

The code follows..........

I hope that will help you on your way...

Chris (Australia):

Option Explicit

Public Sub SendMonthlyMsg()
  Dim cType As String
  cType = InputBox("<O>verdue or <C>redit?", "Select Message type")
  cType = Left(UCase(Trim(cType)), 1)
  Select Case cType
    Case "O"
      SendSpecialMsg "c:\Messages\Msg_Overdue.txt"
    Case "C"
      SendSpecialMsg "c:\Messages\Msg_Credit.txt"
  End Select
End Sub

Private Sub SendSpecialMsg(strMsgFile As String)
  Dim nAttach As Integer
  Dim nAt As Integer
  Dim nFile As Integer
  Dim strFrom As String
  Dim strFile As String
  Dim strSubject As String
  Dim fs As Object
  Dim strLine As String
  Dim strMsg As String
  Dim oMsg As MailItem
  Dim Attachments() As String
  Dim bHasAttachments As Boolean
  Dim oOutlook As outlook.Application
  Set fs = CreateObject("Scripting.FileSystemObject")
  ReDim Attachments(0)
  If fs.fileexists(strMsgFile) Then
    nFile = FreeFile
    Open strMsgFile For Input As #nFile
    Line Input #nFile, strLine
    Do While Not EOF(nFile)
      If Left(Trim(strLine), 2) = "<<" Then
        strLine = LCase(Trim(strLine))
        nAt = InStr(strLine, "=")
        Select Case Mid(strLine, 3, nAt - 3)
          Case "attach"
            ReDim Preserve Attachments(UBound(Attachments) + 1)
            strFile = Trim(Mid(strLine, nAt + 1))
            If fs.fileexists(strFile) Then
              bHasAttachments = True
              Attachments(UBound(Attachments)) = strFile
            End If
          Case "subject"
            strSubject = Trim(Mid(strLine, nAt + 1))
        End Select
        strMsg = strMsg + strLine + vbCrLf
      End If
      Line Input #nFile, strLine
    Close #nFile
    If Len(strMsg) > 0 Then
      Set oOutlook = New outlook.Application
      Set oMsg = oOutlook.CreateItem(olMailItem)
      oMsg.Body = strMsg
      If bHasAttachments Then
        For nAttach = 1 To UBound(Attachments)
          oMsg.Attachments.add Attachments(nAttach)
      End If
      If Len(strSubject) > 0 Then
        oMsg.Subject = strSubject
      End If
    End If
  End If
  oMsg.Display vbModal
End Sub

Open in new window

IdaracAuthor Commented:
Excellent answer Chris thanx over there in Aussi land.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now