Solved

Printing a .DOC in runtime

Posted on 2000-04-27
17
287 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
[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
  • 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 70

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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
 
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 70

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 70

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

739 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