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

发布时间:2023年12月19日

????????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

?

文章来源:https://blog.csdn.net/yongshiqq/article/details/135087389
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。