2

I have multiple word files (more than 1500), and I want to convert them to PDF programmatically. I use the following function

public static void convertor(string filename)
{

    Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
    File.SetAttributes(filename, FileAttributes.Temporary);
    wordDocument = appWord.Documents.Open(filename);
    wordDocument.ExportAsFixedFormat(filename.ToString() + ".pdf", WdExportFormat.wdExportFormatPDF);

}

but I get the following dialog for each file

enter image description here

How can I unlock the files programmatically?

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Hesham
  • 41
  • 6
  • interop is yucky for releasing stuff properly. Related to excel interop, but might be of use: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – Kritner Dec 06 '16 at 19:18

2 Answers2

2

You'll have to change your method call to Open to pass along a read-only flag.

appWord.Documents.Open(FileName:=filename, ReadOnly:=True);

Read more over at Microsoft. You probably want to only open one instance of Word -- and explicitly close each file when you're done -- because with the number of files you're processing, you'll run out of RAM quickly.

Xavier J
  • 4,326
  • 1
  • 14
  • 25
0

I moved all the files from system partition to removable flash memory and it's worked.

Hesham
  • 41
  • 6