-
I am opening a Word Document from my form in VB.
I want user to edit the document and he can use any of
options provided except the Close button on the title bar of the word document. I simply want to hide the title bar
so that user can't close the word document.
Please help.
-
Is the word document in Word or is it on a form that is in your app? What I mean is, is the document being opened using Word or is it being opened on a form that is part of your code?
-
'PASTE THIS INTO YOUR FORM WITH A COMMAND BUTTON ON IT
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Const MF_BYPOSITION = &H400&
Const MF_REMOVE = &H1000&
Private Sub Command1_Click()
DisableClose
Command1.Caption = "&Done"
End Sub
Private Sub DisableClose()
Dim hSysMenu As Long, nCnt As Long
Dim x As Variant
Dim lnghWnd As Long
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
For Each x In colWord
lnghWnd = CLng(x)
hSysMenu = GetSystemMenu(lnghWnd, False)
If hSysMenu Then
' Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
' Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the seperator
DrawMenuBar lnghWnd
' Force caption bar's refresh. Disabling X button
End If
End If
Next x
End Sub
'PASTE THIS INTO A MODULE
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Global Running As Boolean
Global colWord As New Collection
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
If InStr(1, sSave, "Microsoft Word") Then
colWord.Add hwnd
End If
EnumWindowsProc = True
End Function
BTW: it's not simple!
THIS CODE WILL DISABLE THE EXIT OPTION AND CLOSE BUTTON ON ALL INSTANCES OF MICROSOFT WORD, AND I ACCEPT NO LIABILITY WHATSOEVER FOR ANY MISUSE THEREOF.
-
Thanks mlewis.
The word document is in Word.That is, the document is opened using Word. I first created an instance of word in
VB and then open the document using following command :
appword.Documents.Open FileName
-
humm... If you can find the hWnd of the Word document, you can use the SetWindowLong API as follows:
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const WS_CLIPCHILDREN = &H2000000
Private Const WS_CLIPSIBLINGS = &H4000000
Private Const WS_OVERLAPPED = &H0&
Private Const WS_VISIBLE = &H10000000
' some where in code
SetWindowLong WordDocHWnd, GWL_STYLE, WS_CLIPCHILDREN Or WS_CLIPSIBLINGS Or WS_OVERLAPPED Or WS_VISIBLE
End Sub
this will hide the title bar. but you need to find the hwnd of the document window first, I don't know how to do that off the top of my head.
-
The only way I could think of to find the hWnd was to recurse all the windows, and detect all those with an instr of "Microsoft Word" in the window title, I'm not sure how to narrow it down any more than that, unless you are absolutely sure of the document name, then you could do a window name match on the title, using EnumWindows.
-
yeah, that would work. its tedious, but it would work.
-
Couldn't you use the handle? or doesn't that work for all windows
-
it'll work if the handle is an hwnd.
I don't know for sure if Word passes hwnds to its child windows.