VBA basics - How to read excel cells value?

VBA basics - How to read excel cells value?

Anonymous
Not applicable
6,184 Views
2 Replies
Message 1 of 3

VBA basics - How to read excel cells value?

Anonymous
Not applicable
Hi,
I just started using VBA in AutoCAD. I need to do this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. I got an opened excel worksheet named "TEST", from AutoCAD VBA I need to read cell B1 value and export this value to string.
2. Save the string as a text on the drawing.
3. Read B2 value export to string and save as text on the drawing.
and so on till B8
sounds easy?but not for me
Could anyone help me with this plz
thx
0 Likes
6,185 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

Attached is a tutorial that I got from somewhere. I
think it was AUGI but not sure. It deals with Autocad VBA to Excel.

 

This is the code I used to create Dtext in
Autocad.

<code>

 If obRoofOver6in.Value = True
Then
     Dim textObj As
AcadText
     Dim TextString As
String
     Dim instPT(0 To 2) As
Double
     Dim height As
Double
     TextString =
Label31.Caption
     instPT(0) = 2138: instPT(1) = 242.1:
instPT(2) = 0
     height = 4
    
Set textObj = ThisDrawing.ModelSpace.AddText(TextString, instPT,
height)
     textObj.Layer =
"SL_NTS"
     textObj.color =
92
     textObj.StyleName = "DIN"

</code>

 

 
0 Likes
Message 3 of 3

HJohn1
Advocate
Advocate
Here is a little routine you can try. It is no the best sample or a tutorial, but something simple to get you thinking. Create an Excel file and save it as "C:\My Documents\TestBook.xls", name the first WorkSheet "Test" and write something in cells B1:B8. Open AutoCAD create a new VBA project and write the following code in a code module or ThisDrawing object. Create a new drawing with the imperial template. Finally, run the routine. One more thing forgot, you also need to add reference to your excel application to the VBA project.

{code}Public Sub GetExcelInfo()
Dim exapp As Excel.Application
Dim wsheet As Excel.Worksheet
Dim cll As Excel.Range
Dim rg As Excel.Range
Dim pt(2) As Double


Set exapp = CreateObject("Excel.Application")

exapp.Visible = True

exapp.Workbooks.Open ("C:\My Documents\TestBook.xls")

Set wsheet = exapp.ActiveSheet

If wsheet.Name = "Test" Then

Set rg = wsheet.Range(Cells(1, 2), Cells(8, 2))

rg.Select


pt(0) = 0: pt(1) = 0: pt(2) = 0

For Each cll In rg
ThisDrawing.ModelSpace.AddText cll.Value, pt, 0.16
pt(0) = pt(0) + 1
pt(1) = pt(1) + 1
Next cll

End If

If Not exapp Is Nothing Then
exapp.Quit
End If

ThisDrawing.Regen (acActiveViewport)

End Sub{code}

Edited by: HJohn1 on Mar 4, 2009 10:57 PM Edited by: HJohn1 on Mar 5, 2009 1:47 AM
0 Likes