示例#1
0
        private void ConvertFile(string filepath)
        {
            var fileformat = Word.WdSaveFormat.wdFormatXMLDocument;

            var newfilepath = Path.ChangeExtension(filepath, ".docx");

            Console.WriteLine($"{Path.GetFileName(filepath)} -> {Path.GetFileName(newfilepath)}");
            Word._Document document = _word.Documents.Open(
                filepath,
                ConfirmConversions: false,
                ReadOnly: true,
                AddToRecentFiles: false,
                PasswordDocument: Type.Missing,
                PasswordTemplate: Type.Missing,
                Revert: false,
                WritePasswordDocument: Type.Missing,
                WritePasswordTemplate: Type.Missing,
                Format: Type.Missing,
                Encoding: Type.Missing,
                Visible: false,
                OpenAndRepair: false,
                DocumentDirection: Type.Missing,
                NoEncodingDialog: true,
                XMLTransform: Type.Missing);
            document.Convert();
            string v = _word.Version;

            switch (v)
            {
            case "14.0":
            case "15.0":
                document.SaveAs2(newfilepath, fileformat, CompatibilityMode: Word.WdCompatibilityMode.wdWord2007);
                break;

            default:
                document.SaveAs(newfilepath, fileformat);
                break;
            }
            document.Close();
            document = null;
        }
        static void Main(string[] args)
        {
            Word._Application application = new Word.Application();
            object            fileformat  = Word.WdSaveFormat.wdFormatXMLDocument;
            DirectoryInfo     directory   = new DirectoryInfo(@"D:\abc");

            foreach (FileInfo file in directory.GetFiles("*.doc", SearchOption.AllDirectories))
            {
                if (file.Extension.ToLower() == ".doc")
                {
                    object         filename    = file.FullName;
                    object         newfilename = file.FullName.ToLower().Replace(".doc", ".docx");
                    Word._Document document    = application.Documents.Open(filename);
                    document.Convert();
                    document.SaveAs(newfilename, fileformat);
                    document.Close();
                    document = null;
                }
            }
            application.Quit();
            application = null;
        }
示例#3
0
        public void ConvertDocument()
        {
            Word._Application application = new Word.Application();
            object            fileformat  = Word.WdSaveFormat.wdFormatXMLDocument;
            DirectoryInfo     directory   = new DirectoryInfo(@_sourcePath);

            foreach (FileInfo file in directory.GetFiles("*.doc", SearchOption.AllDirectories))
            {
                if (file.Extension.ToLower() == ".doc")
                {
                    object         filename    = file.FullName;
                    string         newfilename = file.Name.Replace(".doc", ".docx");
                    Word._Document document    = application.Documents.Open(filename);

                    document.Convert();
                    document.SaveAs2(_targetpath + '/' + newfilename, fileformat);
                    document.Close();
                    document = null;
                }
            }
            application.Quit();
            application = null;
        }
示例#4
0
        public static bool convertToNewOfficeDocument(string fileName, out string newFileName, out string newExt)
        {
            string ext = Path.GetExtension(fileName);

            newExt = ext;


            newFileName = fileName;


            if (!destinationDictionary.ContainsKey(ext))
            {
                return(false);
            }

            officeDestination dest = destinationDictionary[ext];

            newExt = dest.Extension;

            newFileName = fileName.Replace(ext, dest.Extension);

            object obj;

            bool result = true;

            switch (dest.Application)
            {
            case officeApplication.Word:
                Word._Application wordApp = new Word.Application();
                Word._Document    wordDoc = null;

                try
                {
                    wordDoc = wordApp.Documents.Open(fileName);
                    wordDoc.Convert();
                    wordDoc.SaveAs(newFileName, dest.FileFormat);
                }
                catch (Exception e)
                {
                    displayError(fileName, e);
                    newFileName = fileName;
                    result      = false;
                }
                finally
                {
                    wordDoc.Close();
                    wordApp.Quit();
                    obj = (object)wordDoc;
                    disposeInteropObject(ref obj);
                    obj = (object)wordApp;
                    disposeInteropObject(ref obj);
                }
                break;

            case officeApplication.Excel:
                Excel._Application excelApp = new Excel.Application();
                Excel._Workbook    excelDoc = null;
                try
                {
                    excelDoc = excelApp.Workbooks.Open(fileName);
                    excelDoc.SaveAs(newFileName, dest.FileFormat);
                }
                catch (Exception e)
                {
                    displayError(fileName, e);
                    newFileName = fileName;
                    result      = false;
                }
                finally
                {
                    excelDoc.Close();
                    excelApp.Quit();
                    obj = (object)excelDoc;
                    disposeInteropObject(ref obj);
                    obj = (object)excelApp;
                    disposeInteropObject(ref obj);
                }
                break;

            case officeApplication.PowerPoint:
                PowerPoint._Application  powerpointApp = new PowerPoint.Application();
                PowerPoint._Presentation powerpointDoc = null;
                try
                {
                    powerpointDoc = powerpointApp.Presentations.Open(fileName, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
                    powerpointDoc.SaveAs(newFileName, (PowerPoint.PpSaveAsFileType)dest.FileFormat);
                }
                catch (Exception e)
                {
                    displayError(fileName, e);
                    newFileName = fileName;
                    result      = false;
                }
                finally
                {
                    powerpointDoc.Close();
                    powerpointApp.Quit();
                    obj = (object)powerpointDoc;
                    disposeInteropObject(ref obj);
                    obj = (object)powerpointApp;
                    disposeInteropObject(ref obj);
                }
                break;
            }

            GC.Collect();
            return(result);
        }