excel打开并操作CAD(excel-vba实现)_另附:CAD打开excel

        excelvba可实现调用cad应用程序,并通过excel内置api弹窗实现打开、另存CAD

文件(cad-vba弹窗功能较为麻烦),代码如下(在excel_vba环境下操作):

Sub excel_vba打开另存并操作CAD文件()
Dim cad As Object
Set cad = CreateObject("autocad.Application")
cad.Visible = True
cad.documents.Add

On Error Resume Next
With excel.Application.FileDialog(msoFileDialogOpen)
    .Title = "请选择你要的文件"
    .AllowMultiSelect = True
    .InitialFileName = "C:\Users\Administrator\Desktop\"
    .Filters.Clear
    .Filters.Add "excel files", "*.xls,*.xlsx,*.dwg"
    If .Show = True Then
    Set gof = .SelectedItems
    End If
End With
cad.Application.documents.Open (gof.Item(1))
MsgBox "另存为"
With excel.Application.FileDialog(msoFileDialogSaveAs)
    .Title = "另存为"
    .AllowMultiSelect = True
    .InitialFileName = "C:\Users\Administrator\Desktop\"
    If .Show = True Then
    Set gof1 = .SelectedItems
    End If
End With
cad.Application.activedocument.SaveAs (gof1.Item(1))

End Sub

另附excel文件可参考。 

以下代码为在CADVBA环境下打开excel并操作的代码:

Sub CAD打开excel_cadvba实现()
Dim excel As Object
Dim excelSheet As Object
    ' Start Excel
    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 load Excel.", vbExclamation
            End
        End If
    End If
    
   
    
    excel.Visible = True
    excel.Workbooks.Add
    excel.Sheets("Sheet1").Select
    Set excelSheet = excel.ActiveWorkbook.Sheets("Sheet1")
End Sub

 

你可能感兴趣的:(Excel,VBA,excel)