用VB调用word,我声明了一个Word.Application,用Word.Document打开一个
word文档,请问怎么判断当前是否打开了一个文档,以便再操作时不会被锁住
word.怎么退出打开的word而不需要提示。(word.quit),能不经过word而通过
VB直接打印这个word文档吗?
word文档,请问怎么判断当前是否打开了一个文档,以便再操作时不会被锁住
word.怎么退出打开的word而不需要提示。(word.quit),能不经过word而通过
VB直接打印这个word文档吗?
* 查找是否存在~$文件名.DOC文件。
* API:findwindow
* 打开一个文件后,做一个记录,例如.log文件中,数据库中。关闭后删除记录。
3 不能。除非你编一段处理word文档,并输出数据到打印机的代码。
能说得具体电吗?* 打开一个文件后,做一个记录,例如.log文件中,数据库中。关闭后删除记录。是存一个临时文件,再删除吗?谢谢!
* 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.
if dir(app.path + "~$文件名.DOC")<>"" then
msgbox "存在"
end if
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
我要的是在我打开这个文件之前,
先判断他是否在内存中已打开,
而不是判断这个doc文件存在不存在,
因为他是个模板,肯定存在,
我要的是在打开这个模板时,
如果模版已打开就先关闭,
再重新打开一个,不至于出现
“你要打开的文件正在被另一用户编辑,以只读方式
打开”提示窗口。
你可以这样做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
wrdApp.Visible = True
而且最后我捕获的错误提示是:“错误:5174,找不到此文件“。
为避免这个错误,最好在调用 OpenDOC 之前选判断文件是否存在
先创建了对象,并打开了文件。我再创建时,就会出现,另一用户在编辑的提示。如果在我后执行就不能判段文件是否已打开。
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