weianhui 发表于 2007-3-29 11:43:00

用VB控制word打印后怎么关闭word

<p>我用VB控制word用.printout方法发送打印后就用wdapp.quit结束了word,但打印机中却没有收到,之后我去掉了wdapp.quit这句就可以打印了,但是进程中却有winword.exe一直在运行,请问怎么才能在发送打印后关掉word程序?大虾帮忙阿,谢谢先</p><p>以下是我的程序:</p><p>Set WordApp = GetObject(, "Word.Application")<br/>&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set WordApp = CreateObject("Word.Application")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "无法启动Word,请检查是否正确安装!"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Set WordDoc = WordApp.Documents.Open(List1.List(i))<br/>&nbsp;&nbsp; WordDoc.PrintOut<br/>&nbsp;&nbsp; Printer.EndDoc<br/>&nbsp;&nbsp; If Err Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox Err.Description<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Err.Clear<br/>&nbsp;&nbsp; End If<br/>&nbsp;&nbsp; WordDoc.Close False<br/>&nbsp;&nbsp; List2.AddItem "正在将'" &amp; List1.List(i) &amp; "'发送至" &amp; Printer.DeviceName<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp; WordApp.Quit False&nbsp;&nbsp;&nbsp;&nbsp; '<font color="#f70968" style="BACKGROUND-COLOR: #ffffff;">就是这句,去掉就能打印,但进程中winword.exe就会一直运行,若保留就不能打印</font></p><p>&nbsp;&nbsp; Set WordApp = Nothing</p>

petty 发表于 2007-3-29 18:58:00

wordapp.close

petty 发表于 2007-3-29 18:59:00

wordapp.close

weianhui 发表于 2007-3-31 00:33:00

<p>不行啊,说“未找到数据成员或方法”,close方法worddoc有,wordapp没有啊,怎么办啊,请教大虾们啊,非常急</p>

xiaoye 发表于 2007-4-10 16:09:00

<p>'关闭Word<br/>WordDoc.Close<br/>WordApp.Documents.Close<br/>WordApp.Quit<br/>Set WordDoc = Nothing<br/>Set WordApp = Nothing</p><p>我没有用Word进行打印,只是打开进行修改之后再将其关闭,能结束进程中的<font color="#f70968">winword.exe</font>。你可以试试</p><p></p>

lwq0990 发表于 2007-10-27 01:34:00

我觉得你在 "WordApp.Quit False" 这句之前加一句 " msgbox "正在打印中",,"提示信息"  "这条语句,等打印完了,再点“确定”,就会执行 "WordApp.Quit False" 这条语句了。<br/>

solen 发表于 2009-3-31 11:17:00

