AW: Nachtrag
20.03.2018 14:08:23
Peter(silie)
Hallo,
das Application Objekt von Word kennt kein hWnd,
aber ein einzelnes Fenster schon.
Somit könntest du folgenden Code ausprobieren:
Option Explicit
Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Long) _
As Long
Public Function CloseWordWindow(ByRef wApp As Word.Application, _
ByVal hwndName As String) As Boolean
Dim hwnd As Long
Dim wWin As Long
'falls es offene fenster gibt in der applikation
If wApp.Windows.count > 0 Then
'durchlaufe alle
For wWin = 1 To wApp.Windows.count
'falls der fenster titel richtig ist dann
If wApp.Windows(wWin).Caption = hwndName Then
'hole dir den pointer zu diesem fenster
hwnd = wApp.Windows(wWin).hwnd
'schließe fenster
SendMessage hwnd, &H10, 0&, 0&
CloseWordWindow = True
Exit Function
End If
Next wWin
End If
End Function
Public Sub Test()
Dim wApp As Word.Application
Dim wFile As Word.Document
Dim pName As String
'Der pfad + dateiname einer word datei
pName = "pfad zu einer word datei"
'erstelle neues word objekt
Set wApp = New Word.Application
'öffne die datei
Set wFile = wApp.Documents.Open(pName)
'Do Stuff
'rufe Function auf, übergebe das Word Objekt und den namen des Dateifensters
If CloseWordWindow(wApp, wFile.Windows(1).Caption) Then
MsgBox "Word Window closed"
End If
End Sub