Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show pdf, tif and jpg

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,569
US

If I have a text file and I want to show it in Notepad, I can do:
Code:
Dim RetVal As Long
RetVal = Shell("NOTEPAD.EXE " & "C:\Test.txt", 1)
What about if I have PDF, TIF, or JPG files?

Is there any 'generic reader' I can call from VB 6 that could show me any of the files?

Also, it would be nice if the location of the file would NOT be revealed to the user (like in recently open files), but this part is not that crucial.

Have fun.

---- Andy
 
I usually use the ShellExecute API which opens the file with the associated program (I think that's the way it works, I stand to be corrected)

Patrick
 
If you have applications associated with specific file types then you can do something like
Code:
If NOT ExecuteWait ("C:\SomeDir\SomeFile.pdf") Then
   MsgBox "No Application Found"
End If

Where the functions are

Code:
' *****************************************************
'  Same as the Execute function but waits for the
'  process to finish before returning.
'  returns true on success.
' *****************************************************
Public Function ExecuteWait(ByVal FileName As String) As Boolean
    Dim CommandString               As String

    CommandString = FindExecutable(FileName)

    If CommandString <> "" Then
        ExecuteWait = ShellWait(CommandString & " " & FileName)
    Else
        ExecuteWait = False
    End If
End Function

'  Runs a command as the Shell command does but waits for the command
'  to finish before returning.  Note: The full path and filename extention
'  is required.
'  You might want to use Environ$("COMSPEC") & " /c " & command
'  if you wish to run it under the command shell (and thus it)
'  will search the path etc...
'
'  returns false if the shell failed

Public Function ShellWait(cCommandLine As String) As Boolean
    Dim NameOfProc                  As PROCESS_INFORMATION
    Dim NameStart                   As STARTUPINFO
    Dim i                           As Long

    NameStart.cb = Len(NameStart)
    i = CreateProcessA(0&, cCommandLine, 0&, 0&, 1&, _
                       NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)

    If i <> 0 Then
        Call WaitForSingleObject(NameOfProc.hProcess, INFINITE)
        Call CloseHandle(NameOfProc.hProcess)
        ShellWait = True
    Else
        ShellWait = False
    End If

End Function


' *****************************************************
'  Finds the executable associated with a file
'  Returns "" if no file is found.
' *****************************************************
Public Function FindExecutable(FileName As String) As String
    Dim i                           As Integer
    Dim ExeName                     As String

    ExeName = String(MAX_FILENAME_LEN, 32) & Chr$(0)

    i = FindExecutableA(FileName & Chr$(0), vbNullString, ExeName)

    If i > 32 Then
        FindExecutable = Left$(ExeName, InStr(ExeName, Chr$(0)) - 1)
    Else
        FindExecutable = ""
    End If

End Function

and the API Declares are
Code:
Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&
Public Const MAX_FILENAME_LEN = 256

Public Declare Function FindExecutableA Lib "shell32.dll" _
                                        (ByVal lpFile As String, ByVal lpdirectory As String, _
                                         ByVal lpResult As String) As Long


Public Declare Function CreateProcessA Lib "kernel32" ( _
                                       ByVal lpApplicationName As Long, ByVal lpCommandLine As String, _
                                       ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
                                       ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
                                       ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
                                       lpStartupInfo As STARTUPINFO, _
                                       lpProcessInformation As PROCESS_INFORMATION) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" _
                                            (ByVal hHandle As Long, _
                                             ByVal dwMilliseconds As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean

' Used in CreateProcessA
Public Type STARTUPINFO
    cb                              As Long
    lpReserved                      As String
    lpDesktop                       As String
    lpTitle                         As String
    dwX                             As Long
    dwY                             As Long
    dwXSize                         As Long
    dwYSize                         As Long
    dwXCountChars                   As Long
    dwYCountChars                   As Long
    dwFillAttribute                 As Long
    dwFlags                         As Long
    wShowWindow                     As Integer
    cbReserved2                     As Integer
    lpReserved2                     As Long
    hStdInput                       As Long
    hStdOutput                      As Long
    hStdError                       As Long
End Type

' Used in CreateProcessA
Public Type PROCESS_INFORMATION
    hProcess                        As Long
    hThread                         As Long
    dwProcessId                     As Long
    dwThreadId                      As Long
End Type
 
Andrzejek said:
Is there any 'generic reader' I can call from VB 6 that could show me any of the files?

You can use the webbrowser control, and use the Navigate method to navigate to the file... If it's an extension that IE can handle, then it will display in your control. But, you are limited to file extensions that IE on the machine you are using can handle.
 
Hi Andy,

I agree with guitarzan: for tif/pdf/jpg, I'd try with a browser control.

If you want something more flexible and generic, do a search for RegQueryValueEx. For a generic approach, you would do best to read the registry value from HKEY_CLASSES_ROOT to get the application associated with a certain file extension, then read the registry entry for the path to that application and Shell it.

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
> you would do best to read the registry value from HKEY_CLASSES_ROOT to get the application associated with a certain file extension, then read the registry entry for the path to that application

Indeed, and handily there is actually a single API call that does all of that for you? AssocQueryString, which I illustrated in thread222-1553883
 

Thanks for the suggestions.

WebBrowser - TIF files, it forces the MicroSoft Office Document Imaging app to show the image, JPG is OK, PDF shows fine. I just notice that webbrowser does not give the possibilities to rotate the image, and no zoom function, either.

I know I could use the applications associated with specific file types (something like)
Code:
Public Sub OpenDocument(strDocPath As String)

Dim G As Long
G = Shell("RUNDLL32.EXE URL.DLL,FileProtocolHandler " & strDocPath, vbNormalFocus)
    
End Sub
But I would like to give my users just one application to view whatever document type they select, and they will not know if they select PDF, JPG or TIF, or they may have more file types comming.

I'm working on a way to give my users ability to search stored documents (archives) somewhere on the server(s), they will not know the file location or the file format. I have a way to access (somebody else's) database with all information needed to do the search, I just need a way to show the files to the user.

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top