Solved

Outlook and Visual Basic 6.0 PDF file

Posted on 2012-03-09
3
912 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
Comment Utility
Stand By....

Chris
0
 
LVL 13

Accepted Solution

by:
Chris Raisin earned 500 total points
Comment Utility
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
Comment Utility
Excellent answer Chris thanx over there in Aussi land.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Resolve DNS query failed errors for Exchange
In this video we show how to create a Shared Mailbox 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 Recipients >> Sha…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

15 Experts available now in Live!

Get 1:1 Help Now