Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Chicago, IL

    Unanswered: Launching an external doc

    Hi there,

    I need to be able to launch a .pdf document from a button. I am currently trying DocName.FileOpen but have yet to find satisfying results.

    Does anybody know how I can launch an external app or doc?


    (I'm sure the answer is in the forum somewhere but I couldn't find it!)

  2. #2
    Join Date
    Nov 2003
    Here is what I use to display a License text document from a button control in a About box . The code first looks for WORD for Windows...if that fails, it then automatically looks to display the document in the Windows WordPAD, and if that fails, it then automatically looks to display the document in Windows NotePad. And if that displays nothing at all.

    Private Sub LicenseButton_Click()
    	On Error Resume Next
    	If Shell("WINWORD.EXE " & fGetShortName(DatabasePath & "License.doc"), 1) = 0 Then
    	 If Shell("WORDPAD.EXE " & fGetShortName(DatabasePath & "License.doc"), 1) = 0 Then
    		 If Shell("NOTEPAD.EXE " & fGetShortName(DatabasePath & "LicenseT.txt"), 1) = 0 Then
    			DoCmd.OpenReport "License Agreement", acViewPreview
    		 End If
    	 End If
    	End If
    End Sub
    Notice I have the path and file names coverted to Short File Name format. I find better sucess with this. The function I use for this is called fGetShortName. Here is the Declarations and functions. Place them into a new code module (I believe I got this code segment from the DevShed):

    'The functions fGetShortName and fGetLongName will return
    'the appropriate Short or Long filename.
    Private Const MAX_PATH& = 260
    Private Const INVALID_HANDLE_VALUE = -1
    	dwLowDateTime As Long
    	dwHighDateTime As Long
    End Type
    Type WIN32_FIND_DATA ' 318 Bytes
    	dwFileAttributes As Long
    	ftCreationTime As FILETIME
    	ftLastAccessTime As FILETIME
    	ftLastWriteTime As FILETIME
    	nFileSizeHigh As Long
    	nFileSizeLow As Long
    	dwReserved¯ As Long
    	dwReserved1 As Long
    	cFileName As String * MAX_PATH
    	cAlternate As String * 14
    End Type
    Private Declare Function apiFindFirstFile Lib "kernel32" _
    	Alias "FindFirstFileA" _
    	(ByVal lpFileName As String, _
    	lpFindFileData As WIN32_FIND_DATA) _
    	As Long
    Private Declare Function apiFindClose Lib "kernel32" _
    	Alias "FindClose" _
    	(ByVal hFindFile As Long) _
    	As Long
    Private Declare Function apiGetShortPathName Lib "kernel32" _
    	Alias "GetShortPathNameA" _
    	(ByVal lpszLongPath As String, _
    	ByVal lpszShortPath As String, _
    	ByVal cchBuffer As Long) _
    	As Long
    Public Function fGetShortName(ByVal stLongPath As String) As String
    	'[ opposite of fGetLongName() ]
    	'Usage Examples:
    	'?fGetShortName("D:\Internet Explorer 4.0 Setup\This folder is safe to delete.txt")
    	Dim stShortPath As String
    	Dim lngBuffer As Long, lngRet As Long
    	stShortPath = String$(MAX_PATH, 0)
    	lngBuffer = Len(stShortPath)
    	lngRet = apiGetShortPathName(stLongPath, stShortPath, lngBuffer)
    	fGetShortName = Left(stShortPath, lngRet)
    End Function
    Function fGetLongName(ByVal strFileName As String) As String
    	'Usage Examples:
    	'D:\Internet Explorer 4.0 Setup\This folder is safe to delete.txt
    	'C:\Program Files\Microsoft Office\Office\Samples\Solutions.mdb
    	'C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb
    	Dim lpFindFileData As WIN32_FIND_DATA
    	Dim strPath As String, lngRet As Long
    	Dim strFile As String, lngX As Long, lngY As Long
    	Dim strTmp As String
    	strTmp = ""
    	Do While Not lngRet = INVALID_HANDLE_VALUE
    		lngRet = apiFindFirstFile(strFileName, lpFindFileData)
    		strFile = Left$(lpFindFileData.cFileName, _
    					InStr(lpFindFileData.cFileName, _
    					vbNullChar) - 1)
    		If Len(strFileName) > 2 Then
    			strTmp = strFile & "\" & strTmp
    			strFileName = fParseDir(strFileName)
    			strTmp = strFileName & "\" & strTmp
    			Exit Do
    		End If
    	fGetLongName = Left$(strTmp, Len(strTmp) - 1)
    	lngY = apiFindClose(lngRet)
    End Function
    Private Function fParseDir(strInFile As String) As String
    	Dim intLen As Long, boolFound As Boolean
    	Dim i As Integer, f As String, strDir As String
    	intLen = Len(strInFile)
    	If intLen > 0 Then
    		boolFound = False
    		For i = intLen To 1 Step -1
    			If Mid$(strInFile, i, 1) = "\" Then
    				f = Mid$(strInFile, i + 1)
    				strDir = Left$(strInFile, i - 1)
    				boolFound = True
    				Exit For
    			End If
    		Next i
    		End If
    	If boolFound Then
    		fParseDir = strDir
    		fParseDir = strInFile
    	End If
    End Function
    DatabasePath is a Global Variable which holds the path to where the actual database was started (it's home path location). This variable is declared in a main database codemodule. To fill this variable with the startup path I use the function below:

    Public Function GetDB_Path() As String
    'DAO 3.6 required
    Dim DB As Database
    Dim i As Integer, a$
    Set DB = CurrentDb()
    a$ = Trim(DB.Name)
    For i = Len(a$) To 1 Step -1
    	 If Mid$(a$, i, 1) = "\" Then Exit For
    Next i
    GetDB_Path = Left$(a$, i)
    End Function
    DatabasePath = GetDB_Path

    In your particular case....simply replace WINWORD.EXE in the Shell statement with whatever program you're using as a PDF reader. The other two IF statements you can do without.

    Hope this helps

  3. #3
    Join Date
    Oct 2004

    open a document

    I did this by adding a HYPERLINK field in the data table. works by clicking on the field the pdf pops right up.

  4. #4
    Join Date
    Oct 2003
    Chicago, IL

    Talking Groooovy

    Thanks, both of you! I was out of the country for a few weeks, didn't get to view this until today... I will try tomorrow! cheers...

  5. #5
    Join Date
    Jul 2004
    Blackburn, UK


    If you still have problems try this
    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_SHOWNORMAL = 1
    Dim lret as long
    Place that at the declarations

    Use this to launch the file

    lRet = ShellExecute(Me.hwnd, vbNullString, "BEN\QUALITY CONTROL\QUALITY CONTROL 2002.mdb", vbNullString, "s:\", SW_SHOWNORMAL)
    Replacing in the obvious places

    You can also use this to open web pages

    ShellExecute Me.hwnd, "open", ", vbNullString, "", 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts