用VB调用Word的时候怎样屏蔽掉Word窗体上的 [X] (不想让用户自己关闭word)!?

解决方案 »

  1.   

    用这样一段代码来删掉word的系统菜单里的关闭条,以此来防止关闭。Private Sub Command1_Click()
        Dim hWordHwnd As Long
        Dim hWordMenu As Long
        Dim lID As Long
        
        '得到word句柄,如果在打开word时能得到最好,如果不能得到,就用findwindow来找
        hWordHwnd = FindWindow(vbNullString, "文档 1 - Microsoft Word")
        If hWordHwnd = 0 Then
            MsgBox "没找到"
            Exit Sub
        End If
        
        '得到系统菜单句柄
        hWordMenu = GetSystemMenu(hWordHwnd, False)
        If hWordMenu = 0 Then
            MsgBox "没菜单"
            Exit Sub
        End If
        
        '得到第六个菜单,也就是“关闭”的id
        nID = GetMenuItemID(hWordMenu, 6)
        
        '删除此菜单项
        If (0 = RemoveMenu(hWordMenu, nID, MF_BYCOMMAND)) Then
            MsgBox "设置失败"
            Exit Sub
        End If
        
        '得到第五个菜单,也就是那条横线的id
        nID = GetMenuItemID(hWordMenu, 5)
        
        '再删掉那条横线的菜单项
        If (0 = RemoveMenu(hWordMenu, nID, MF_BYCOMMAND)) Then
            MsgBox "设置失败"
            Exit Sub
        End If
        
        MsgBox "成功"
    End Sub
      

  2.   

    Private WithEvents wd As Word.ApplicationPrivate Sub wd_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    If MsgBox("真的想关闭" & Doc.Name & "吗?", vbYesNo) = vbNo Then Cancel = True
    End Sub
      

  3.   

    哈,好久不用vb了,有的东西都忘了,其实这段代码这样写就可以。
    用惯了c ,忘了api中用到的常数是需要自己定义的。Private Sub Command1_Click()
        Dim hWordHwnd As Long
        Dim hWordMenu As Long
        Dim lID As Long
        
        '得到word句柄,如果在打开word时能得到最好,如果不能得到,就用findwindow来找
        hWordHwnd = FindWindow(vbNullString, "文档 1 - Microsoft Word")
        If hWordHwnd = 0 Then
            MsgBox "没找到"
            Exit Sub
        End If
        
        '得到系统菜单句柄
        hWordMenu = GetSystemMenu(hWordHwnd, False)
        If hWordMenu = 0 Then
            MsgBox "没菜单"
            Exit Sub
        End If
        
        '删除第六个菜单,也就是“关闭”
        If (0 = RemoveMenu(hWordMenu, 6, MF_BYPOSITION)) Then
            MsgBox "设置失败"
            Exit Sub
        End If
        
        '删除第五个菜单,也就是那条横线
        If (0 = RemoveMenu(hWordMenu, 5, MF_BYPOSITION)) Then
            MsgBox "设置失败"
            Exit Sub
        End If
        
        MsgBox "成功"
    End Sub
      

  4.   

    to: Maconel(Maconel)多谢你的回复,GetSystemMenu(hWordHwnd, False)  RemoveMenu(hWordMenu, 6, MF_BYPOSITION)着两个函数是怎摸定义的呀?MF_BYPOSITION 的常量是多少呀?
      

  5.   

    Private WithEvents wd As Word.ApplicationPrivate Sub wd_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    If MsgBox("真的想关闭" & Doc.Name & "吗?", vbYesNo) = vbNo Then Cancel = True
    End Sub
      

  6.   

    Private WithEvents wd As Word.Application 不能设为 public 呀!
      

  7.   

    to zzzwwwllll(zwl) :Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As LongPrivate Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As LongPrivate Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As LongConst MF_BYPOSITION = &H400