Author Topic: Two Modules Work Great Alone But Not Together  (Read 3379 times)

0 Members and 1 Guest are viewing this topic.

Bill Tillman

  • Newt
  • Posts: 90
Two Modules Work Great Alone But Not Together
« on: January 19, 2012, 04:04:49 PM »
I have a two stage program working. Can't go into great detail due to confidentialty agreement so I'll try to explain breifly.

There are AutoCAD 2005 and AutoCAD 2009 machines in this place. The Excel VBA program I've written launches AutoCAD and runs a VLISP file which edits a drawing. Works great on my machine.

Now move the process to a user with a different verions of AutoCAD. It depends on the correct Reference Types being enabled with the Excel program. The ones for AutoCAD 2005 don't work with 2009 and visa-versa. I have come up with a way to figure out which version of AutoCAD is on the machine and load the correct References.

So I try to put the two parts together to work as one process. Set the References then run AutoCAD... Not so easy. I split the portions of the code into two different modules. I can run the first part which sets the references. I can then run the second part which does the AutoCAD thing. So I think great I will place a call statement at the end of the first module to call the main function in the second module. But when i do this it's like all the code in the first part get's skipped over. When I step through (F8) the program it jumps straight to the Dim statements in the second part and since the References aren't enabled, it crashes right there.   :roll:

OK I took out anything that could get me into dutch and here is a composite of what I'm trying to accomplish

Code: [Select]
------- BEGIN MODULE 1  ------------------------------------------------------------------

Sub AddReference()

    Dim strGUID1 As String, strGUID2 As String, theRef As Variant, i As Long
    Dim ACADVersion As String
   
'************************************************************************
' OPEN TEXT FILE ON USERS' HOME DRIVE AND GET ACAD VERSION NUMBER
'************************************************************************

Open "H:\ACADVersion.txt" For Input As #1
Input #1, ACADVersion
Close #1

If ACADVersion = 2009 Then
   strGUID1 = "{851A4561-F4EC-4631-9B0C-E7DC407512C9}" ' ACAD 2009
   Else
   strGUID1 = "{1EFD8E85-7F3B-48E6-9341-3C8B2F60136B}" ' ACAD 2005
End If

   strGUID2 = "{FC0C6DFB-96E9-4520-B0D2-993650F89DD4}" ' COMMON
     
'    'Set to continue in case of error
    On Error Resume Next
     
     'Remove any missing references
    For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
        Set theRef = ThisWorkbook.VBProject.References.Item(i)
        If theRef.isbroken = True Then
            ThisWorkbook.VBProject.References.Remove theRef
        End If
    Next i
     
     'Clear any errors so that error trapping for GUID additions can be evaluated
    Err.Clear
     
     'Add the reference
    ThisWorkbook.VBProject.References.AddFromGuid _
    GUID:=strGUID1, Major:=1, Minor:=0
   
    ThisWorkbook.VBProject.References.AddFromGuid _
    GUID:=strGUID2, Major:=1, Minor:=0
     
     'If an error was encountered, inform the user
    Select Case Err.Number
    Case Is = 32813
         'Reference already in use.  No action necessary
    Case Is = vbNullString
         'Reference added without issue
    Case Else
         'An unknown error was encountered, so alert the user
        MsgBox "A problem was encountered trying to" & vbNewLine _
        & "add or remove a reference in this file" & vbNewLine & "Please check the " _
        & "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
    End Select
    On Error GoTo 0

'Main

End Sub


------- END OF MODULE 1 ------------------------------------------------------------------


------- BEGIN MODULE 2  ------------------------------------------------------------------

Option Explicit
'' Require Reference to:
'' Tools--> References --> AutoCAD 2XXX Type Library
'' Tools--> References --> AutoCAD Focus Control for VBA Type Library
'' and also set options here:
'' Tools--> Options --> General --> Error Trapping -> check 'Break on Unhahdled Errors' button
Dim acApp As AcadApplication
Dim AcDocs As AcadDocuments
Dim acDoc As AcadDocument
Dim acSpace As AcadBlock
Dim acdCap As String, ExcCap As String, copyStr As String
Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long

Sub Main()

'************************************************************************
'*   WRITE THE TEXT FILE IN THE USERS' HOME FOLDER
'************************************************************************
Dim ce As Range
Open "H:\tf.txt" For Output As #1
For Each ce In Worksheets("Sheet1").Range("D5:D6,K6,M6,D8,F12:F19,F21,F23:F27,F29:F30,F32:F33,F35,F38:F39,I17")
    Write #1, ce.Value
Next ce
Close #1

'************************************************************************
'* OPEN AUTOCAD THEN OPEN THE DRAWING FILE, LOAD THE VLISP AND EXECUTE IT
'************************************************************************
Dim strDrawing As String
    On Error Resume Next
    Set acApp = GetObject(, "AutoCAD.Application")
    If Err.Number <> 0 Then
        Err.Clear
        Set acApp = CreateObject("AutoCAD.Application")
    End If
    On Error GoTo Err_Control
    acApp.Visible = True
