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

Print .msg files with attachments vb6.0

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
RIAS
Asked:
RIAS
  • 11
  • 6
1 Solution
 
Shanmuga SundaramDirector of Software EngineeringCommented:
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
 
RIASAuthor Commented:
Cheers mate will try and get back
0
 
RIASAuthor Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
JohnBPriceCommented:
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
 
RIASAuthor Commented:
Cheers mate will try and get back
0
 
RIASAuthor Commented:
Hi,
Is this syntax correct?

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

Cheers
0
 
RIASAuthor Commented:
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
 
JohnBPriceCommented:
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
 
JohnBPriceCommented:
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
 
JohnBPriceCommented:
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
 
RIASAuthor Commented:
Great will try and be back...attachments are any type example: pdf,doc etc...you are really helping me...cheers mate
0
 
RIASAuthor Commented:
Will try in office tomm..again cheers
0
 
JohnBPriceCommented:
>>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
 
RIASAuthor Commented:
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
 
JohnBPriceCommented:
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
 
RIASAuthor Commented:
Cheers mate will try and get back
0
 
RIASAuthor Commented:
You made my life simple
Cheers mate
0
 
RIASAuthor Commented:
Excellent solution given.Perfect in all respects.Easy to understand and implement.
Extremely helpful and patient mentor.
0
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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