Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 3rd, 2003, 10:21 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do you Display a pdf document

I can open and display word and excel documents from a vb program by declaring, settting and opening a particular document. I can find no references in print or online as to how to open and display a pdf document apart from having a OLE container embedding the document at run time and then double clicking the icon in the container. This I feel is a bit messy in comparison with opening a word or excel document using a command button and code. The reference to the pdf document is held in an access database for the particular document. The following code opens an excel file and displays the particulwar workbook after Excel has been declared and set. The second piece of code opens acrobat but to open the particular pdf file at the same time is the problem. I have done it as a case statement because the methods of opening are dependant on type, ie .doc(word file), .xls (excel file) or .pdf (adobe file)

            Case "xls"

                Filepath = gsFolder & "msds docs\" & Directory & "\" & rsMSDS!RefNo

                With objExcel

                    Workbooks.Open FileName:=Filepath

                    .Visible = True

                End With



            Case "pdf"

                Dim AppName As String

                Dim AppName1 As Object

                Filepath = gsFolder & "msds docs\" & Directory & "\" & rsMSDS!RefNo

                AppName = "C:\Program Files\Adobe\Acrobat 5.0\Reader\AcroRd32.exe"

                Call Shell(AppName, 1) ' AppName contains the path of the executable file.

any advice would be appreciated
 
Old June 4th, 2003, 12:37 AM
Authorized User
 
Join Date: Jun 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter,

1. Assuming all you want to do is open the file (no interaction from your VB program once it's opened).

Use the Windows ShellExecute API function. That allows you to specify the name of the file and it will be opened with the default program (Acrobat or Acrobat Reader, whichever the use has). Note that you could also use this method for opening your Word and Excel docs as well (no need for a switch, common code for all). Again this is assuming all that you want to do is open them for user interaction/viewing, no control from your program.

Rather than me mis-type an example here, try this web page for all the necessary code.
http://vbaccelerator.nuwebhost.com/c...ll/shellex.htm

2. If you want to interact with the Acrobat file from your VB program, I should look at the Abode Acrobat SDK pages.
http://partners.adobe.com/asn/tech/pdf/acrobatsdks.jsp
The control from VB is pretty limited (although it may have improved with version 6 with indirect control with the Javascript object) , but you can get pretty good control with C++.
 
Old June 4th, 2003, 08:27 AM
Authorized User
 
Join Date: Jun 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, I'd say there's a better way. In a new project's Form1, add a CommonDialog and a CommandButton, then paste the following into the code page, and you'll see how to open any file with it's associated app. Doing it this way causes your app to have the operating system make the determination of which app should handle the file, and of where that app is physically located.

Pete

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 Sub Command1_Click()
Dim strFileName As String
Dim lngReturn As String

    ' set up for display
    CommonDialog1.CancelError = True

    ' have user select the file to be opened
    CommonDialog1.Action = 1

    ' if no file selected, do nothing
    If CommonDialog1.FileName = "" Then
        Exit Sub
    End If

    ' save path and filename, and filename only.
    strFileName = CommonDialog1.FileName

    lngReturn = ShellExecute( _
        Me.hWnd, _
        "OPEN", _
        strFileName, _
        "", _
        "", _
        vbNormalFocus)

End Sub
 
Old June 4th, 2003, 04:06 PM
Authorized User
 
Join Date: Jun 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Pete,

I'd agree that the ShellExecute API may be a better way to go, in fact it's the way I suggested above. ;)

If I read Peter's post correctly though, he has no need for a Common Dialog control as he already knows the URL for the file (Excel, Word, PDF) as it's stored in his Access database.
 
Old June 4th, 2003, 04:21 PM
Authorized User
 
Join Date: Jun 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry Phil...I must have had sleep in my eyes when I read your post. You were right on the money. I only included the CommonDialog to demonstrate the function in the sample program.

Pete
 
Old June 4th, 2003, 04:56 PM
Authorized User
 
Join Date: Jun 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Pete,

I'm glad to added the Common Dialog example as I hope it'll be useful to anyone browsing that doesn't have the URL of the file available.

I'd encourage anyone who hasn't to have a look at the various API calls, you may be surprised at what you can do with them. For example, I've just used the WinINet API functions as a replacement for the limited VB Internet Transfer Control.
 
Old June 4th, 2003, 08:53 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Phil & Peter
Both methods work fine. I don't need the Common Dialog as it is reading the Database reference and opening all Doco's. Word & Excel Docs work better if they have been set to Read-only otherwise you get the question to open as read only and as I want the view to be read only ie view the doc Peter's solution is fine.
Thanking you kindley.
PeterF:)
 
Old June 12th, 2003, 09:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How about a compromise. What if we wrote a function that had an optional paramter for the file and path. We can set a default value to "NULL". If the value is NULL then we call the common dialog box and allow the user to select the file. Then we can have one source of code and use it in various ways. Better yet we can make a Dll and reuse it in many applications. It would be a handy tool.

Just a thought.

Larry Asher
 
Old May 25th, 2005, 01:56 AM
Registered User
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks guys you answered two of my questions, you can also print pdf's by changing the lpOperation parameter from "OPEN" to "PRINT". too easy!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Document to PDF BSkelding ASP.NET 1.0 and 1.1 Professional 2 July 19th, 2013 04:53 AM
View PDF document without Acrobat kirti_goyal ASP.NET 2.0 Professional 5 May 11th, 2007 08:28 AM
Open pdf document ppenn Access VBA 1 February 20th, 2007 12:31 PM
Dynamic PDF Document Generation nialljpmurphy J2EE 4 March 27th, 2006 07:13 PM
PDF Document Import helga Pro VB 6 1 September 20th, 2004 08:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.