Solved

Outlook and Visual Basic 6.0 PDF file

Posted on 2012-03-09
3
917 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Idarac
  • 2
3 Comments
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 37704697
Stand By....

Chris
0
 
LVL 13

Accepted Solution

by:
Chris Raisin earned 500 total points
ID: 37704862
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:
   
  Msg_Credit.txt:
                               This is a CREDIT message with one attachment!
                               <<subject=Credit Message
                               <<attach=c:\Messages\MyPdf1.pdf

Msg_Overdue.txt:

                             This is an Overdue Message with TWO attachmentS
                             <<attach=c:\Messages\MyPdf1.pdf
                             <<attach=c:\Messages\MyPdf2.pdf
                             <<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:
"SendMonthlyMsg".

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
                       attachment

  "<<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...

Cheers
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
      Else
        strMsg = strMsg + strLine + vbCrLf
      End If
      Line Input #nFile, strLine
    Loop
    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)
        Next
      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

0
 
LVL 1

Author Closing Comment

by:Idarac
ID: 37705379
Excellent answer Chris thanx over there in Aussi land.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now