Solved

Printing a word file

Posted on 2000-02-17
5
413 Views
Last Modified: 2011-09-20
How can i print a word file, or any other file, from visual basic?(ALL IN CODE)
0
Comment
Question by:jonjhess
5 Comments
 
LVL 1

Expert Comment

by:Catouch
Comment Utility
Private Sub PrintContents()
    On Error GoTo ErrPrinting
    Dim prnDefault   As Printer

   
    Set prnDefault = Printer

    With prnDefault
       .FontName = "Courier New"
       .FontSize = 8

    Case Text
                .Orientation = vbPRORPortrait
                prnDefault.Print TxtDocument.Text
end select
end with

ErrPrinting:
    prnDefault.EndDoc
    Screen.MousePointer = vbNormal
End Sub
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
Have you tried this:

Private Declare Function GetSystemDirectory _
                Lib "kernel32" _
                Alias "GetSystemDirectoryA" _
                (ByVal lpBuffer As String, _
                 ByVal nSize As Long _
                ) As Long
Private Declare Function ShellExecute _
                Lib "shell32.dll" _
                Alias "ShellExecuteA" _
                (ByVal hwnd As Long, _
                 ByVal lpOperation As String, _
                 ByVal lpFile As String, _
                 ByVal lpParameters As String, _
                 ByVal lpDirectory As String, _
                 ByVal nShowCmd As Long _
                ) As Long

Private Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const SE_ERR_NOASSOC = 31


PrintFile Me.hwnd, "Print", "c:\test.txt"

Public Sub PrintFile(ByVal plngHwnd As Long, _
                                         ByVal pstrOperation As String, _
                                         ByVal pstrFichierAOuvrir As String)
Dim lngResult As Long
Dim strDir As String

    lngResult = ShellExecute(plngHwnd, pstrOperation, pstrFichierAOuvrir, vbNullString, "", IIf(pstrOperation = "Print", SW_HIDE, SW_SHOW))

    'If the function fails, the return value is an error value that is less than or equal to 32.
    If lngResult >= 33 Then Exit Sub
   
    Select Case lngResult
        Case ERROR_FILE_NOT_FOUND
            MsgBox "Le document est introuvable (" & pstrFichierAOuvrir & ").", vbExclamation + vbOKOnly
        Case SE_ERR_NOASSOC
            MsgBox "Ce type de document n'est pas ASSOCIÉ à une application Windows." & vbCrLf & vbCrLf & "Appuyer sur le bouton OK et vous pourrez l'associer.", vbExclamation + vbOKOnly
            strDir = Space$(260)
            lngResult = GetSystemDirectory(strDir, Len(strDir))
            strDir = Left$(strDir, lngResult)
            Call ShellExecute(plngHwnd, vbNullString, "RUNDLL32.EXE", "shell32.dll,OpenAs_RunDLL " & pstrFichierAOuvrir, strDir, vbNormalFocus)
        Case Else
            MsgBox "Un problème empêche d'ouvrir ce document( " & pstrFichierAOuvrir & "). (Code: " & lngResult & ")", vbCritical + vbOKOnly
    End Select
End Sub
0
 

Author Comment

by:jonjhess
Comment Utility
Ok... that answer will just print the text of the document with a whole bunch of unwanted characters. i need to retain all formating. I dont have a printer now but i'm getting one in a bit and i'll try the second method then. if it works i'll accept your comment as an aswer
0
 
LVL 1

Accepted Solution

by:
cvidler earned 200 total points
Comment Utility
shell "winword.exe /p" & filename$

simple...

(as long as winword.exe is in your path - if not give the full path)
0
 
LVL 3

Expert Comment

by:paulstamp
Comment Utility
Hmmm... some complicated and interesting answers there.

I've done it in the past for Excel but the same technique will work for word.

Use a hidden OLE container on the form and the CREATEMBED("mydoc.doc") command to create an instance of word with the required file loaded. You then have access to Word's object model.  and can do something like ole1.object.documents(1).print - not sure of the specifics of the Word object model - you'll need to look that up.

This has a couple of advantages over cvidler's approach - a) you dont need to know where Word is installed, and b) word does not become visible so it effectively is done in the background.

Hope that helps
0

Featured Post

Highfive Gives IT Their Time Back

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 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

10 Experts available now in Live!

Get 1:1 Help Now