vb.net - How can I close a file created with Word.SaveAs2 method? -


i running visual studio 2015 community, visual basic. referencing microsoft.office.interop.word.

the code below creates pdf file converting simple word file using doc.saveas2 method pdf parameter format.

my problem when try delete pdf file later in application, tells me still in use.

it seems should need close file, i'm not sure how that. code below closes word document not pdf. i've tried using fileclose() method accepts integer parameter.

i've tried no value, 1 , 2, i'm still getting

"file in use"

error when try delete it.

i'm new vb coding , appreciate help.

    private sub createtitlepage()      dim wdapp microsoft.office.interop.word.application = new microsoft.office.interop.word.application     dim wddoc microsoft.office.interop.word.document = new microsoft.office.interop.word.document     dim wdpara1 microsoft.office.interop.word.paragraph     'dim wdpage1 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      ' following statements save document , close word application     wddoc.saveas(bindernamedoc)     wddoc.close()     wdapp.quit()      convertwordtopdf(tbproject.text + "\", "binder" + formatteddate, doclit)  end sub      public sub convertwordtopdf(mypathname string, myfilename string, myfileext string)      dim mywordname string = mypathname + myfilename + myfileext     dim mypdfname string = mypathname + myfilename + pdflit     dim word microsoft.office.interop.word.application = new microsoft.office.interop.word.application()     dim doc microsoft.office.interop.word.document = word.documents.open(mywordname)     dim doc2 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 notorious maintaining file locks, best bet search files generated in previous session , delete them @ start of next session, however...

since not vba - not running in-process - , not com it's important release com resources. if don't, objects won't released memory when expect , won't garbage collected.

for every object instantiate in com area, need release it. , should trigger garbage collection ensure objects released. years ago, andrew whitechapel wrote great book on working office using .net , chapter dealing available on msdn: https://msdn.microsoft.com/en-us/library/office/aa679807%28v=office.11%29.aspx?f=255&mspperror=-2147217396

in nutshell, need set every object declare explicitly nothing, in reverse order instantiated, after you're finished them. so:

wddoc.close() wdapp.quit set wddoc = nothing set wdapp = nothing 'now garbage collection 

beyond that, note in code post call entirely separate instance of word.application in second procedure convertwordtopdf. i'm not sure why use second instance, same applies here, well.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -