Hallo Leute,
ich habe folgendes Problem:
Mit einen Button möchte ich verschiedene Zellen kopieren und danach ein Programm öffnen.
Somit ergibt sich dieser Code
Code:
Sub Kopieren()
Range("$A$9:$F$17").Copy
Shell "explorer.exe C:\MeinProgramm", 1
End Sub
Funktioniert absolut super! Die Zellen sind kopiert und das Programm öffnet sich.
Problem ist nur, dass dieses Programm auf dem Rechner immer läuft, das Fenster jedoch minimiert ist.
Gibt es eine Möglichkeit mit dem Button einfach nur das Fenster des Programms zu öffnen bzw zu wechseln ähnlich
wie mit ALT + Tab, damit das Programm nicht immer zusätzlich gestartet wird?
Wäre super wenn ihr mir helfen könnt
Gruß
Hast Du ein Handle auf das geöffnete Programm? Oder kennst Du die "Caption"?
Dann ginge z.B. folgendes
Code:
Sub TestIt()
Dim myWnd As New cWnd
Dim pid As Long
Dim xl As Excel.Application
pid = Shell("notepad")
myWnd.SetHWnd , "Unbenannt - Editor"
myWnd.Activate
Application.Wait Now + TimeSerial(0, 0, 3)
Shell "Taskkill /F /PID " & pid
Set xl = New Excel.Application
xl.Visible = True
Application.Wait Now + TimeSerial(0, 0, 3)
xl.WindowState = xlMinimized
Application.Wait Now + TimeSerial(0, 0, 3)
myWnd.SetHWnd xl.hwnd
myWnd.Activate
Application.Wait Now + TimeSerial(0, 0, 3)
xl.Quit
End Sub
Die Klasse cWnd sieht wie folgt aus
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowPlacement Lib "user32" _
(ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function SetWindowPlacement Lib "user32" _
(ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function BringWindowToTop Lib "user32" _
(ByVal hwnd As Long) As Long
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type
Dim mHWnd As Long
Private Sub Class_Initialize()
mHWnd = 0
End Sub
Property Get Initialized() As Boolean
Initialized = (mHWnd <> 0)
End Property
Function SetHWnd(Optional xHnd As Long, Optional xCaption As String) As Boolean
If xHnd = 0 And xCaption = "" Then
SetHWnd = False
Exit Function
End If
If xHnd <> 0 And xCaption <> "" Then
SetHWnd = False
Exit Function
End If
If xHnd = 0 And xCaption <> "" Then
mHWnd = FindWindow(vbNullString, xCaption)
If mHWnd <> 0 Then
SetHWnd = True
End If
Exit Function
End If
If xHnd <> 0 And xCaption = "" Then
mHWnd = xHnd
SetHWnd = True
End If
End Function
Function Activate() As Boolean
If Initialized Then
Activate = ActivateWindow(mHWnd)
End If
End Function
Private Function ActivateWindow(xhWnd&) As Boolean
Dim Result&, WndPlcmt As WINDOWPLACEMENT
With WndPlcmt
.Length = Len(WndPlcmt)
Result = GetWindowPlacement(xhWnd, WndPlcmt)
If Result Then
If .showCmd = SW_SHOWMINIMIZED Then
.flags = 0
.showCmd = SW_SHOWNORMAL
Result = SetWindowPlacement(xhWnd, WndPlcmt)
Else
Call SetForegroundWindow(xhWnd)
Result = BringWindowToTop(xhWnd)
End If
If Result Then ActivateWindow = True
End If
End With
End Function
Private Function DeActivateWindow(xhWnd&) As Boolean
Dim Result&, WndPlcmt As WINDOWPLACEMENT
With WndPlcmt
.Length = Len(WndPlcmt)
Result = GetWindowPlacement(xhWnd, WndPlcmt)
If Result Then
.flags = 0
.showCmd = SW_SHOWMINIMIZED
Result = SetWindowPlacement(xhWnd, WndPlcmt)
If Result Then DeActivateWindow = True
End If
End With
End Function
Vielen Dank für die schnelle Antwort!
Jedoch verstehe ich nicht ganz was ich da jetzt wie einfügen soll.
Das Programm wird vor jedem Prozess gestartet.
Das Fenster heißt: Maschineninfo
Die Datei ist: Maschine1.Ink
Typ: Verknüpfung
Ordnerpfad: C:\Benutzer\Name\Desktop
Wie muss ich das in das Macro einfügen?
Ach so, Du bist völlig anhnungslos
Du hast ja nicht mal ansatzweise meine Fragen verstanden.
Dann lassen wir das mal lieber, Klassen und dann mit API ... das wird
Voodo-Chicken Coding für Dich.
Zur Lektüre sei
dies empfohlen
(10.02.2019, 20:27)zowie3 schrieb: [ -> ]Das Fenster heißt: Maschineninfo
hier eine einfache Variante:
Sub Kopieren()
Range("$A$9:$F$17").Copy
'Shell "explorer.exe C:\MeinProgramm", 1
AppActivate "Maschineninfo", True 'aktiviert das Fenster
SendKeys "% x" 'maximiert das aktive Fenster
End Sub
Gruß Uwe