Solved

Printing a .DOC in runtime

Posted on 2000-04-27
17
283 Views
Last Modified: 2010-05-02
In my program, I need a .DOC sent to a printer on Command1_Click.  Does anyone know how to do this?  For an extra 30 pts, tell me how to print ANY file.
0
Comment
Question by:R_N_WARD
  • 8
  • 6
  • 3
17 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2756160
Public 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

Public Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long



ShellExecute GetDesktopWindow(), "Print", "C:\anypath\anyfile", 0&, 0&, vbNormalFocus

something to that effect
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2756215
You can use this code. Paste this code in a standard module:
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

Public Sub OuvrirApplicationShellExecute(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 "The document doesn't exist (" & pstrFichierAOuvrir & ").", vbExclamation + vbOKOnly
        Case SE_ERR_NOASSOC
            MsgBox "This document is not ASSOCIATED to a Windows application.", 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 "A problem occured while opening the document( " & pstrFichierAOuvrir & "). (Code: " & lngResult & ")", vbCritical + vbOKOnly
    End Select
End Sub

Now from a form, you can call it (from your Command_Click event) this way:
Call OuvrirApplicationShellExecute(Me.hwnd, "Open", "c:\test.doc")
0
 

Author Comment

by:R_N_WARD
ID: 2756284
AzraSound, I am getting tons of errors with your code.
0
 

Author Comment

by:R_N_WARD
ID: 2756292
emoreau, I don't understand most of yours.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2756296
what sort of errors?  provided i didnt add traps like emoroeau did but the basic idea should work
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2756315
put this in your form module:

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 Declare Function GetDesktopWindow Lib "user32" () As Long




Private Sub Command1_Click()
    Dim retVal As Long
    retVal = ShellExecute(GetDesktopWindow(), "Print", "C:\Windows\Desktop\test.doc", 0&, 0&, vbMinimized)
End Sub


i just tried it with a test document and it worked
0
 

Author Comment

by:R_N_WARD
ID: 2756335
Compile error:
sub of Function not defined
0
 

Author Comment

by:R_N_WARD
ID: 2756347
what am I doing wrong?! I copyed your code EXACTLY
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:AzraSound
ID: 2756351
if you put it all in the form code then be sure to declare the functions as Private.  If you want to put the API functions in a bas Module then be sure to declare them as Public
0
 

Author Comment

by:R_N_WARD
ID: 2756372
Same Error. What ver of VB do you have? Maybe that is the diffirence. I have VB6 Learning Edition.
0
 

Author Comment

by:R_N_WARD
ID: 2756409
emoreau, your code opened the document, but did not print it.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2756423
what line does it highlight when you get the error?
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 80 total points
ID: 2756429
Here is what my code is doing:

This line try to open the file you want (pstrFichierAOuvrir) with the operation you want (pstrOperation which is OPEN or PRINT):
    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

In case of an error, you can allow users to specify with which application they want to open the file.      
            Call ShellExecute(plngHwnd, vbNullString, "RUNDLL32.EXE", "shell32.dll,OpenAs_RunDLL " & pstrFichierAOuvrir, strDir, vbNormalFocus)

Is that a bit clearer now?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2756435
Change Open for Print on this line:
Call OuvrirApplicationShellExecute(Me.hwnd, "Print", "c:\test.doc")
0
 

Author Comment

by:R_N_WARD
ID: 2756460
Adjusted points from 50 to 80
0
 

Author Comment

by:R_N_WARD
ID: 2756461
Thanks For explaning this to me emoreau
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2756471
i just want to clear up where you were getting the error.  perhaps the getdesktopwindow call. try my code using Me.hWnd in place of that function call.  i'm just curious.  since emoreau also uses the shellexecute call i assume that isnt an issue
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

22 Experts available now in Live!

Get 1:1 Help Now