Solved

Printing a word file

Posted on 2000-02-17
5
415 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
[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
5 Comments
 
LVL 1

Expert Comment

by:Catouch
ID: 2530313
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 70

Expert Comment

by:Éric Moreau
ID: 2531101
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
ID: 2533429
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
ID: 2533720
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
ID: 2537945
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

Technology Partners: 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!

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

737 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