Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Call pdf file from visual basic 6.0  (Read 21085 times)

0 Members and 1 Guest are viewing this topic.

demo71

    Topic Starter


    Rookie

    Call pdf file from visual basic 6.0
    « on: February 18, 2009, 01:56:22 AM »
    Help me!

    I like call always other pdf file from visual basic program
    What wrong this lines?
    Thank you

    Private Sub Command1_Click()
                       
    Dim iResult As Double
    Dim iPath As String
    Dim filename
    filename = "d:\2009calendar.pdf"
    iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "
    iResult = Shell(iPath, vbMaximizedFocus)
                       
    End Sub

    CBSk



      Beginner
    • Look for opportunities where I can help...
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows XP
      Re: Call pdf file from visual basic 6.0
      « Reply #1 on: February 18, 2009, 02:39:24 AM »
      Quote
      iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "


      Change the line to

      Code: [Select]
      iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe" & " " & filename
      You can't use Variables inside double quotes. Only String values are represented by double quotes. :)
      Selvakumar CBSk

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Call pdf file from visual basic 6.0
      « Reply #2 on: February 18, 2009, 02:45:09 AM »
      your inserting the filename within the string:

      Code: [Select]
      Private Sub Command1_Click()
                        
      Dim iResult As Double
      Dim iPath As String
      Dim filename
      filename = "d:\2009calendar.pdf"

      'iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "
      'Fixed line below:
      iPath = "C:\Program FIles\Adobe\Acrobat 7.0\Reader\AcroRd32.exe " & filename
      iResult = Shell(iPath, vbMaximizedFocus)
                        
      End Sub


      That should work.

      However, while I'm here- how about I give you some suggestions for improvement? As an example, instead of executing Acrobat reader directly, it's possible to instead execute the pdf file itself, which opens the program that is set to open PDF files.


      To do this, Place this code in the Declarations section of the form (the very top, underneath "Option Explicit" if present.

      Code: [Select]
      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

      And then place this at the bottom of the rest of your code...
      Code: [Select]
      Private Function ShowFileDefault(FilePath As String, ownerHwnd As Long) As Boolean
           Dim dummy As Long
          
                     'open the file using the default Editor or viewer.
           dummy = ShellExecute(ownerHwnd, "Open", FilePath & Chr$(0), Chr$(0), _
                                                Left$(FilePath, InStr(FilePath, "\")), 0)
          
        
           ShowFileDefault=Dummy
          
          
          
      End Function


      As for your original code, to use the new routine:

      Code: [Select]
      Private Sub Command1_Click()
                        
      Dim iResult As Double
      Dim iPath As String
      Dim filename
      filename = "d:\2009calendar.pdf"

      'replaced this block...
      'iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "
      'Fixed line below:
      'iPath = "C:\Program FIles\Adobe\Acrobat 7.0\Reader\AcroRd32.exe " & filename

      'iResult = Shell(iPath, vbMaximizedFocus)
                        


      'with this line:
      ShowFileDefault Filename, Me.Hwnd


      End Sub


      I hope this helps!  8)
      I was trying to dereference Null Pointers before it was cool.

      demo71

        Topic Starter


        Rookie

        Re: Call pdf file from visual basic 6.0
        « Reply #3 on: February 18, 2009, 07:41:49 AM »
        Thx all!

        iftekhar_bd

        • Guest
        Re: Call pdf file from visual basic 6.0
        « Reply #4 on: May 12, 2009, 03:28:03 AM »
        while using the last code in my project and run i got the message

        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

        -- i added this option in the general declarations option


        Private Sub mnuHelpGeneral_Click()
        Dim iResult As Double
        Dim iPath As String
        Dim filename

        filename = App.Path & "\help.pdf"
        ShowFileDefault filename, Me.hWnd
        End Sub


        -- i Added this in the in the help click option

        Private Function ShowFileDefault(FilePath As String, ownerHwnd As Long) As Boolean
             Dim dummy As Long
             
                       'open the file using the default Editor or viewer.
             dummy = ShellExecute(ownerHwnd, "Open", FilePath & Chr$(0), Chr$(0), _
                                                  Left$(FilePath, InStr(FilePath, "\")), 0)
             
           
             ShowFileDefault = dummy
             
           
             
        End Function


        -- i added this in the bottom of the code

        but while i run the source and got error messages


                       ByRef argument type mismatch


        please help me to solve this problem

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Call pdf file from visual basic 6.0
        « Reply #5 on: May 12, 2009, 03:42:34 AM »
        Code: [Select]
        Dim filename

        dimensions a Variant type variable. ShowFileDefault wants a String. Either change the Dim to say:

        Code: [Select]
        Dim filename as String


        Or, optionally, change the showFiledefault declaration to:
        Code: [Select]
        Private Function ShowFileDefault(Byval FilePath As String, ownerHwnd As Long) As Boolean


        which will attempt to convert the given argument (in this case, a variant) to the required string.
        I was trying to dereference Null Pointers before it was cool.

        Reno



          Hopeful
        • Thanked: 32
          Re: Call pdf file from visual basic 6.0
          « Reply #6 on: May 12, 2009, 03:50:17 AM »
          Code: [Select]
            dummy = ShellExecute(ownerHwnd, "Open", FilePath & Chr$(0), Chr$(0), _
                                                    Left$(FilePath, InStr(FilePath, "\")), 0)

          and if the 5th argument (lpDirectory) means working directory, then InStrRev should be used instead of InStr that returns the root drive??

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: Call pdf file from visual basic 6.0
          « Reply #7 on: May 12, 2009, 04:14:42 AM »
          Or, just leave it blank. That works too.

          Probably change the 0 to vbnormalFocus as well; 0 appears to make it appear on the taskbar but not be able to display the window to the program.

          I prefer ShellExecuteEx myself, actually.
          I was trying to dereference Null Pointers before it was cool.

          Reno



            Hopeful
          • Thanked: 32
            Re: Call pdf file from visual basic 6.0
            « Reply #8 on: May 12, 2009, 04:46:32 AM »
            yup, correct, that works too.
            from msdn reference:
            http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx
            Quote
            lpDirectory
            [in] A pointer to a null-terminated string that specifies the default (working) directory for the action. If this value is NULL, the current working directory is used. If a relative path is provided at lpFile, do not use a relative path for lpDirectory.
            nShowCmd
            [in] The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it.
            SW_HIDE
            SW_SHOWNORMAL

            ShellExecuteEx API looks complicated, but seems to provides more control on the created process. do you have a wrapper module for ShellExecuteEx?

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Call pdf file from visual basic 6.0
            « Reply #9 on: May 12, 2009, 05:13:14 AM »
            It's not really that complicated...

            Here's one that uses it to show the  Open With dialog for any file:

            declarations:
            Code: [Select]
            Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As Long

            Private Declare Function ShellExecuteEx Lib "shell32.dll" (ByRef lpExecInfo As SHELLEXECUTEINFOA) As Long
            Private Type SHELLEXECUTEINFOA
                cbSize As Long
                fMask As Long
                hwnd As Long
                lpVerb As String
                lpFile As String
                lpParameters As String
                lpDirectory As String
                nShow As Long
                hInstApp As Long
                ' fields
                lpIDList As Long
                lpClass As String
                hkeyClass As Long
                dwHotKey As Long
                hIcon As Long
                hProcess As Long
            End Type


            Code:
            Code: [Select]
            Public Function OpenWith(ByVal HwndOwner As String, ByVal FilePath As String, Optional ByVal Icon As Long = 0) As Long

                Dim sei As SHELLEXECUTEINFOA
                Dim verba As String
                verba = "openas"
                sei.hIcon = Icon
                sei.hProcess = GetCurrentProcess()
                sei.hInstApp = App.hInstance
               
                sei.lpVerb = verba
                sei.lpFile = FilePath
                sei.nShow = vbNormalFocus
                sei.cbSize = Len(sei)
               
                OpenWith = ShellExecuteEx(sei)


                'Stop

            End Function

            'another option, would be to allow for the verb to be specified as an argument, along with several other portions of the structure.
            I was trying to dereference Null Pointers before it was cool.