VBA程序如下:
FORM控件如下:
TextBox--tbPath
CommandButton--cbPurge
ListBox--ListBox1
Label--LabTips
Private Sub cbPurge_Click()
Dim Fso As New FileSystemObject
Dim RootFolder As Folder
If Fso.FolderExists(Me.tbPath.Text) = False Then
Me.LabTips.Caption = "目录不存在。"
Exit Sub
End If
Set RootFolder = Fso.GetFolder(Me.tbPath.Text)
GetFolder RootFolder
Me.LabTips.Caption = "共计处理 " + Me.ListBox1.ListCount + " 个文件"
End Sub
Private Sub PurgeDrawing(oFolder As Folder)
On Error Resume Next
Dim Fso As New FileSystemObject
Dim oFile As File
Dim Ext As String
For Each oFile In oFolder.Files
Ext = Fso.GetExtensionName(oFile.Path)
If LCase$(Ext) = "dwg" Then
Me.LabTips.Caption = "正在处理" + oFile.Path
Application.Documents.Open (oFile.Path)
Dim i As Integer
For i = 0 To 10
Application.ActiveDocument.SendCommand ("_purge" &
vbCr & "all" & vbCr & "*" & vbCr & "n" & vbCr)
Next i
Application.ActiveDocument.Save
Application.ActiveDocument.Close
Me.ListBox1.AddItem oFile.Path
Me.ListBox1.ListIndex = Me.ListBox1.ListCount - 1
Me.ListBox1.Selected (Me.ListBox1.ListCount - 1)
Me.Repaint
End If
Next oFile
End Sub
Private Sub GetFolder(oFolder As Folder)
On Error Resume Next
Dim Fso As New FileSystemObject
Dim oDir As Folder
PurgeDrawing oFolder
If oFolder.SubFolders.Count > 0 Then
For Each oDir In oFolder.SubFolders
GetFolder oDir
Next oDir
End If
End Sub