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.
OK I took out anything that could get me into dutch and here is a composite of what I'm trying to accomplish
------- 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.