用VB调用word,我声明了一个Word.Application,用Word.Document打开一个
word文档,请问怎么判断当前是否打开了一个文档,以便再操作时不会被锁住
word.怎么退出打开的word而不需要提示。(word.quit),能不经过word而通过
VB直接打印这个word文档吗?

解决方案 »

  1.   

    1 很多途径
      * 查找是否存在~$文件名.DOC文件。
      * API:findwindow
      * 打开一个文件后,做一个记录,例如.log文件中,数据库中。关闭后删除记录。
     
    3 不能。除非你编一段处理word文档,并输出数据到打印机的代码。
      

  2.   

    我调用的是同一个word模板,有可能调用一次后,还要调用。需要判断是否调用,
    能说得具体电吗?* 打开一个文件后,做一个记录,例如.log文件中,数据库中。关闭后删除记录。是存一个临时文件,再删除吗?谢谢!
      

  3.   

    2 quit方法可以带参数:
    * wdDoNotSaveChanges        0        Close without saving changes
    * wdSaveChanges            -1        Save changes before closing
    * wdPromptToSaveChanges    -2        Ask the user whether or not to save changes before closing; usually not a good choice in an Automation environment.
     
      

  4.   

    怎么判断当前是否打开了这文档?就像一个ADO的连接可通过判断conn.state进行,(* 查找是否存在~$文件名.DOC文件。  * API:findwindow),怎么实现,能具体点吗?
      

  5.   

    最方便就是用查找是否存在~$文件名.DOC文件
    if dir(app.path + "~$文件名.DOC")<>"" then
    msgbox "存在"
    end if
      

  6.   

    试一试Dim wrdApp As Word.ApplicationSub OpenDOC(DocName as String)
        Dim Doc As Object
        Dim docPath As String
        For Each Doc In wrdApp.Application.Documents
            docPath = Doc.Path
            If Right$(docPath, 1) <> "\" Then docPath = docPath & "\"
            If docPath & Doc.Name = DocName then
               Msgbox "文件已经打开"
               Exit For
            EndIf
        Next
    End Sub
      

  7.   

    vertynew的方法试了不行。
    我要的是在我打开这个文件之前,
    先判断他是否在内存中已打开,
    而不是判断这个doc文件存在不存在,
    因为他是个模板,肯定存在,
    我要的是在打开这个模板时,
    如果模版已打开就先关闭,
    再重新打开一个,不至于出现
    “你要打开的文件正在被另一用户编辑,以只读方式
    打开”提示窗口。
      

  8.   

    我的方法是判断文件是否打开(既是否在内存中已打开)
    你可以这样做Sub OpenDOC(DocName as String)
        Dim wrdApp As Object
        On Error Goto RunWord
        Set wrdApp = GetObject(,"Word.Application")
        Dim Doc As Object
        Dim docPath As String
        For Each Doc In wrdApp.Application.Documents
            docPath = Doc.Path
            If Right$(docPath, 1) <> "\" Then docPath = docPath & "\"
            If docPath & Doc.Name = DocName then
               Doc.Close False              '如果要保存,则用True
               Exit For
            EndIf
        Next
        Goto OpenDoc
    RunWord:
        Set wrdApp = CreateObject("Word.Application")
    OpenDoc:
        wrdApp.Documents.Open(DocName)
    End Sub
      

  9.   

    在Set wrdApp = CreateObject("Word.Application")后加一句
    wrdApp.Visible = True
      

  10.   

    还是不行,执行到For Each Doc In wrdApp.Application.Documents时,就没有进入到循环,
    而且最后我捕获的错误提示是:“错误:5174,找不到此文件“。
      

  11.   

    执行到For Each Doc In wrdApp.Application.Documents时,就没有进入到循环可能是你已经启动了多个Word,如果直接使用Word是不会出现这种情况的,除非是你的程序调用。把这些Word都结束后,重新再试试。   错误:5174,找不到此文件是因为文件参数 DocName 不存在,即并没有你要打开的文件。
       为避免这个错误,最好在调用 OpenDOC 之前选判断文件是否存在
      

  12.   

    现在执行倒是没有问题了,可是如果你的程序在我创建word及word.document之前执行,则他
    先创建了对象,并打开了文件。我再创建时,就会出现,另一用户在编辑的提示。如果在我后执行就不能判段文件是否已打开。
      

  13.   

    你可以事先引用或起动Word在试一试这样Private wrdApp As ObjectPrivate Sub Form_Load()
        On Error Goto RunWord
        Set wrdApp = GetObject(,"Word.Application")
    RunWord:
        On Error Resume Next
        Set wrdApp = CreateObject("Word.Application")
    End SubSub OpenDOC(DocName as String)
        Dim Doc As Object
        Dim docPath As String
        If wrdApp is Nothing then Exit Sub
        For Each Doc In wrdApp.Application.Documents
            docPath = Doc.Path
            If Right$(docPath, 1) <> "\" Then docPath = docPath & "\"
            If docPath & Doc.Name = DocName then
               Doc.Close False              '如果要保存,则用True
               Exit For
            EndIf
        Next
        wrdApp.Documents.Open(DocName)
    End Sub