2011-03-11 159 views

回答

5

相同的代碼,修改一個單一的文件:

using Microsoft.Office.Interop.Word; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 

... 

// Create a new Microsoft Word application object 
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 

// C# doesn't have optional arguments so we'll need a dummy value 
object oMissing = System.Reflection.Missing.Value; 

// Get a Word file 
FileInfo wordFile = new FileInfo("myDoc.doc"); 

word.Visible = false; 
word.ScreenUpdating = false; 

// Cast as Object for word Open method 
Object filename = (Object)wordFile.FullName; 

// Use the dummy value as a placeholder for optional arguments 
Document doc = word.Documents.Open(ref filename, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
doc.Activate(); 

object outputFileName = wordFile.FullName.Replace(".doc", ".pdf"); 
object fileFormat = WdSaveFormat.wdFormatPDF; 

// Save document into PDF Format 
doc.SaveAs(ref outputFileName, 
    ref fileFormat, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

// Close the Word document, but leave the Word application open. 
// doc has to be cast to type _Document so that it will find the 
// correct Close method.     
object saveChanges = WdSaveOptions.wdDoNotSaveChanges; 
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); 
doc = null; 

// word has to be cast to type _Application so that it will find 
// the correct Quit method. 
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
word = null; 
2

代碼正在尋找一個文件夾中的所有.doc文件,並通過他們的循環。如果你只是在文件夾中有一個文件,它只會轉換那個文件。

你可以改變這行代碼:

FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); 

只爲你尋找文件,如

FileInfo[] wordFiles = dirInfo.GetFiles("myselectedfile.doc");