Results 1 to 4 of 4
  1. #1
    HS_1 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    87

    compile error, OpenNativeApp


    HTML Code:
    I had a problem before with shell function under another thread. Have got a code from ranman256 (thanks for the code by the way) to use instead of shell function. Wonder what I am doing wrong here that I can’t pass Public Sub OpenNativeApp?. Each time I try to run the code I am getting compile error: “Sub or Function not defined” and the code stops at “r = StartDoc(psDocName)”
    Below is the original code and my button to run it.
    Code:
    'Attribute VB_Name = "modNativeApp"Option Compare Database
    Option Explicit
    
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Const SW_SHOWNORMAL = 1
    Const SE_ERR_FNF = 2&
    Const SE_ERR_PNF = 3&
    Const SE_ERR_ACCESSDENIED = 5&
    Const SE_ERR_OOM = 8&
    Const SE_ERR_DLLNOTFOUND = 32&
    Const SE_ERR_SHARE = 26&
    Const SE_ERR_ASSOCINCOMPLETE = 27&
    Const SE_ERR_DDETIMEOUT = 28&
    Const SE_ERR_DDEFAIL = 29&
    Const SE_ERR_DDEBUSY = 30&
    Const SE_ERR_NOASSOC = 31&
    Const ERROR_BAD_FORMAT = 11&
    Code:
    Public Sub OpenNativeApp(ByVal psDocName As String)Dim r As Long, msg As String
    r = StartDoc(psDocName)
    If r <= 32 Then
        'There was an error
        Select Case r
            Case SE_ERR_FNF
                msg = "File not found"
            Case SE_ERR_PNF
                msg = "Path not found"
            Case SE_ERR_ACCESSDENIED
                msg = "Access denied"
            Case SE_ERR_OOM
                msg = "Out of memory"
            Case SE_ERR_DLLNOTFOUND
                msg = "DLL not found"
            Case SE_ERR_SHARE
                msg = "A sharing violation occurred"
            Case SE_ERR_ASSOCINCOMPLETE
                msg = "Incomplete or invalid file association"
            Case SE_ERR_DDETIMEOUT
                msg = "DDE Time out"
            Case SE_ERR_DDEFAIL
                msg = "DDE transaction failed"
            Case SE_ERR_DDEBUSY
                msg = "DDE busy"
            Case SE_ERR_NOASSOC
                msg = "No association for file extension"
            Case ERROR_BAD_FORMAT
                msg = "Invalid EXE file or error in EXE image"
            Case Else
                msg = "Unknown error"
        End Select
    '    MsgBox msg
    End If
    End Sub
    Code:
    Private Sub Command273_Click()OpenNativeApp "C:\TempFolder"
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    7,997

    Private Sub Command273_Click()
    OpenNativeApp "C:\TempFolder"
    End Sub

    that is correct IF 'TempFolder' exists.

    NOTE: the code shown for OpenNativeApp should be in 1 single subroutine. The website keeps breaking it into 2 blocks.

    StartDoc , is its own routine too.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    7,997
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Const SW_SHOWNORMAL = 1
    Const SE_ERR_FNF = 2&
    Const SE_ERR_PNF = 3&
    Const SE_ERR_ACCESSDENIED = 5&
    Const SE_ERR_OOM = 8&
    Const SE_ERR_DLLNOTFOUND = 32&
    Const SE_ERR_SHARE = 26&
    Const SE_ERR_ASSOCINCOMPLETE = 27&
    Const SE_ERR_DDETIMEOUT = 28&
    Const SE_ERR_DDEFAIL = 29&
    Const SE_ERR_DDEBUSY = 30&
    Const SE_ERR_NOASSOC = 31&
    Const ERROR_BAD_FORMAT = 11&


    Public Sub OpenNativeApp(ByVal psDocName As String)
    Dim r As Long, msg As String
    r = StartDoc(psDocName)
    If r <= 32 Then
    'There was an error
    Select Case r
    Case SE_ERR_FNF
    msg = "File not found"
    Case SE_ERR_PNF
    msg = "Path not found"
    Case SE_ERR_ACCESSDENIED
    msg = "Access denied"
    Case SE_ERR_OOM
    msg = "Out of memory"
    Case SE_ERR_DLLNOTFOUND
    msg = "DLL not found"
    Case SE_ERR_SHARE
    msg = "A sharing violation occurred"
    Case SE_ERR_ASSOCINCOMPLETE
    msg = "Incomplete or invalid file association"
    Case SE_ERR_DDETIMEOUT
    msg = "DDE Time out"
    Case SE_ERR_DDEFAIL
    msg = "DDE transaction failed"
    Case SE_ERR_DDEBUSY
    msg = "DDE busy"
    Case SE_ERR_NOASSOC
    msg = "No association for file extension"
    Case ERROR_BAD_FORMAT
    msg = "Invalid EXE file or error in EXE image"
    Case Else
    msg = "Unknown error"
    End Select
    ' MsgBox msg
    End If
    End Sub


    Private Function StartDoc(psDocName As String) As Long
    Dim Scr_hDC As Long
    Scr_hDC = GetDesktopWindow()
    StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:", SW_SHOWNORMAL)
    End Function

  4. #4
    HS_1 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2016
    Posts
    87
    HTML Code:
    Thanks ranman256 one more time, I was missing the last function in module "Private Function StartDoc(psDocName As String", now the code works perfectly)
    Quote Originally Posted by ranman256 View Post
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Const SW_SHOWNORMAL = 1
    Const SE_ERR_FNF = 2&
    Const SE_ERR_PNF = 3&
    Const SE_ERR_ACCESSDENIED = 5&
    Const SE_ERR_OOM = 8&
    Const SE_ERR_DLLNOTFOUND = 32&
    Const SE_ERR_SHARE = 26&
    Const SE_ERR_ASSOCINCOMPLETE = 27&
    Const SE_ERR_DDETIMEOUT = 28&
    Const SE_ERR_DDEFAIL = 29&
    Const SE_ERR_DDEBUSY = 30&
    Const SE_ERR_NOASSOC = 31&
    Const ERROR_BAD_FORMAT = 11&


    Public Sub OpenNativeApp(ByVal psDocName As String)
    Dim r As Long, msg As String
    r = StartDoc(psDocName)
    If r <= 32 Then
    'There was an error
    Select Case r
    Case SE_ERR_FNF
    msg = "File not found"
    Case SE_ERR_PNF
    msg = "Path not found"
    Case SE_ERR_ACCESSDENIED
    msg = "Access denied"
    Case SE_ERR_OOM
    msg = "Out of memory"
    Case SE_ERR_DLLNOTFOUND
    msg = "DLL not found"
    Case SE_ERR_SHARE
    msg = "A sharing violation occurred"
    Case SE_ERR_ASSOCINCOMPLETE
    msg = "Incomplete or invalid file association"
    Case SE_ERR_DDETIMEOUT
    msg = "DDE Time out"
    Case SE_ERR_DDEFAIL
    msg = "DDE transaction failed"
    Case SE_ERR_DDEBUSY
    msg = "DDE busy"
    Case SE_ERR_NOASSOC
    msg = "No association for file extension"
    Case ERROR_BAD_FORMAT
    msg = "Invalid EXE file or error in EXE image"
    Case Else
    msg = "Unknown error"
    End Select
    ' MsgBox msg
    End If
    End Sub


    Private Function StartDoc(psDocName As String) As Long
    Dim Scr_hDC As Long
    Scr_hDC = GetDesktopWindow()
    StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:", SW_SHOWNORMAL)
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 07-24-2013, 02:01 PM
  2. Compile Error: Syntax Error in DoCmd.RunSQL Statement
    By Evilferret in forum Programming
    Replies: 1
    Last Post: 08-27-2012, 12:32 PM
  3. Replies: 2
    Last Post: 06-23-2012, 11:59 PM
  4. Replies: 7
    Last Post: 06-08-2012, 09:55 PM
  5. Replies: 6
    Last Post: 09-28-2011, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums