Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Printing a .DOC in runtime

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
R_N_WARD
Asked:
R_N_WARD
  • 8
  • 6
  • 3
1 Solution
 
AzraSoundCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
R_N_WARDAuthor Commented:
AzraSound, I am getting tons of errors with your code.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
R_N_WARDAuthor Commented:
emoreau, I don't understand most of yours.
0
 
AzraSoundCommented:
what sort of errors?  provided i didnt add traps like emoroeau did but the basic idea should work
0
 
AzraSoundCommented:
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
 
R_N_WARDAuthor Commented:
Compile error:
sub of Function not defined
0
 
R_N_WARDAuthor Commented:
what am I doing wrong?! I copyed your code EXACTLY
0
 
AzraSoundCommented:
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
 
R_N_WARDAuthor Commented:
Same Error. What ver of VB do you have? Maybe that is the diffirence. I have VB6 Learning Edition.
0
 
R_N_WARDAuthor Commented:
emoreau, your code opened the document, but did not print it.
0
 
AzraSoundCommented:
what line does it highlight when you get the error?
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
Change Open for Print on this line:
Call OuvrirApplicationShellExecute(Me.hwnd, "Print", "c:\test.doc")
0
 
R_N_WARDAuthor Commented:
Adjusted points from 50 to 80
0
 
R_N_WARDAuthor Commented:
Thanks For explaning this to me emoreau
0
 
AzraSoundCommented:
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

Industry Leaders: 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!

  • 8
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now