AW: Userform Label Bookmarks
05.01.2022 01:52:26
Everlost03
Das meiste stand schon, halt nur auf Exeltabellen bzw. Zellen gemünzt.
Konnte es jetzt erfolgreich umbauen und es ist umsonst! ;-)
Option Explicit
Private Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByRef lpdwProcessId As Long) As Long
Private Declare PtrSafe Function AllowSetForegroundWindow Lib "user32.dll" ( _
ByVal dwProcessId As Long) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" ( _
ByVal hWnd As Long) As Long
Private Const GC_CLASSNAMEWORD = "OpusApp"
Private Const SW_MAXIMIZE = 3
Private Sub CommandButton1_Click()
Dim appWord As Object
Dim test As Object
Set appWord = CreateObject("Word.Application")
appWord.WindowState = 1
appWord.Visible = True
Set test = appWord.Documents.Add("C:\Users\Documents\test.docx")
test.ActiveWindow.Activate
test.Bookmarks("test1").Range.Text = "Nachname.Caption"
test.Bookmarks("test2").Range.Text = "Nachname.Caption"
test.Bookmarks("test3").Range.Text = "Nachname.Caption"
test.Bookmarks("test4").Range.Text = "Nachname.Caption"
Dim lngHwnd As Long, lngProcessID As Long
lngHwnd = FindWindow(GC_CLASSNAMEWORD, vbNullString)
If lngHwnd = 0 Then Err.Raise Number:=vbObjectError, Description:= _
"Das Fensterhandle von Word kann nicht ermittelt werden."
lngProcessID = GetWindowThreadProcessId(lngHwnd, ByVal 0&)
Call AllowSetForegroundWindow(lngProcessID)
Call SetForegroundWindow(lngHwnd)
Call ShowWindow(lngHwnd, SW_MAXIMIZE)
Set test = Nothing
Set appWord = Nothing
End Sub