Code Red > VB(A)

Two Modules Work Great Alone But Not Together

(1/2) > >>

Bill Tillman:
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: ---------- 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 ------------------------------------------------------------------
--- End code ---

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.

CmdrDuh:
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

BlackBox:
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: ---(vl-load-com)
((lambda ( key )
  (cond
    ((vl-string-search "R17.2" key)
      ;; <- 2009
      )
    ((vl-string-search "R16.1" key)
      ;; <- 2005
      )
    )
  )
  (vlax-product-key))

--- End code ---

Bill Tillman:
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.

M@yhem:
Here's another way to get the version from VBA.

http://www.visiblevisual.com/index.php/AutoCad-VB/VBA/get-autocad-version.html

Navigation

[0] Message Index

[#] Next page

Go to full version