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

Printing a word file

How can i print a word file, or any other file, from visual basic?(ALL IN CODE)
0
jonjhess
Asked:
jonjhess
1 Solution
 
CatouchCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
jonjhessAuthor Commented:
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
 
cvidlerCommented:
shell "winword.exe /p" & filename$

simple...

(as long as winword.exe is in your path - if not give the full path)
0
 
paulstampCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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