SetFocus acApp.hwnd
Application.WindowState = xlMinimized
acApp.WindowState = acMax

strDrawing = "C:\Path_to_Dwg.dwg"

Set acDoc = acApp.Documents.Open(strDrawing)
acDoc.Activate
Set acDoc = acApp.ActiveDocument

acDoc.SendCommand ("(load ""MyLisp.lsp"" ""The load failed"") doit" & Chr(13))

Err_Control:

End Sub

------- END OF MODULE 2 ------------------------------------------------------------------

UPDATE:  I got it now....I moved the four Dim statements at the top of module 2 to just below the Sub Main line and it's working now. If anyone has additional advice on this I'd appreciate it.
« Last Edit: January 19, 2012, 05:10:09 PM by Bill Tillman »
Of all the things I've lost, I miss my mind the most!

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: Two Modules Work Great Alone But Not Together
« Reply #1 on: January 19, 2012, 05:58:24 PM »
quick answer but not the best solution, make 2 versions, one that references the 2009 LIB and one that refs the 2005 one.  Put on correct machines, and go from there.  Use your VLisp to determine which version of Acad your running, and then load the correct dvb file.  The macro name can be the same in both dvb files.  HTH
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

BlackBox

  • King Gator
  • Posts: 3745
Re: Two Modules Work Great Alone But Not Together
« Reply #2 on: January 19, 2012, 11:56:54 PM »
You can use vl-string-search on vlax-product-key to test for which version, then conditionally load/run the correct VBA module.

Pseudo code:
Code: [Select]
(vl-load-com)
((lambda ( key )
  (cond
    ((vl-string-search "R17.2" key)
      ;; <- 2009
      )
    ((vl-string-search "R16.1" key)
      ;; <- 2005
      )
    )
  )
  (vlax-product-key))
"How we think determines what we do, and what we do determines what we get."

Bill Tillman

  • Newt
  • Posts: 90
Re: Two Modules Work Great Alone But Not Together
« Reply #3 on: January 20, 2012, 05:15:24 AM »
Hey Render Man, that's pretty cool. Didn't know you could do that from within VLISP itself. Now I don't suppose you could set the Excel References based on this do you? Our goal here is to have one file which knows what to do for which version of AutoCAD the user is running. Which I have achieved now by moving those Dim statements. As you can tell I am still learning. Right now the process works by having only one spreadsheet file, one VLISP file and one dwg file. The bosses are insisting on that.
Of all the things I've lost, I miss my mind the most!

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Two Modules Work Great Alone But Not Together
« Reply #4 on: January 20, 2012, 08:14:16 AM »
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Lee Mac

  • Seagull
  • Posts: 12313
  • London, England
Re: Two Modules Work Great Alone But Not Together
« Reply #5 on: January 20, 2012, 08:23:31 AM »
Or just:

Code - Auto/Visual Lisp: [Select]
  1. (atof (getvar 'ACADVER))

Or

Code - Auto/Visual Lisp: [Select]
  1. (atoi (substr (ver) 13))

BlackBox

  • King Gator
  • Posts: 3745
Re: Two Modules Work Great Alone But Not Together
« Reply #6 on: January 20, 2012, 10:15:48 AM »
Hey Render Man, that's pretty cool. Didn't know you could do that from within VLISP itself.

Thanks Bill - Any of the methods posted here should work for you.

Now I don't suppose you could set the Excel References based on this do you? Our goal here is to have one file which knows what to do for which version of AutoCAD the user is running. Which I have achieved now by moving those Dim statements.

I am not adept at coding in VBA, as I chose to learn Visual LISP, and am now making the jump to .NET development. However, if my limited understanding is correct, you would actually could produce two separate modules (different VBA projects?); one for each AutoCAD version. The LISP would conditionally call the appropriate module version; example:

Quote
(defun c:FOO  ()
  (vl-load-com)
  ((lambda (vrsn macro)
     (cond
       ((= 16.1 vrsn)
        (vl-vbarun (strcat "<FilePath>/<vbaFile2005>.dvb" macro)))
       ((= 17.2 vrsn)
        (vl-vbarun (strcat "<FilePath>/<vbaFile2009>.dvb" macro)))
       ((prompt "\n** This version not supported ** "))
       )
     )
    (atof (getvar 'acadver)) ; <-- ** Cheers, Lee! :beer:
    "!<ModuleName>.<SubName>")
  (princ))


** This is similar to what we do for some legacy VBA routines, given that we work on multiple versions (VBA originally written in-house for 2006, 2009). The LISP is incorporated into menu, toolbar, and ribbon CUI components, and is callable via the command line also.

As you can tell I am still learning. Right now the process works by having only one spreadsheet file, one VLISP file and one dwg file. The bosses are insisting on that.

No worries; we all start somewhere.  :wink:
« Last Edit: January 20, 2012, 10:19:56 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."