How to use a wildcard when opening an executable file with StrProg

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #16
    Nick,

    Sorry for the long delay, I've been out of the office for the last three days.

    Here is a bit of documentation on ShellExecuteA, that you might want to breeze through.

    Anyway, I forgot this was a VBA forum when I posted my bit for Linux/Unix type systems.

    So, to make a comprehensive windows solution (I am using Excel for testing), what I did was create a "Module" named "Code Test", which contained my test code, as follows:
    Code:
    ''====================================================================
    ''====================================================================
    ''====================================================================
    ''
    ''  Code Test
    ''    Module with various prepared code tests and use examples
    ''
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    
    Option Compare Database
    Option Explicit
    
    
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    Public Sub TEST_showPDF()
      Dim wi As Object
      Set wi = New [Windoze Interface]
      Call wi.showPDF("C:\tmp\IRON File Systems (vijayan-thesis).pdf")
    End Sub
    
    '=====================================================================
    '=====================================================================
    '=====================================================================

    And then, I created a "Class Module" with the actual launch code, as follows:
    Code:
    ''====================================================================
    ''====================================================================
    ''====================================================================
    ''
    ''  Windoze Interface
    ''    Code to interface to µsWindows DLLs
    ''
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    
    Option Compare Database
    Option Explicit
    
    
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    
    Private Type Body
      database As DAO.database      ''--database object
      databaseFile As String        ''--local database file
    
      EOL As String                 ''--end of line
    
      lastPDF As String             ''--last launched PDF file [diagnostic]
    End Type
    
    Private Const moduleName = "Windoze Support"
    
    
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    
    Private this As Body
    
    
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    
    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 Class_Initialize()
      Set this.database = Application.CurrentDb
      this.databaseFile = this.database.name
      this.EOL = VBA.vbCrLf  'ASCII.CRLF
      this.lastPDF = "<invalid>"
    End Sub
    
    
    Private Sub Class_Terminate()
      'MsgBox ("Terminating module: " & moduleName)
    End Sub
    
    
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    
    Property Get databaseFile() As String
    ''
    ''  accessor for this.databaseFile
    ''
      databaseFile = this.databaseFile
    End Property
    
    
    Property Let databaseFile(ByVal value As String)
    ''
    ''  accessor for this.databaseFile
    ''
      this.databaseFile = value
    End Property
    
    
    Property Get lastPDF() As String
      lastPDF = this.lastPDF
    End Property
    
    
    ''====================================================================
    ''====================================================================
    ''====================================================================
    
    
    Public Sub showPDF(ByVal fileName As String)
    ''
    ''  shows the given PDF file using acrobat
    ''
    ''  inputs:
    ''    fileName - fully qualified file name to show
    ''               (ex: C:/tmp/my.PDF)
    ''  raises:
    ''    true  - unqualified success
    ''    false - otherwise
    ''
      ''--local variables and definitions
      Dim pdf As String
      Dim run As Boolean
      Dim seResult As Long
    
      ''--initialize
      pdf = fileName
      run = True
    
      ''--slight diagnostic here...
      this.lastPDF = fileName
    
      ''--attempt liftoff
      If (run) Then
        seResult = ShellExecute(0, "OPEN", pdf, "", "", 0)
    
        If (32 >= seResult) Then
          run = False
          Err.Raise 0, moduleName, "ShellExecute failed with code: " & seResult
        End If
      End If
    
      ''--end of compilation unit with implicit return
    End Sub
    
    
    ''====================================================================
    ''====================================================================
    ''====================================================================
    IMPORTANT: the first is a "Module", and the second a "Class Module".

    Then, in the "Immediate" window of the VBA interface window, I typed the following to demonstrate my code launch.
    Code:
    CALL [Code Test].TEST_showPDF
    I sure hope this helps.

    Luck!

    Comment

    • Nick Cooper
      New Member
      • Jul 2010
      • 44

      #17
      Thanks Oralloy and ADezii, for spending your valuable time on this. My problem (or at least one of them!) is that I really don't understand how and where to put this code. I am using Access 2007 and enter code by going into form design and then 'view code' so whilst I can generate code in 'Class' modules, I'm not clear on where to create a Standard Module, which I guess is why I had no joy with your suggestion at #9, ADezii. It just stopped me opening the form with the error 'No Current Record'. This is the code I used:
      Code:
      Public 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 ShowCmd As Long) As Long
      
      Option Compare Database
      
      Private Sub Form_DblClick(Cancel As Integer)
      Dim strFile As String
      Dim lngErr As Long
      strFile = "C:\Wheelbase\Invoices\" & Me!Account & "-" & Me![Inv No] & ".pdf"  
      lngErr = ShellExecute(0, "OPEN", strFile, "", "", 0)
      Unless you can see an obvious problem then I think it's only fair on you both that I do some more reading up before I waste any more of your time.
      Nick

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #18
        Nick,

        When you are editing your VB code, there should be a "Project Explorer" window.

        If not, you can show it by using the "View|Proje ct Explorer" menu option.

        It should show several folders, of which two are "Modules" and "Class Modules". You'll see your application's modules under these folders.

        To create a new module, just right mouse ("click") anywhere in the window, and select the "Insert" option from the pop-up menu. From the "Insert" menu, you can insert a "Module" or "Class Module", depending on what you need todo.

        Generally anything you insert will be given a generic (and fairly worthless) name like "Module1". You can change the name through the properties window.

        If the properties window isn't showing, you can show it by using the "View|Propertie s Window" menu option.

        BTW, my examples are often wordy, as I tend to build support skeletons around my code, or I steal some of my existing code and delete meat from the bones (the support skeleton). If you don't want the baggage, just remove it - after all, it's your code now, and I'm not proud.

        Luck!

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #19
          The best thing that I can do at this point is to create a simple Demo for you, parallelling your situation as much as possible. In this manner, you can see exactly what is going on, and how it is accomplished. I'll have one for you in a day or two. Stay tuned...

          Scratch the above! I just created a Demo for you in World Record Time.
          1. Download the Attachment
          2. Copy the *.pdf Files to the C:\Wheelbase\In voices\ Directory
          3. Open the Demo Database
          4. There are 3 Dummy Records, click on the Command Button to Open any 1 of the 3 corresponding .pdfs
          5. Play with the Code, and if you have any questions, please feel free to ask.
          Attached Files

          Comment

          • Nick Cooper
            New Member
            • Jul 2010
            • 44

            #20
            Bravo! Oralloy, your advice on where to put a standard module has enabled me to use ADezii's original code in #9 which now works a treat. Sorry A for the extra work you put in last night. This question about standard modules has baffled me for many months despite searching high and low in Access Help for advice. So I am most grateful to you both for solving this particular problem and must divide the credit equally. Is it allowed for me to tick 'best answer' for both of you? Thanks also to NeoPa for his inputs along the way, which I am now better equipped to take on board.
            Cheers
            Nick

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #21
              Oralloy deserves most of the credit, kindly allot him the Best Answer.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #22
                ADezii is generous to a fault. It's a long term affliction he's had since I first knew him :-D

                @Nick:
                Anyway, it's quite gratifying to see how much you've progressed in a single thread. Good for you :-)

                PS. I'm afraid it's not possible to assign Best Answer to more than one post. I know both have been very helpful, but I suggest you look at the original question, and try to determine which single post goes furthest towards answering that. Think in terms of others following in your footsteps and wanting an answer for a similar problem.
                Last edited by NeoPa; Nov 4 '10, 02:19 AM.

                Comment

                Working...