Code to get Excel cell data as MText source

Discussion in 'AutoCAD' started by Bob Anderson, Aug 6, 2003.

  1. Bob Anderson

    Bob Anderson Guest

    Can anyone see the reason Excel is remaining "hooked" open after running the
    following code?

    Private Sub cmdXL_CAD_Click()
    Dim objExcel As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim fName As String 'Excel File as source
    Dim minExt As Variant 'mText bounding box
    Dim maxExt As Variant 'mText bounding box
    Dim MTextObject As AcadMText
    Dim dblInsPoint(0 To 2) As Double 'mText insertion point
    Dim strContents As String 'will be Excel cell contents

    ' Below is for office computer - hardwire file for testing
    fName = "C:\user\dwgs\VisBasic\Projects\Genotes\ExcelWks.xls"
    ' Below is for home computer - hardwire file for testing
    ' fName = "C:\user\drawings\Projects\win-dxf\ExcelWks.xls"

    Let dblInsPoint(0) = 0: dblInsPoint(1) = 0: dblInsPoint(2) = 0 'mText
    insertion point
    Set objExcel = New Excel.Application 'create instance of Excel
    Set objBook = objExcel.Workbooks.Open(fName)
    Set objSheet = objBook.Worksheets(1)
    Let strContents = objSheet.Range("A1").Value
    Set MTextObject = ThisDrawing.ModelSpace.AddMText(dblInsPoint, 1,
    strContents)
    Let MTextObject.Height = 0.125 'This is
    optional
    Let MTextObject.AttachmentPoint = acAttachmentPointTopLeft 'This is
    optional
    ' Bounding box stuff is optional - z coordinate [minExt(2)] is left off
    MTextObject.GetBoundingBox minExt, maxExt
    MsgBox "The extents of the bounding box for the mtext are:" & vbCrLf _
    & "Min Extent: " & minExt(0) & "," & minExt(1) _
    & vbCrLf & "Max Extent: " & maxExt(0) & "," & maxExt(1),
    vbInformation, _
    "GetBoundingBox Example"
    objExcel.Workbooks.Close
    Excel.Application.Quit
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objExcel = Nothing
    Set MTextObject = Nothing
    ' Unload Me 'Unload form from memory
    End Sub
     
    Bob Anderson, Aug 6, 2003
    #1
  2. Try connecting to Excel like this:

    ' Open a connection to Microsoft Excel   On Error Resume Next   Set Excel = GetObject(, "Excel.Application")   If Excel Is Nothing Then     Err.Clear     ExcelWasRunning = False     Set Excel = GetObject("", "Excel.Application")     If Err.Number <> 0 Then       Err.Clear       MsgBox "Error: Put some message here"       Set Excel = Nothing       Exit Function     End If   Else     ExcelWasRunning = True   End If

    ' Do your stuff here.

    If Not ExcelWasRunning Then     Excel.Application.Quit   End If
     
    Mark_Abercrombie, Aug 6, 2003
    #2
  3. Bob Anderson

    Bob Anderson Guest

    That is good. I am not complaining. I'm thankful for the quick attention.
    Seems I always reply to my own posts. I prefer using Options Explicit, so
    tried to work with what I had. The code written was pieced together from
    several examples. I should have used objExcel.Application.Quit instead of
    Excel.Application.Quit near the bottom of my code.

    What are the chances anyone can respond to why the code works with Excel 8.0
    library reference ('97) but gives an object library feature not supported
    error with Excel 9.0 library reference (2000)?

    Try connecting to Excel like this:

    ' Open a connection to Microsoft Excel
    On Error Resume Next
    Set Excel = GetObject(, "Excel.Application")
    If Excel Is Nothing Then
    Err.Clear
    ExcelWasRunning = False
    Set Excel = GetObject("", "Excel.Application")
    If Err.Number <> 0 Then
    Err.Clear
    MsgBox "Error: Put some message here"
    Set Excel = Nothing
    Exit Function
    End If
    Else
    ExcelWasRunning = True
    End If ' Do your stuff here. If Not ExcelWasRunning Then
    Excel.Application.Quit
    End If
     
    Bob Anderson, Aug 8, 2003
    #3
  4. Bob Anderson

    Bob A. Guest

    Ya'all please forgive me for replying to my own post (maybe someone else is
    reading this) , but now I see it. Here's a well deserved plug for
    www.dicks-clicks.com for Late Binding. That's was your point, huh, Mark?
     
    Bob A., Aug 10, 2003
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.