?
Solved

Printing a word file

Posted on 2000-02-17
5
Medium Priority
?
417 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 600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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