Message 1 of 5
Opening AutoCAD documents from Excel VBA

Not applicable
08-08-2002
12:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have an excel spreadsheet with drawing file names and verbal descriptions am developing an application to open a selected drawing from this spreadsheet (using VBA). I would like the drawing to appear on screen when it is opened. Everything works fine for the first drawing. When I try to open a second drawing, it usually doesn't open unless I single step through the macro. On the occasions when it does open, it is not displayed on the screen. Any ideas on how I can get this sucker working. The code is shown below.
Sub DWG_Open()
'Routine to open an AutoCAD file from an index. Index must
' be in a format created by file CAD_NDX.xls. In the
' index, select the cell containing the file name of
' the drawing to be opened. Click "Open" button.
Dim AcadDoc As AcadDocumentDim FType As String, RowNum As Long, Col_SD As Long, Col_File As Long
Dim M_Dir As String, S_Dir As String, FileNm As String, PathNm As String
FType = ".dwg"
Col_SD = 1 'Subdirectory column
Col_File = 2 'File Name Column
RowNum = Selection.Row
M_Dir = Trim(Range("IndexPath").Text) 'Get Main directory
If Right(M_Dir, 1) = "\" Then M_Dir = Left(M_Dir, Len(M_Dir) - 1) 'Trim backslash
S_Dir = Trim(Cells(RowNum, Col_SD).Text) 'Get Sub directory
If Right(S_Dir, 1) = "\" Then S_Dir = Left(S_Dir, Len(S_Dir) - 1) 'Trim backslashes
If Left(S_Dir, 1) = "\" Then S_Dir = Right(S_Dir, Len(S_Dir) - 1) 'in sub directory
FileNm = Trim(Cells(RowNum, Col_File).Text) 'Get File Name
PathNm = M_Dir + "\"
If S_Dir <> "" Then PathNm = PathNm + S_Dir + "\" 'Add Sub Dir if not blank
FileNm = PathNm + FileNm + FType 'Get completed filename
Set AcadDoc = GetObject(FileNm) ' Open DrawingEnd Sub
Sub DWG_Open()
'Routine to open an AutoCAD file from an index. Index must
' be in a format created by file CAD_NDX.xls. In the
' index, select the cell containing the file name of
' the drawing to be opened. Click "Open" button.
Dim AcadDoc As AcadDocumentDim FType As String, RowNum As Long, Col_SD As Long, Col_File As Long
Dim M_Dir As String, S_Dir As String, FileNm As String, PathNm As String
FType = ".dwg"
Col_SD = 1 'Subdirectory column
Col_File = 2 'File Name Column
RowNum = Selection.Row
M_Dir = Trim(Range("IndexPath").Text) 'Get Main directory
If Right(M_Dir, 1) = "\" Then M_Dir = Left(M_Dir, Len(M_Dir) - 1) 'Trim backslash
S_Dir = Trim(Cells(RowNum, Col_SD).Text) 'Get Sub directory
If Right(S_Dir, 1) = "\" Then S_Dir = Left(S_Dir, Len(S_Dir) - 1) 'Trim backslashes
If Left(S_Dir, 1) = "\" Then S_Dir = Right(S_Dir, Len(S_Dir) - 1) 'in sub directory
FileNm = Trim(Cells(RowNum, Col_File).Text) 'Get File Name
PathNm = M_Dir + "\"
If S_Dir <> "" Then PathNm = PathNm + S_Dir + "\" 'Add Sub Dir if not blank
FileNm = PathNm + FileNm + FType 'Get completed filename
Set AcadDoc = GetObject(FileNm) ' Open DrawingEnd Sub