Option Explicit
'-------------------打开文件-------------
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOW = 5
'-------------------打开文件-------------
'-------------------切换线程-------------
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" _
(ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
'Private Const SW_SHOW = 5 '与打开文件里面的重复了,这里取消
Private Const SW_RESTORE = 9
'-------------------切换线程-------------
Private Sub Form_Load()
Dim App As Object
Dim AppDoc As Object
Dim xlApp As Object 'Excel.Application
Dim xlSheet As Object ' Worksheet
Dim XPath As String
Dim VBAProjectPath As String
Dim i As Integer
On Error Resume Next
XPath = VB.App.Path
DoEvents
Set App = GetObject(, "autocad.Application")
If Err.Number <> 0 Then
Err.Clear
ShellExecute Me.hwnd, "open", XPath & "\TT_AutoCAD启动页.dwg", vbNullString, vbNullString, SW_SHOW
If Err Then
MsgBox "没有发现正在运行中的AutoCAD,请先启动AutoCAD软件!", vbInformation, "提示"
Err.Clear
End
End If
DengDai:
Set App = GetObject(, "autocad.Application")
'MsgBox "没有发现正在运行中的AutoCAD,请先启动AutoCAD软件!", vbInformation, "提示"
' End
End If
If App.Documents.Count = 0 Then
App.Documents.Add
End If
Set AppDoc = App.ActiveDocument
If Err Then
Err.Clear
GoTo DengDai
End If
For i = 1 To Len(XPath)
If Mid(XPath, i, 1) = "\" Then
VBAProjectPath = VBAProjectPath & "/"
Else
VBAProjectPath = VBAProjectPath & Mid(XPath, i, 1)
End If
Next i
Set xlApp = GetObject(, "Excel.Application")
Set xlSheet = xlApp.Workbooks("TT_AutoCAD文字自动替代").ActiveSheet
If Err Then
Err.Clear
ShellExecute Me.hwnd, "open", XPath & "\TT_AutoCAD文字自动替代.xls", vbNullString, vbNullString, SW_SHOW
End If
ForceForegroundWindow Me.hwnd
DoEvents
ForceForegroundWindow App.hwnd
AppDoc.SendCommand "-vbarun " & VBAProjectPath & "/批量打开文件文字替代.dvb!ThisDrawing.PiLiangTiHuan "
ForceForegroundWindow App.hwnd
End
End Sub
Public Function ForceForegroundWindow(ByVal hwnd As Long) As Boolean
Dim ThreadID1 As Long ' 线程ID
Dim ThreadID2 As Long ' 线程ID
Dim nRet As Long
' 如果指定的窗体已经在前台,不做任何操作
If hwnd = GetForegroundWindow() Then
ForceForegroundWindow = True
Else
' 首先获得指定窗体相关的线程和当前前台窗口所在的线程
ThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
ThreadID2 = GetWindowThreadProcessId(hwnd, ByVal 0&)
' 通过共享输入状态,两个线程分享当前窗口
If ThreadID1 <> ThreadID2 Then
Call AttachThreadInput(ThreadID1, ThreadID2, True)
nRet = SetForegroundWindow(hwnd)
Call AttachThreadInput(ThreadID1, ThreadID2, False)
Else
nRet = SetForegroundWindow(hwnd)
End If
' 恢复和重画
If IsIconic(hwnd) Then
Call ShowWindow(hwnd, SW_RESTORE)
Else
Call ShowWindow(hwnd, SW_SHOW)
End If
' 精确地返回函数执行结果
ForceForegroundWindow = CBool(nRet)
End If
End Function