AutoCAD 基本操作

‘绑定 AutoCAD
Dim oAutoCAD As Object ‘AutoCAD Application 本身
Call BindAutoCAD(True)
Dim oDraw As Object ‘AutoCAD 多文档之一

‘打开并绑定文件 D:/Test.dwg
Set oDraw = oAutoCAD.Application.Documents.Open(“D:/Test.dwg”)

‘绑定已经打开文件 Demo.dwg
Set oDraw = oAutoCAD.Documents.Item(“Demo.dwg”)

‘新建文件并绑定
Set oDraw = oAutoCAD.Documents.Add

‘绑定已经打开的当前文件
Set oDraw = oAutoCAD.ActiveDocument

‘另存文件并指定格式
oDraw.SaveAs “D:/TestBack.dwg”, 12 ’12 为 AutoCAD 2000 DWG (*.dwg) 格式

‘退出 AutoCAD 并释放资源
oAutoCAD.Quit
Set oAutoCAD = Nothing
Set oDraw = Nothing

绑定 WPS Word 对象

模块 BindWord 后期绑定 WPS Word 对象,调用方法如下:

Dim sProg As String
sProg = “kwps” ‘WPS 2013 之前版本为 wps
Dim oWord As Object
Call BindWord(True)

Public Sub BindWord(ByVal bVisible As Boolean)
If Not oWord Is Nothing Then Exit Sub
On Error Resume Next
Set oWord = GetObject(, sProg & “.Application”)
If Err.Number <> 0 Then ‘没有打开
Err.Clear
Set oWord = CreateObject(sProg & “.application”)
If Err.Number <> 0 Then ‘没有正确安装
Err.Clear
Exit Sub
End If
oWord.Visible = bVisible
End If
On Error GoTo 0
End Sub

绑定 WPS Excel 对象

模块 BindExcel 后期绑定 WPS Excel 对象,调用方法如下:

Dim sProg As String
sProg = “ket” ‘WPS 2013 之前版本为 et
Dim oExcel As Object
Call BindExcel(True)

Public Sub BindExcel(ByVal bVisible As Boolean)
If Not oExcel Is Nothing Then Exit Sub
On Error Resume Next
Set oExcel = GetObject(, sProg & “.Application”)
If Err.Number <> 0 Then ‘没有打开
Err.Clear
Set oExcel = CreateObject(sProg & “.application”)
If Err.Number <> 0 Then ‘没有正确安装
Err.Clear
Exit Sub
End If
oExcel.Visible = bVisible
End If
On Error GoTo 0
End Sub

绑定 Microsoft Word 对象

模块 BindWord 后期绑定 Microsoft Word 对象,调用方法如下:

Dim sProg As String
sProg = “Word”
Dim oWord As Object
Call BindWord(True)

Public Sub BindWord(ByVal bVisible As Boolean)
If Not oWord Is Nothing Then Exit Sub
On Error Resume Next
Set oWord = GetObject(, sProg & “.Application”)
If Err.Number <> 0 Then ‘没有打开
Err.Clear
Set oWord = CreateObject(sProg & “.application”)
If Err.Number <> 0 Then ‘没有正确安装
Err.Clear
Exit Sub
End If
oWord.Visible = bVisible
End If
On Error GoTo 0
End Sub

绑定 Microsoft Excel 对象

模块 BindExcel 后期绑定 Microsoft Excel 对象,调用方法如下:

Dim sProg As String
sProg = “Excel”
Dim oExcel As Object
Call BindExcel(True)

Public Sub BindExcel(ByVal bVisible As Boolean)
If Not oExcel Is Nothing Then Exit Sub
On Error Resume Next
Set oExcel = GetObject(, sProg & “.Application”)
If Err.Number <> 0 Then ‘没有打开
Err.Clear
Set oExcel = CreateObject(sProg & “.application”)
If Err.Number <> 0 Then ‘没有正确安装
Err.Clear
Exit Sub
End If
oExcel.Visible = bVisible
End If
On Error GoTo 0
End Sub