Solved

Print .msg files with attachments vb6.0

Posted on 2009-05-13
18
716 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

707 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

12 Experts available now in Live!

Get 1:1 Help Now