Results 1 to 15 of 15

Run code with AcadOle object

Threaded View

EK1 Run code with AcadOle object 04-27-2010, 10:01 AM
shg Re: Run code with AcadOle... 04-27-2010, 10:47 AM
EK1 Re: Run code with AcadOle... 04-27-2010, 10:50 AM
Marcol Re: Need help to run code... 04-28-2010, 07:33 AM
romperstomper Re: Need help to run code... 04-28-2010, 08:46 AM
EK1 Re: Need help to run code... 04-28-2010, 08:55 AM
romperstomper Re: Need help to run code... 04-28-2010, 09:03 AM
EK1 Re: Need help to run code... 04-28-2010, 09:25 AM
Marcol Re: Need help to run code... 04-29-2010, 12:35 PM
EK1 Re: Need help to run code... 04-29-2010, 01:11 PM
Marcol Re: Need help to run code... 04-29-2010, 01:18 PM
EK1 Re: Need help to run code... 04-29-2010, 03:41 PM
Marcol Re: Run code with AcadOle... 04-30-2010, 11:00 AM
  1. #1
    Registered User
    Join Date
    03-01-2010
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    57

    Run code with AcadOle object

    Hi everyone,

    I got help from cadtutor.net where someone posted the code for a short program that is useful to me. One part, however does not work but it apparently works on their computers. I think it has something to do with References.

    Public Sub ExportDims()
     Dim oEnt As AcadEntity
     Dim oDim As AcadDimRotated
     Dim oOle As AcadOle
     Dim mea1 As Double
     Dim mea2 As Double
     Dim pickPt As Variant
    When I run it, I get a User-defined type not defined and the AcadOle line is selected and the Public Sub ExportDims() is highlighted in yellow.

    I have added the following references with no success:

    Microsoft DAO 3.6 Object Library
    Microsoft OLE DB Error Libarary
    oleprn 1.0 Type Library
    Microsoft ActiveX Data Objects 2.7 Library
    OLE Automation

    This is the full code if anyone needs the big picture.
    '' require reference to Microsoft Excel XX.X Object Library
    Option Explicit
    Public Declare Function GetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Public Type OPENFILENAME
      lStructSize As Long
      hwndOwner As Long
      hInstance As Long
      lpstrFilter As String
      lpstrCustomFilter As String
      nMaxCustFilter As Long
      nFilterIndex As Long
      lpstrFile As String
      nMaxFile As Long
      lpstrFileTitle As String
      nMaxFileTitle As Long
      lpstrInitialDir As String
      lpstrTitle As String
      flags As Long
      nFileOffset As Integer
      nFileExtension As Integer
      lpstrDefExt As String
      lCustData As Long
      lpfnHook As Long
      lpTemplateName As String
    End Type
    '@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
    ' Display and use the File open dialog
    '@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
    Public Function ShowOpen() As String
      Dim strTemp As String
      Dim VertName As OPENFILENAME
      VertName.lStructSize = Len(VertName)
      VertName.hwndOwner = ThisDrawing.HWND
      VertName.lpstrFilter = "All Excel Files (*.xls)" + Chr$(0) + _
                             "*.xls" + Chr$(0) + " | " + "Excel Files (*.xlsx)" + Chr$(0) + _
                             "*.xlsx"
      VertName.lpstrFile = Space$(254)
      VertName.nMaxFile = 255
      VertName.lpstrFileTitle = Space$(254)
      VertName.nMaxFileTitle = 255
      VertName.lpstrInitialDir = CurDir
      VertName.lpstrTitle = "Select Excel File"
      VertName.flags = 0
      If GetOpenFileName(VertName) Then
        strTemp = (Trim(VertName.lpstrFile))
        ShowOpen = Mid(strTemp, 1, Len(strTemp) - 1)
      End If
    End Function
    '' Modified 10/8/03 to remove early binding and
    '' include late binding. Now should work with
    '' any version Excel
     
    Function IsExcelRunning() As Boolean
        Dim objXL As Object
        On Error Resume Next
        Set objXL = GetObject(, "Excel.Application")
        IsExcelRunning = (Err.Number = 0)
        Set objXL = Nothing
        Err.Clear
    End Function
    '' based on macros written by Jeff Mishler
    'Changed the way Excel is loaded per suggestion by
    'Randall Rath - http://www.vbdesign.net/
    'which also added the "Function IsExcelRunning"
    Public Sub ExportDims()
     Dim oEnt As AcadEntity
     Dim oDim As AcadDimRotated
     Dim oOle As AcadOle
     Dim mea1 As Double
     Dim mea2 As Double
     Dim pickPt As Variant
     
     ThisDrawing.Utility.GetEntity oEnt, pickPt, vbCrLf & "Select length dimension >> "
        If Not TypeOf oEnt Is AcadDimension Then Exit Sub
        Set oDim = oEnt
        mea1 = oDim.Measurement
     
         ThisDrawing.Utility.GetEntity oEnt, pickPt, vbCrLf & "Select width dimension >> "
        If Not TypeOf oEnt Is AcadDimension Then Exit Sub
        Set oDim = oEnt
        mea2 = oDim.Measurement
     
        ThisDrawing.Utility.GetEntity oEnt, pickPt, vbCrLf & "Select embedded table >> "
        If Not TypeOf oEnt Is AcadOle Then Exit Sub
        Set oOle = oEnt
     
        Dim xlFileName As String
     
        '***Begin code from Randall Rath******
        Dim oXL As Object
        Dim blnXLRunning As Boolean
        blnXLRunning = IsExcelRunning()
        If blnXLRunning Then
            Set oXL = GetObject(, "Excel.Application")
        Else
            Set oXL = CreateObject("Excel.Application")
            oXL.Visible = False
            oXL.UserControl = False
            oXL.DisplayAlerts = False
        End If
        '***End code from Randall Rath******
     
        Dim oWb As Object
        Dim oWs As Object
        xlFileName = ShowOpen()
        Set oWb = oXL.Workbooks.Open(xlFileName)
        If oWb Is Nothing Then
            MsgBox "The Excel file " & xlFileName & " not found" & _
                   "Try again."
            GoTo Exit_Here
        End If
        Set oWs = oWb.Worksheets("Sheet1")
        oWs.Activate
     
        ' write data to Excel
     
        oWs.Columns(1).NumberFormat = "@"
        oWs.Columns(2).NumberFormat = "0.00"
        oWs.Columns(3).NumberFormat = "0.00"
        oWs.Cells(2, 1) = "-001"
        oWs.Cells(2, 2) = mea1
        oWs.Cells(2, 3) = mea2
     
        oWs.Columns.AutoFit
    Exit_Here:
        Set oWs = Nothing
        oWb.Save: oWb.Close
        Set oWb = Nothing
        oXL.Quit
        Set oXL = Nothing
    DoEvents
        MsgBox "Done"
     
    End Sub
    Last edited by EK1; 04-27-2010 at 11:35 AM. Reason: Change in title

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1