<p>谢谢楼上 再补充下</p><p>oWord:Word.Application &nbsp; <br/>&nbsp; oWord.ActiveDocument.BuiltInDocumentProperties(Index).value &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; Index=1 &nbsp; 标题(Title) &nbsp; <br/>&nbsp; Index=2 &nbsp; 主题(Subject) &nbsp; <br/>&nbsp; Index=3 &nbsp; 作者(Author) &nbsp; <br/>&nbsp; Index=4 &nbsp; 关键词(Keywords) &nbsp; <br/>&nbsp; Index=5 &nbsp; 备注(Comments) &nbsp; <br/>&nbsp; Index=6 &nbsp; 模板(Template) &nbsp; <br/>&nbsp; Index=7 &nbsp; 单位(Last &nbsp; author)</p><p>---------------------------------------------------------------------------</p><p>uses &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; ComObj &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; function &nbsp; IsFileInUse(const &nbsp; FileName:String):Boolean; &nbsp; <br/>&nbsp; var &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; FileStrm:TFileStream; &nbsp; <br/>&nbsp; begin &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; Result:=false; &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; try &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileStrm:=TFileStream.Create(FileName,fmOpenReadWrite); &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileStrm.Free; &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; except &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=true; &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; end; &nbsp; <br/>&nbsp; end; &nbsp; <br/>&nbsp; &nbsp; <br/>&nbsp; procedure &nbsp; TForm1.Button1Click(Sender: &nbsp; TObject); &nbsp; <br/>&nbsp; var &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; oWord:OleVariant; &nbsp; <br/>&nbsp; &nbsp; &nbsp; &nbsp; FileName:String; &nbsp; <br/>&nbsp; begin &nbsp; <br/>&nbsp; &nbsp; &nbsp; FileName:='C:\Documents &nbsp; and &nbsp; Settings\Administrator\Desktop\料品库存异动汇总表.doc'; &nbsp; <br/>&nbsp; &nbsp; &nbsp; if &nbsp; not &nbsp; FileExists(FileName) &nbsp; then &nbsp; exit; &nbsp; <br/>&nbsp; &nbsp; &nbsp; if &nbsp; IsFileInUse(FileName) &nbsp; then &nbsp; exit;//检查文件是否被打开,否则不能修改 &nbsp; <br/>&nbsp; &nbsp; &nbsp; oword &nbsp; := &nbsp; CreateOLEObject('Word.Application'); &nbsp; <br/>&nbsp; &nbsp; &nbsp; Sleep(100);//等待Word启动 &nbsp; <br/>&nbsp; &nbsp; &nbsp; oword.documents.open(FileName); &nbsp; <br/>&nbsp; &nbsp; &nbsp; oWord.ActiveDocument.BuiltInDocumentProperties(1):='测试修改Word文档属性'; &nbsp; //标题(Title) &nbsp; <br/>&nbsp; &nbsp; &nbsp; oWord.ActiveDocument.BuiltInDocumentProperties(2):='修改Word相关信息'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //主题(Subject) &nbsp; <br/>&nbsp; &nbsp; &nbsp; oWord.ActiveDocument.BuiltInDocumentProperties(3):='最好玩滴那个'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //作者(Author) &nbsp; <br/>&nbsp; &nbsp; &nbsp; oWord.ActiveDocument.BuiltInDocumentProperties(4):='修改 &nbsp; 文档 &nbsp; 属性'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //关键词(Keywords) &nbsp; <br/>&nbsp; &nbsp; &nbsp; oWord.ActiveDocument.BuiltInDocumentProperties(5):='就是最好玩滴那个干滴好事啦,嘻嘻';//备注(Comments) &nbsp; <br/>&nbsp; &nbsp; &nbsp; //oWord.ActiveDocument.BuiltInDocumentProperties(6) &nbsp; //模板(Template) &nbsp; <br/>&nbsp; &nbsp; &nbsp; oWord.ActiveDocument.BuiltInDocumentProperties(7):='不记得是在哪个单位了'; &nbsp; //单位(Last &nbsp; author) &nbsp; <br/>&nbsp; &nbsp; &nbsp; oword.ActiveDocument.Save; &nbsp; <br/>&nbsp; &nbsp; &nbsp; oword.quit; &nbsp; <br/>&nbsp; end;&nbsp;&nbsp;</p>

solen 发表于 2009-3-31 11:31:00

本帖最后由 作者 于 2009-4-2 19:48:57 编辑 <br /><br /> <p>我在VB中用VBA在后台调出word文档并用printout打印,并在完成后关闭word。但出现一个问题就是,如果调出<br/>的word文档页码很多,在我关闭时还来不及输出到打打印机,word就会提示:word正在打印。如果退出word,会<br/>取消所有未完成的打印作业。是否要退出word。我如何才能知道打印输出完成了,这时我才去关闭word。&nbsp;&nbsp; <br/>&nbsp; VBA代码:&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveDocument.PrintOut&nbsp;&nbsp; <br/>&nbsp; '在这里检测并等待word打印输出完毕。就是不知怎么做。&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ActiveWindow.Close&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application.Quit. <br/>解决方法<br/>XXXXXXXXX.PrintOut&nbsp;&nbsp; Background:=False&nbsp; </p><p>&nbsp;&nbsp;&nbsp; Set wb = ThisWorkbook<br/>&nbsp;&nbsp;&nbsp; With wb<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = .Title '标题<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = .Subject '主题<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = .Author '作者<br/>&nbsp;&nbsp;&nbsp; End With</p>

sailorcwx 发表于 2009-3-31 13:41:00

<p>采用后台打印</p>
页: [1]
查看完整版本: 用VB控制word打印后怎么关闭word