Solved

Print .msg files with attachments vb6.0

Posted on 2009-05-13
18
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 500 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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: …

733 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