简体   繁体   English

如何关闭使用Word.SaveAs2方法创建的文件?

[英]How can I close a file created with Word.SaveAs2 method?

I am running Visual Studio 2015 Community, visual basic. 我正在运行Visual Studio 2015社区,Visual Basic。 I am referencing Microsoft.Office.Interop.Word . 我正在引用Microsoft.Office.Interop.Word

The code below creates a PDF file by converting a simple Word file using the Doc.SaveAs2 method with the PDF parameter for the format. 下面的代码通过使用带有PDF参数的Doc.SaveAs2方法转换简单的Word文件来创建PDF文件。

My problem is that when I try to delete the PDF file later in the application, it tells me that it is still in use. 我的问题是,当我稍后尝试在应用程序中删除PDF文件时,它告诉我它仍在使用中。

It seems like all I should need to do would be to close the file, but I'm not sure how to do that. 看来我应该要做的就是关闭文件,但是我不确定该怎么做。 The code below closes the Word document but not the PDF. 下面的代码关闭Word文档,而不关闭PDF。 I've tried using the FileClose() method but it only accepts an integer as a parameter. 我尝试使用FileClose()方法,但它仅接受整数作为参数。

I've tried it with no value, a 1 and a 2, but I'm still getting the 我已经尝试了没有值的1和2,但是我仍然得到了

"file in use" “文件正在使用中”

error when I try to delete it. 我尝试删除它时出错。

I'm very new to VB coding and would appreciate any help. 我是VB编码的新手,希望能对您有所帮助。

    Private Sub CreateTitlePage()

    Dim wdApp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application
    Dim wdDoc As Microsoft.Office.Interop.Word.Document = New Microsoft.Office.Interop.Word.Document
    Dim wdPara1 As Microsoft.Office.Interop.Word.Paragraph
    'Dim wdPage1 As Microsoft.Office.Interop.Word.Page

    wdDoc.Application.Visible = False
    wdDoc.PageSetup.VerticalAlignment = WdVerticalAlignment.wdAlignVerticalCenter

    wdPara1 = wdDoc.Content.Paragraphs.Add
    wdPara1.Range.InsertParagraphAfter()
    wdPara1.Range.Text = "BINDER DOCUMENT" + vbVerticalTab + vbVerticalTab + "Created on:  " + formattedDate2
    wdPara1.Range.Font.Bold = True
    wdPara1.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter

    ' The following statements save the document and close the Word application
    wdDoc.SaveAs(binderNameDoc)
    wdDoc.Close()
    wdApp.Quit()

    ConvertWordToPDF(tbProject.Text + "\", "Binder" + formattedDate, docLit)

End Sub

    Public Sub ConvertWordToPDF(myPathName As String, myFileName As String, myFileExt As String)

    Dim myWordName As String = myPathName + myFileName + myFileExt
    Dim myPDFName As String = myPathName + myFileName + pdfLit
    Dim word As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
    Dim doc As Microsoft.Office.Interop.Word.Document = word.Documents.Open(myWordName)
    Dim doc2 As PdfDocument = New PdfDocument
    doc.Activate()
    doc.SaveAs2(myPDFName, WdSaveFormat.wdFormatPDF)
    doc.Close()

    ReDim Preserve pdfArray(pdfArray.Length)
    pdfArray(countConversions) = myPDFName
    countConversions = countConversions + 1

End Sub

Word is notorious for maintaining file locks, so sometimes your best bet is to search for the files generated in a previous session and delete them at the start of the next session, however... Word因维护文件锁而臭名昭著,因此有时最好的选择是搜索上一个会话中生成的文件,然后在下一个会话开始时将其删除。

Since this is not VBA - not running in-process - and not COM it's very important that you properly release the COM resources. 由于这不是VBA,也不是在进程内运行,也不是COM,因此正确释放COM资源非常重要。 If you don't, objects won't be released from memory when you expect and won't be garbage collected. 如果不这样做,对象将不会在您期望的时候从内存中释放出来,也不会被垃圾回收。

For every object you instantiate in the COM area, you need to specifically release it. 对于在COM区域中实例化的每个对象,都需要专门释放它。 And you should also trigger Garbage Collection to ensure the objects are completely released. 并且您还应该触发垃圾回收以确保对象被完全释放。 Years ago, Andrew Whitechapel wrote a great book on working with Office using .NET and the chapter dealing with this is available on MSDN: https://msdn.microsoft.com/en-us/library/office/aa679807%28v=office.11%29.aspx?f=255&MSPPError=-2147217396 几年前,安德鲁·怀特查佩尔(Andrew Whitechapel)写了一本很棒的书,介绍了如何使用.NET与Office一起工作,有关此问题的章节可在MSDN上找到: https : //msdn.microsoft.com/zh-cn/library/office/aa679807%28v=office .11%29.aspx?f = 255&MSPPError = -2147217396

In a nutshell, you need to set every object you declare explicitly to Nothing, in the reverse order they were instantiated, after you're finished with them. 简而言之,您需要在完成对对象的声明后,以相反的顺序将每个声明的对象显式设置为Nothing。 So: 所以:

wdDoc.Close()
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
'Now garbage collection

Beyond that, I note in the code you post that you call an entirely separate instance of the Word.Application in your second procedure ConvertWordToPDF. 除此之外,我还注意到在您发布的代码中,您在第二个过程ConvertWordToPDF中调用了Word.Application的完全独立的实例。 I'm not sure why you use a second instance, but the same applies here, as well. 我不确定为什么要使用第二个实例,但是同样适用于此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM