Originally Posted by
david.hughes
Thanks for that example. Looks similar to the Espinosa tutorial examples. Which part would I modify to call for a specific excel template file as opposed to just a new worksheet?
Regards,
Dave
Hi again
Here are 2 examles on this theme:
The first one will open template file to write data
after program would ended you need to save it manually
The fsecond one allows to user automatically save a file
in the same folder where is located current drawing
Hope this helps
f.
Code:
Option Explicit
Public Excel As Object
Public ExcelWorkbook As Object
Public ExcelSheet As Object
'\\' to write LWPOLYLINE properties to Excell
Sub WriteProps2Excel2()
Dim plineObj As AcadEntity
Dim basepnt As Variant
Dim oProps As Variant
On Error GoTo WhereIsProblem
ThisDrawing.Utility.GetEntity plineObj, basepnt, vbCr & "Select polyline :"
Dim props As Variant
If TypeOf plineObj Is AcadLWPolyline Then
ReDim oProps(4, 1)
With plineObj
oProps(0, 0) = "Layer": oProps(0, 1) = .Layer
oProps(1, 0) = "Color": oProps(1, 1) = .color
oProps(2, 0) = "Linetype": oProps(2, 1) = .Linetype
oProps(3, 0) = "LinetypeScale": oProps(3, 1) = .LinetypeScale
oProps(4, 0) = "Lineweight": oProps(4, 1) = .Lineweight
'\\' etc...
End With
Else
MsgBox "This is not a polyline"
Exit Sub
End If
Dim ColNum As Integer
Dim RowNum As Integer
On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set Excel = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel!", vbCritical
End
End If
End If
Excel.Visible = True
Set ExcelWorkbook = Excel.Workbooks.Add
Set ExcelWorkbook = Excel.Workbooks.Open("C:\Documents and Settings\admin\Application Data\Microsoft\Øàáëîíû\Ptable.xlt")
Set ExcelSheet = ExcelWorkbook.Worksheets(1)
ExcelSheet.Name = "Lwpolyline Properties"
ColNum = 2
For RowNum = 1 To UBound(oProps, 1) + 1
ExcelSheet.Cells(RowNum, ColNum - 1).Value = CStr(oProps(RowNum - 1, 0))
ExcelSheet.Cells(RowNum, ColNum).Value = CStr(oProps(RowNum - 1, 1))
Next RowNum
WhereIsProblem:
MsgBox Err.Description
End Sub
Code:
Option Explicit
Public Excel As Object
Public ExcelWorkbook As Object
Public ExcelSheet As Object
'\\' to write LWPOLYLINE properties to Excell
Sub WriteProps2Excel3()
Dim plineObj As AcadEntity
Dim basepnt As Variant
Dim oProps As Variant
On Error GoTo WhereIsProblem
ThisDrawing.Utility.GetEntity plineObj, basepnt, vbCr & "Select polyline :"
Dim props As Variant
If TypeOf plineObj Is AcadLWPolyline Then
ReDim oProps(4, 1)
With plineObj
oProps(0, 0) = "Layer": oProps(0, 1) = .Layer
oProps(1, 0) = "Color": oProps(1, 1) = .color
oProps(2, 0) = "Linetype": oProps(2, 1) = .Linetype
oProps(3, 0) = "LinetypeScale": oProps(3, 1) = .LinetypeScale
oProps(4, 0) = "Lineweight": oProps(4, 1) = .Lineweight
'\\' etc...
End With
Else
MsgBox "This is not a polyline"
Exit Sub
End If
Dim fName As String
Dim ColNum As Integer
Dim RowNum As Integer
On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set Excel = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Could not start Excel!", vbCritical
End
End If
End If
Excel.Visible = True
Set ExcelWorkbook = Excel.Workbooks.Add
Set ExcelSheet = ExcelWorkbook.Worksheets(1)
fName = ThisDrawing.GetVariable("DWGPREFIX")
fName = fName & ThisDrawing.GetVariable("DWGNAME")
fName = Left$(fName, Len(fName) - 4) & ".xls"
ExcelWorkbook.SaveAs fName
ExcelWorkbook.Close
Set ExcelWorkbook = Excel.Workbooks.Open(fName)
Set ExcelSheet = ExcelWorkbook.Worksheets(1)
Excel.DisplayAlerts = False
ExcelSheet.Name = "Lwpolyline Properties"
ColNum = 2
For RowNum = 1 To UBound(oProps, 1) + 1
ExcelSheet.Cells(RowNum, ColNum - 1).Value = CStr(oProps(RowNum - 1, 0))
ExcelSheet.Cells(RowNum, ColNum).Value = CStr(oProps(RowNum - 1, 1))
Next RowNum
Set ExcelSheet = Nothing
ExcelWorkbook.Close SaveChanges:=True
Set ExcelWorkbook = Nothing
Excel.Quit
Set Excel = Nothing
WhereIsProblem:
MsgBox Err.Description
End Sub