Solved

Printing a word file

Posted on 2000-02-17
5
416 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

696 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