Printing an external file from form

crann

Registered User.
Local time
Today, 17:15
Joined
Nov 23, 2002
Messages
160
Hi

I have a form with the following code on a Cmd Button to view a scanned or saved document for some fire safety certifciates. The scanned docs are saved in a generic Z:\ witht he file name always equal to the relevant property code.

I now want to be able to print that same document from within the form without needing to launch the pdf viewer or navigate away from the form is this possible?

here is the current code:

Thanks


Code:
Private Sub Command351_Click()

Dim strFilePath As String


strFilePath = "Z:\GasCertificates\" & Me.txttwenty & ".PDF"

If Dir(strFilePath) > "" Then

   Application.FollowHyperlink strFilePath

Else

  MsgBox "There are no GAS Certificates saved for this Property. Please Add or Scan a new document!"
End If


End Sub
 
usage:
ExecuteFile strFileName, "print"
Code:
Option Compare Database
Option Explicit

Private Const SW_HIDE = 0
Private Const SW_MINIMIZE = 6
Private Const SW_RESTORE = 9
Private Const SW_SHOW = 5
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOWNORMAL = 1

#If Win64 Then
Private Declare PtrSafe 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
#Else
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
#End If

Public Sub ExecuteFile(sFileName As String, Optional sAction As String = "Open")
    Dim lngWindowMode As Long
    'sAction can be either “Open” or “Print”.
    lngWindowMode = IIf(sAction = "Open", SW_SHOWNORMAL, SW_SHOWNA)
    If ShellExecute(Access.hWndAccessApp, sAction, _
        sFileName, vbNullString, "", lngWindowMode) < 33 Then
        DoCmd.Beep
        MsgBox "File not found."
    End If
End Sub
 
Hi Arnelgp

Thanks. Where is this code suppose to go. As a module? or on the click event of a cmd button?

Im not very experienced so takes me a little longer to understand what I am looking at.
 
the block of code goes in the module, and the normal text (Execute strFilePath, "print") goes to your command351_click event. comment out Application.FollowHyperlink ..., and insert the above code.
 
Ok maybe a little lost here this is what I now have

Have I lost the plot?

This is now my Cmd Button:


Code:
Private Sub Command351_Click()

ExecuteFile strFileName, "print"

Dim strFilePath As String


strFilePath = "Z:\GasCertificates\" & Me.txttwenty & ".PDF"

If Dir(strFilePath) > "" Then

   'Application.FollowHyperlink strFilePath

Else

  MsgBox "There are no GAS Certificates saved for this Property. Please Add or Scan a new document!"
End If


End Sub

and this is my module:
Code:
Option Compare Database

Option Explicit

Private Const SW_HIDE = 0
Private Const SW_MINIMIZE = 6
Private Const SW_RESTORE = 9
Private Const SW_SHOW = 5
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOWNORMAL = 1

#If Win64 Then
Private Declare PtrSafe 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
#Else
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
#End If

Public Sub ExecuteFile(sFileName As String, Optional sAction As String = "Open")
    Dim lngWindowMode As Long
    'sAction can be either “Open” or “Print”.
    lngWindowMode = IIf(sAction = "Open", SW_SHOWNORMAL, SW_SHOWNA)
    If ShellExecute(Access.hWndAccessApp, sAction, _
        sFileName, vbNullString, "", lngWindowMode) < 33 Then
        DoCmd.Beep
        MsgBox "File not found."
    End If
End Sub
 
If you are waint to print and not open then

Code:
ExecuteFile strFileName, "print"

replaces the line:

Code:
  'Application.FollowHyperlink strFilePath
 
Hi Thanks

I am getting the following error when I try this code:

Compile Error:
ByRef argument type mismatch

With this part of my code highlighted:

Code:
Private Sub Command351_Click()

'ExecuteFile strFileName, "print"

Dim strFilePath As String


strFilePath = "Z:\GasCertificates\" & Me.txttwenty & ".PDF"

If Dir(strFilePath) > "" Then

    ExecuteFile [COLOR="Yellow"]strFileName[/COLOR], "print"
   'Application.FollowHyperlink strFilePath

Else

  MsgBox "There are no GAS Certificates saved for this Property. Please Add or Scan a new document!"
End If


End Sub

Thanks
 

Users who are viewing this thread

Back
Top Bottom