Access to start application

NigelShaw

Registered User.
Local time
Today, 08:41
Joined
Jan 11, 2008
Messages
1,572
Hi,

can access be programmed to start an outside application when a button is clicked like AutoCAD or something to that effect?

regs,


nigel
 
Look at Shell and FollowHyperlink in VBA help.
 
Can access be programmed to start an outside application when a button is clicked like AutoCAD or something to that effect?

Sorry for rescuscitating an old thread, but i found it whilst looking for a related topic and thought someone might find my solution useful.

As PBaldy suggests, Shell or FollowHyperlink could both be used to good effect, but I find this better to use where you then want to do something with the drawing once it is open.

It first checks to see if AutoCAD is already running, and if not then it launches it. But not only does it open AutoCAD, it opens a specific drawing if it's not already open or switches to said drawing if it is open.

Put this code into a sub for the OnClick of your 'Open Drawing' button.

Code:
'get your filename from a field called DWG
Me.DWG.SetFocus
strDWGNo = Me.DWG.Text
 
'Switch to AutoCAD if it's already running
On Error Resume Next
Set objAcadApp = GetObject(, "AutoCAD.Application")
 
'If AutoCAD is not running then launch it
If Err <> 0 Then
    Set objAcadApp = CreateObject("AutoCAD.Application")
    objAcadApp.Visible = True
End If
 
'Get the current collection of documents open in AutoCAD
Set objAcadDocs = objAcadApp.Documents
 
'Iterate the collection to look for desired drawing
For Each objEachDwg In objAcadDocs
    If objEachDwg.Name = strDWGName Then
        Set objThisDwg = objEachDwg
        varDWGPresent = 1
        Exit For
    Else
        varDWGPresent = 0
    End If
Next
 
'If your drawing is not found, then open it
If varDWGPresent = 0 Then
 
    'insert your drawing path here        
    strPath = "C:\DWG\"
 
    objAcadApp.Documents.Open (strPath & strDWGName)
 
    Set objAcadDocs = objAcadApp.Documents
    For Each objEachDwg In objAcadDocs
         If objEachDwg.Name = strDWGName Then
            Set objThisDwg = objEachDwg
            Exit For
         Else
         End If
    Next
Else
End If
 
objThisDwg.Activate

Please note, this code also puts the drawing itself into an object called 'objThisDwg' which can then be used to manipulate data within the drawing. I use this as the beginning of an app that extracts attributes from the drawing files.

Hope this helps.

Cheers,...Jon.
 
Incidentally:

I came across the following page when trying to run programs from Access. The function described will start anything including shortcuts with parameters.

http://www.suodenjoki.dk/us/productions/articles/vbashellexecute.htm

I used in combination with a compiled AutoIT script to test for a particular instance of IE running and bring it to the front if found or start IE on the page if it didn't. After much research I had been unable to get Access to recognise the IE instance required.

AutoIT is easy to use, very powerful and free. If you can't get Access to do what you want, just put it in an AutoIT script and run that from Access instead. I find the language is simpler than VB.

http://www.autoitscript.com/autoit3/
 
Thanks Galaxiom,

I haven't tested it yet, but I would go so far as to say that the first of your suggestions would be the ideal approach for opening AutoCAD if you didn't need to manipulate the drawing file afterwards. In fact, it may even be the best regardless, as my approach can easily be adapted to work with it and find the open session of AutoCAD and then open the drawing.

The reason I say this is because of it's ability to work with link files (shortcuts). Anyone familiar with AutoCAD in the last few years will be aware of it's "profiles" and it's "flavours". All of these can be loaded (or not) by command line switches in the shortcut.

The downside to my approach is that it relies on the OS remembering which version of AutoCAD was used last and opening that. This is actually a problem if you are manipulating the drawing file with code once it's open, and if you have more than one version of AutoCAD installed on your machine (eg AutoCAD 2008 and AutoCAD 2009 on the same machine) as you can only load one AutoCAD type library at a time in your references and so your code can only be compatible with one version of AutoCAD at any given time. Thus, if you opened the wrong version last time (which is quite likely when you have eight different versions of AutoCAD installed like I do) then your code will fall over before it even opens the session properly. (It will launch the acad.exe, but it won't execute past the 'objAcadApp.Visible = True' call, thus leaving it as an orphaned process unnecessarily hogging about 150Mb of your RAM)

The ShellExecute approach, however, allows you to specify exactly which version of AutoCAD, and which profile, you want to launch from your application.

So, thankyou again Galaxiom - you have just given me a solution I wasn't looking for to a problem I hadn't tried to solve. (But saved me a lot of trouble in future by doing so! :D )
 
you have just given me a solution I wasn't looking for to a problem I hadn't tried to solve. (But saved me a lot of trouble in future by doing so! :D )

I have lately found myself visiting this forum more frequently for exactly this kind of serendipity.

I have learnt several functions and techniques I would not have known about except for coming across someone posting a problem they had experienced using them.
 
OK, well I've had some time to fiddle with it, and the ShellExecute method works great but there is one thing you need to be aware of:

If you are using it to launch the app then connect to it with VBA, then you need make sure the app is launched before you try to make the connection. (Sounds obvious I know, but the result you get otherwise can be somewhat unexpected.)

I achieved this with AutoCAD by inserting the following code:

Code:
While objAcadApp.Name <> "AutoCAD"
    Set objAcadApp = GetObject(, "AutoCAD.Application")
    objAcadApp.WindowState = acMax
Wend

immediately after the ShellExecute.

i.e. it now reads:

Code:
    Set objAcadApp = GetObject(, "AutoCAD.Application")
    If Err <> 0 Then
 
        On Error Resume Next
        RetVal = ShellExecute(hwnd, "open", "C:\Program Files\AutoCAD 2010\acad.exe", "", _
                 "C:\DWG\", SW_SHOWMAXIMISED)
 
        While objAcadApp.Name <> "AutoCAD"
            Set objAcadApp = GetObject(, "AutoCAD.Application")
            objAcadApp.WindowState = acMax
        Wend
 
        objAcadApp.Visible = True
 
    End If

Without this check I was getting a mix of odd results, most often the 'acad.exe' process would launch invisibly and I'd be forced to kill it manually through task manager.

(Interestingly, though, if I ran the function a second time after the first try launced it invisibly it would still open the required file quite happily and extract the relevant data as required, but I still didn't have a front end with which to close it. I suppose it could be closed with code as well, making the whole process of the data extraction quicker and less resource intensive due to the lack of graphic interface.)

Hopefully this will help someone out one day. :)

Cheers,...Jon.
 

Users who are viewing this thread

Back
Top Bottom