No of visitors who read this post: 658
Category: Visual basic
Type: Question
Author: Jdiaz
No votes yet

How can I send an email files to be viewed in Excel from the software that is written in Visual Basic 6 (VB6), so that when the receiver opens the Excel book, it will not only shows the data but copies all the codes as well?

Can someone send me a suggestion?

Thanks.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

# Comment Id: 42566

To send an email file so that it will be viewed in Excel from the software that is written in Visual Basic 6 (VB6), so that when the receiver opens the Excel book, it will not only shows the data but copies  all the codes as well, you will first need to have mail server, and in this case I am assuming you are using outlook.

The first thing you will need to do is select the outlook object from the visual basic project preferences, and the code for that process is as below:

Public sub SendMail(tSubject As String, tBody As String)
'Description: Uses the outlook object to create and send a mail using the passed parameters of tsubject,tbody and tTo
Dim oItem As Outlook.MailItem
Dim oitems As Items
Set oOutlook = New Outlook.Application
Set oitems = objInbox.Items
Set oItem = oOutlook.CreateItem(olMailItem)
'With oItem
.To = tTo
.Body = tBody
.Subject = tSubject
.send
End With

Without Outlook (My preferred method)
Reference the CDO 1.21 object from your references section in the VB IDE

Private Sub SendMail(tBody As String, tSubject As String)
Dim objSession As MAPI.Session
Dim objmessage As MAPI.Message
Dim objRecipient As MAPI.Recipient
Dim objAttach As MAPI.Attachment

'Create the Session Object
  Set objSession = CreateObject("mapi.session")
   
  'Logon using the session object
  'Specify a valid profile name if you want to
  'Avoid the logon dialog box
  'If you don't include a profilename then a dialog is popup requesting one
  objSession.Logon profileName:="MS Exchange Settings"

  'Add a new message object to the OutBox
  Set objmessage = objSession.Outbox.Messages.Add

  'Set the properties of the message object
  objmessage.Subject = tSubject
  objmessage.Text = tBody
  'Popup the global address list to select your recipients
  'Force the resolution of the named recipients
  Set objmessage.Recipients = objSession.AddressBook(, "Select Recipients", , True, , ">>")
    'To add attachments
  Set objAttach = objmessage.Attachments.Add
        objAttach.Name = "testing" 'Pass your own name
        objAttach.Source = "C:\Boot.ini" 'Pass in your own filename
        objAttach.Type = CdoFileData 'This is the Default
  
    'Send the message
  objmessage.Send showDialog:=False
  
  'Logoff using the session object
  objSession.Logoff
End Sub

Regards

Thompson Locker