Excel VBA, AutoCAD ActiveX ¸Àº¸±â °ÁÂ(17) |
Excel°ú AutoCAD¿¬°á(2)
ÀÏ´Ü ¾îÁ¦ º¸´õ extattr.xlsÀÇ ¸ðµâ¿¡ ÀÖ´Â extract()ÇÔ¼ö ¼Ò½º¸¦ º¸°í ½ÃÀÛÇϵµ·Ï ÇÏ°Ú½À´Ï´Ù. ¾îÁ¦ object¸¦ º¯¼ö¿¡ ³Ö±â À§Çؼ´Â setÀ» »ç¿ëÇÑ´Ù´Â
°ÍÀ» ¸»¾¸µå·È½À´Ï´Ù.
Public acad As Object
Public mspace As Object
Public excel As Object
Public AcadRunning As Integer
Public excelSheet As Object
Sub Extract()
Dim sheet As Object
Dim shapes As Object
Dim elem As Object
Dim excel As Object
Dim Max As Integer
Dim Min As Integer
Dim NoOfIndices As Integer
Dim excelSheet As Object
Dim RowNum As Integer
Dim Array1 As Variant
Dim Count As Integer
Set excel = GetObject(, "Excel.Application")
Worksheets("Attributes").Activate
Set excelSheet = excel.ActiveWorkbook.Sheets("Attributes")
excelSheet.Range(Cells(1, 1), Cells(1000, 100)).Clear
excelSheet.Range(Cells(1, 1), Cells(1, 100)).Font.Bold = True
Set acad = Nothing
On Error Resume Next
Set acad = GetObject(, "AutoCAD.Application")
If Err <> 0 Then
Set acad = CreateObject("AutoCAD.Application")
MsgBox "Open the drawing file first and then rexecute!"
Exit Sub
End If
' acad.Visible = True
Set doc = acad.ActiveDocument
Set mspace = doc.ModelSpace
...ÀÌÇÏ»ý·«...
set¸í·ÉÀ¸·Î º¯¼ö¿¡´Ù °³Ã¼¸¦ Àâ¾Æ³Ö½À´Ï´Ù. getobject°¡ °³Ã¼¸¦ Àâ¾Æ ³Ö´Â °ÍÀä. oop°³³ä¿¡¼ÀÇ °³Ã¼´Â ÇÁ·Î±×·¥¿¡µµ ÇØ´çµË´Ï´Ù. ¸»ÇÏÀÚ¸é
¿¢¼¿À̶õ ÇÁ·Î±×·¥µµ ÇϳªÀÇ °³Ã¼¶ó´Â °ÍÀÌÁö¿ä. excelÀ̶õ ÇÁ·Î±×·¥ÀÇ °³Ã¼ ¹Ø¿¡´Â ÀڽݳüµéÀÌ Àִµ¥, ¿¹¸¦ µé¾î¼ sheet1, sheet2 °°Àº °ÍµéÀÌ
Àڽݳü°¡ µÇ´Â °ÍÀÔ´Ï´Ù. ¶ÇÇÑ sheet1¾Æ·¡¿¡ "A1","B1"°ú °°Àº range°³Ã¼°¡ Àڽݳü·Î Á¸ÀçÇÏ´Â °ÍÀÔ´Ï´Ù. excelÀ̶õ ÇÁ·Î±×·¥À» °³Ã¼·Î Àâ±â À§Çؼ
getobject¶ó´Â ¸í·ÉÀ» »ç¿ëÇÏ´Â °ÍÀÔ´Ï´Ù. ºñ½ÁÇÑ ¸í·ÉÀ¸·Î createobject¶ó´Â °ÍÀÌ Àִµ¥ ÀÌ°ÍÀº ÇÁ·Î±×·¥À» ½ÇÇà½ÃÅ°´Â ¸í·ÉÀÔ´Ï´Ù.
ù¹ø° ÁÙ¿¡ Set excel = GetObject(, "Excel.Application")¶ó°í ½è±â ¶§¹®¿¡ ¾ÕÀ¸·Î´Â excelÀ̶õ ÇÁ·Î±×·¥¿¡ Á¢±ÙÇÏ·Á¸é ¶Ç´Â excel¾Æ·¡ÀÇ ¾î¶²
½¬Æ®³ª cell¿¡ Á¢±ÙÇϱâ À§Çؼ excelÀ» ÀÌ¿ëÇÕ´Ï´Ù. ¿¹¸¦ µé¾î¼ ÇöÀç ½ÇÇàµÇ¾îÀÖ´Â excelÀÇ ÇöÀç ½¬Æ®¿¡ Á¢±ÙÇϱâ À§Çؼ´Â excel.activesheet¶ó°í Çϸé
µÇ°ÙÁÒ? ¶ÇÇÑ ÇöÀç ½¬Æ®ÀÇ "A1"¼¿¿¡ Á¢±ÙÇÏ·Á¸é excel.activesheet.range("a1")ÇÏ¸é µÇ°ÚÁö¿ä?
°£´ÜÇÏ°Ô ¸î°¡Áö Å×½ºÆ®¸¦ Çغ¼±î¿ä? ¹é¹®ÀÌ ºÒ¿©ÀÏ·±À̶ó..ÀÏ´Ü Çѹø Çغ¸´Â °ÍÀÌ ¹«¾ùº¸´Ù Áß¿äÇÏ°ÚÁö¿ä? ¾Æ·¡¿Í °°ÀÌ ÄÚµùÀ» ÇÑ´ÙÀ½¿¡ runÀ» ´·¯º¸¸é
¸Þ½ÃÁö¤Ó ¹Ú½º¿¡ sheet1À̳ª sheet2°°ÀÌ ÇöÀç ¼±ÅÃµÈ ½¬Æ®À̸§À» º¸¿©ÁÙ °ÍÀÔ´Ï´Ù.
Public excel As Object
Sub test()
Dim excel As Object
Set excel = GetObject(, "Excel.Application")
MsgBox excel.ActiveSheet.Name
End Sub
¶ÇÇÑ ¾Æ·¡¿Í °°ÀÌ A1¿¡´Ù 123À» ÀÔ·ÂÇÑ ´ÙÀ½¿¡ ¾Æ·¡¿Í °°ÀÌ ÄÚµùÇؼ runÀ» Çغ¸¸é 123À϶ó°í º¸¿©ÁÝ´Ï´Ù.
Public excel As Object
Sub test()
Dim excel As Object
Set excel = GetObject(, "Excel.Application")
MsgBox excel.ActiveSheet.range("a1")
End Sub
ÀÌ·¸°Ô VBA¸¦ Áö¿øÇÏ´Â ¾î¶² ÇÁ·Î±×·¥µµ getobject·Î ÇÁ·Î±×·¥Àº ¼±ÅÃÇÏ¿© ±× ¾Æ·¡¿¡ ÀÖ´Â ¸ðµç °³Ã¼ ¸ðµç ¼Ó¼º¿¡ Á¢±ÙÀÌ °¡´ÉÇÑ °ÍÀÔ´Ï´Ù. »ç½Ç À§ÀÇ
ÄÚµåÁß¿¡¼ Set excel = GetObject(, "Excel.Application")¶ó´Â ÁÙ ¾øÀÌ ¾Æ·¡¿Í °°ÀÌ ÄÚµùÇصµ °á°ú´Â °°½À´Ï´Ù. ÇÏÁö¸¸ ¿Ö excelÀ̶õ objectÇü º¯¼ö¿¡
excel.applicationÀ» ÁöÁ¤Çؼ »ç¿ëÇÏ¿´À»±î¿ä? ±×°ÍÀº VBA°¡ ¿©·¯ÇÁ·Î±×·¥µéÀ» ¿Ô´Ù °¬´ÙÇÏ¸é¼ ½ÇÇàÀÌ µÇ±â ¶§¹®ÀÔ´Ï´Ù. ¿¹¸¦ µé¾î ijµå¿Í ¿¬°áµÈ
EXcel VBA¶ó¸é Excel¿¡¼ ÀÛ¾÷ÇÏ´Ù CAD·Î °°´Ù°¡ ´Ù½Ã excel·Î µ¹¾Æ¿Í¾ß Çϴµ¥ ÀÌ·± ÀÛ¾÷µéÀ» Çϱâ À§Çؼ´Â ExcelÀÌ¶ó³ª °³Ã¼¸¦ ÁöÁ¤Çصξî¾ß Çϱ⠶§¹®ÀÔ´Ï´Ù.
ÇöÀç excel¿¡¼ÀÇ ¸ðµâÀ̱⠶§¹®¿¡ excelÀ» ÁöÁ¤ÇÏÁö ¾Ê¾Æµµ ´ç¿¬È÷ ÇöÀç ÇÁ·Î±×·¥ÀÌ excelÀ̶õ °ÍÀ» ¾Ë°í Àֱ⠶§¹®¿¡ activesheet¸¸À¸·Îµµ Á¢±ÙÀÌ
°¡´ÉÇÑ °ÍÀÔ´Ï´Ù.
Sub test()
MsgBox ActiveSheet.Name
End Sub
´ÙÀ½ÁÙ·Î ³Ñ¾î°¡º¸µµ·Ï ÇÒ±î¿ä? ´ÙÀ½ÁÙÀº Worksheets("Attributes").Activate¶ó°í µÇ¾îÀÖ½À´Ï´Ù. ´ëÀÔÇÏ´Â µîÈ£(=)µµ ¾ø´Â °Éº¸´Ï activate¶õ ³à¼®ÀÌ
¸Þ¼µå¶ó´Â °ÍÀ» ¾Ë ¼ö ÀÖ½À´Ï´Ù. worksheets("attributes")´Â °³Ã¼ÀÌ°ÚÁÒ? ExcelÀÇ sheetµé(worksheets)Áß¿¡¼ "attributes"¶ó´Â À̸§À» °¡Áø ³à¼®À»
ÁöĪÇÏ´Â °ÍÀÔ´Ï´Ù. ±×³à¼®À» activate¶ó´Â ¶æÀε¥..activate´Â Àú¹ø.. °ÀÇ¿¡¼ ¼³¸íµå·ÈµíÀÌ È°¼ºÈ ½ÃÅ°¶ó´Â ¶æÀÔ´Ï´Ù. Áï ¾ÕÀ¸·Î attributes¶ó´Â
sheet¿¡¼ÀÛ¾÷À» ÇÒÅ×´Ï È°¼ºÈ¸¦ ½ÃÄѶó..¶Ç´Â ¼±ÅÃÇضó.. ¶ó´Â ¶æÀÌ°ÚÁö¿ä?
¿À´Ãµµ ¿å½É¸¸Å Áøµµ¸¦ ³ª°¡Áö ¸øÇß½À´Ï´Ù. ¾ÕºÎºÐÀÌ »ç½Ç»ó VBAÀÇ Áß¿äÇÑ ºÎºÐÀ̱⠶§¹®¿¡ À̺κÐÀº Áøµµ¸¦ õõÈ÷ ³ª°¡·Á°í ÇÕ´Ï´Ù. »ç½Ç Excel°ú
ijµå¸¦ ¿¬°áÇÏ´Â Áß¿äÇÑ ÄÚµåµéÀ̱⠶§¹®ÀÔ´Ï´Ù. ÀÏ´Ü ¿¬°áÇÏ´Â ºÎºÐ¸¸ Áö³ª¸é ±× ´ÙÀ½Àº ÀÏ¹Ý VB ÇÁ·Î±×·¥ °°±â ¶§¹®ÀÔ´Ï´Ù.
±×·³ ÁÁÀº ³¯ µÇ½Ã°í ´ÙÀ½¿¡ ºË°Ú½À´Ï´Ù.
[ÀÌÀü °ÁÂ]
[´ÙÀ½ °ÁÂ]
|