Improve company productivity with a Business Account.Sign Up

x
?
Solved

Print .msg files with attachments vb6.0

Posted on 2009-05-13
18
Medium Priority
?
742 Views
Last Modified: 2012-05-06
Hi,
 I need to set a printer which is different from default printer how do i set it and get the .msg file printed with all its attachments in vb 6.0
0
Comment
Question by:RIAS
  • 11
  • 6
18 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24374250
to print message check here

http://www.freevbcode.com/ShowCode.asp?ID=4837

use printer.print instead of debug.print in the code. for changing default printer see this link

http://bytes.com/topic/access/answers/821821-changing-default-printer-using-vb-code-access

0
 

Author Comment

by:RIAS
ID: 24374256
Cheers mate will try and get back
0
 

Author Comment

by:RIAS
ID: 24375067
Hi,
In the link above where you set the folder the code shows the default outlook folder not sure how to use a different folder where my outlook files are located.

Set oFldr = oNs.GetDefaultFolder(olFolderInbox)


Cheers
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LVL 16

Expert Comment

by:JohnBPrice
ID: 24375502
This code will find any folder in outlook.  


    Dim n As Outlook.NameSpace
    Dim Incoming As Outlook.MAPIFolder
    Set n = o.GetNamespace("MAPI")
    Set Incoming = FindFolder(n.Folders, "\\Public Folders\All Public Folders\YourFolderName")
 
 
Function FindFolder(TopFolder As Outlook.Folders, TargetPath As String) As Outlook.MAPIFolder
    Dim TempFolder As Outlook.MAPIFolder
    Dim TempFolder2 As Outlook.MAPIFolder
    For Each TempFolder In TopFolder
        If TempFolder.FolderPath = TargetPath Then
            Set FindFolder = TempFolder
            Exit Function
        Else 'walk through this folder too
            Set TempFolder2 = FindFolder(TempFolder.Folders, TargetPath)
            If Not TempFolder2 Is Nothing Then
                Set FindFolder = TempFolder2
                Exit Function
            End If
        End If
    Next
    
End Function

Open in new window

0
 

Author Comment

by:RIAS
ID: 24376042
Cheers mate will try and get back
0
 

Author Comment

by:RIAS
ID: 24376175
Hi,
Is this syntax correct?

Set Incoming = FindFolder(oOutlook.Folders, "C:\testing")

Cheers
0
 

Author Comment

by:RIAS
ID: 24376393
Actually i don't want to loo[p throught the folder as i have individual .msg files on my C drive any suggestions on this  on how to print them with attachments


Cheers
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 24376432
No, "C:\testing" is a file system folder, not an outlook folder name which are like "\\Mailbox - John Price\Sent Items".  I thought you meant outlook folders.

If you have already saved the emails as .msg files, you need a different approach using regular file system searching for the .msg files and then create an MailItem from the .msg file.  The only ways I know of to create an Outlook.MailItem from a .msg file is to either use outlook.CreateItemFromTemplate which actually creates a reply to the .msg (and thus may not get you what you need) or use outlook.CopyFile(filename,foldername) to copy the .msg file to an outlook folder, whereupon you can open the mailitem in the usual way.  I'll see if I can dredge up some code.  Also some code for scanning folders.
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 24377351
Let me back up a minute, you have .msg files.  You want to print these with attachments.  Printing the file is trivial, you don't need to deal with outlook at all, just do outlook.exe /p "msgfilename" as shown.  I added the code to change the default printer.

What are the attachments?





' Windows API Declarations
Private Declare Function GetProfileString Lib "Kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private Declare Function WriteProfileString Lib "Kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
 
Private Sub cmdView_Click()
    Dim PrevPrinter As String
    PrevPrinter = GetDefaultPrinter
    SetDefaultPrinter "\\DELLSERVER\HP LaserJet 8150 PCL 6,winspool,Ne06:"
    Shell "C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE /p ""C:\Projects\OutlookTestingVB6\test.msg"""
    SetDefaultPrinter PrevPrinter
End Sub
 
Private Function SetDefaultPrinter(PrinterName_DriverName_PrinterPort As String) As Boolean
    WriteProfileString "windows", "device", PrinterName_DriverName_PrinterPort
    SetDefaultPrinter = CBool(SendMessageByString(&HFFFF&, &H1A, 0&, "windows"))
End Function
 
Private Function GetDefaultPrinter() As String
    GetDefaultPrinter = Space$(1024)
    GetDefaultPrinter = Trim$(Left$(GetDefaultPrinter, GetProfileString("windows", "device", "", GetDefaultPrinter, 1024)))
End Function

Open in new window

0
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 2000 total points
ID: 24377543
doh!, printing the message and attachments is pretty trivial

    Dim ol As New Outlook.Application
    Dim msg As Outlook.MailItem
    Set msg = ol.CreateItemFromTemplate("C:\Projects\OutlookTestingVB6\test2.msg")
    msg.PrintOut
    msg.Attachments.Item(1).SaveAsFile "C:\Projects\OutlookTestingVB6\" & msg.Attachments.Item(1).FileName
    ShellExecute 0, "print", "C:\Projects\OutlookTestingVB6\" & msg.Attachments.Item(1).FileName, 0, 0, 0

Open in new window

0
 

Author Comment

by:RIAS
ID: 24377912
Great will try and be back...attachments are any type example: pdf,doc etc...you are really helping me...cheers mate
0
 

Author Comment

by:RIAS
ID: 24377927
Will try in office tomm..again cheers
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 24378100
>>attachments are any type example: pdf,doc

The ShellExecute will print any type of document as long as you can print it from your machine.  it is the same as if you were to right-click on it and choose "Print".  It opens the associated application and asks it to print to the default printer.
0
 

Author Comment

by:RIAS
ID: 24382686
Hi,

Tried your solution it does a great job for attachments but doesnt print the mail body.
msg.PrintOut----retrieves permission but doesnt print the body.Any suggestions


Cheers
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 24384324
instead of mail.Printout, use the same ShellExecute, e.g.



 ShellExecute 0, "print", "C:\Projects\OutlookTestingVB6\test2.msg", 0, 0, 0

Open in new window

0
 

Author Comment

by:RIAS
ID: 24384820
Cheers mate will try and get back
0
 

Author Comment

by:RIAS
ID: 24385099
You made my life simple
Cheers mate
0
 

Author Closing Comment

by:RIAS
ID: 31580937
Excellent solution given.Perfect in all respects.Easy to understand and implement.
Extremely helpful and patient mentor.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

As a person who answers a lot of questions, I often see code that could be simplified, made easier to read, and perhaps most importantly made easier to maintain if the code was modified to use the Select Case statement. This article explains how to…
While repairing a PST file with ScanPST.exe, many users have encountered the error "Attempting to Validate BBT Refcounts". In this article, we are going to describe the reasons and solution to fix the error.
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

579 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