Results 1 to 9 of 9

Thread: Hide the titlebar of a Word Document from VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Location
    New Delhi, India
    Posts
    4

    Question

    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.
    Deepti Bhatnagar

  2. #2
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    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?
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  3. #3
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    '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.
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Location
    New Delhi, India
    Posts
    4

    Lightbulb

    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
    Deepti Bhatnagar

  5. #5
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    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.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  6. #6
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    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.
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  7. #7
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    yeah, that would work. its tedious, but it would work.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  8. #8
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Couldn't you use the handle? or doesn't that work for all windows
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  9. #9
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    it'll work if the handle is an hwnd.

    I don't know for sure if Word passes hwnds to its child windows.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width