簡體   English   中英

以編程方式將Doc轉換為Docx錯誤

[英]Programatically Converting Doc to Docx bug

我試圖將服務器中的所有.doc文檔轉換為.docx格式,以便使用Java以編程方式對其進行修改。 我不太擅長C#,但是我能夠找到此程序並根據需要對其進行修改。

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

namespace ConvertDOCtoDOCx
{
public class ObjectConstants
{
public static Object MissingValue = System.Reflection.Missing.Value;
public static Object True = true;
public static Object False = true;
}

class Program
{
    static void Main(string[] args)
    {
        string path = "Z:\\PREE\\";

        foreach (string letters in Directory.GetDirectories(path))
        {
            foreach (string students in Directory.GetDirectories(letters))
            {
                foreach (string file in System.IO.Directory.GetFiles(students, "*.doc"))
                {
                    Console.Write(file);
                    ConvertDocToDocx(file, Path.GetFileNameWithoutExtension(file) + ".docx");
                }
            }

        }

    }

    public static void ConvertDocToDocx(string docFilePath, string outputDocxFilePath)
    {
        var app = new Application();
        app.Visible = false;
        var doc = OpenDocument(app, docFilePath, false);
        SaveDocAsDocx(app, doc, outputDocxFilePath);
        app.Quit(ref ObjectConstants.False, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue);
    }

    public static void SaveDocAsDocx(Application app, Document doc, object outputFilePath)
    {
        object format = WdSaveFormat.wdFormatXMLDocument;

        try
        {

            doc.SaveAs(ref outputFilePath, ref format,
            ref ObjectConstants.False, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue);
        }
        catch (NullReferenceException)
        {
            return;
        }
    }

    public static Document OpenDocument(Application app, object filePath, bool visible)
    {
        try
        {
            var doc = (Document)app.Documents.Open(ref filePath, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue,
            ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue);


            if (!visible)
            {
                doc.ActiveWindow.Visible = false;
            }

            return doc;
        }
        catch (System.Runtime.InteropServices.COMException e)
        {
            return null;
        }
    }
}
}

每次運行它時,都不會創建新的docx文件。 我能夠縮小問題的范圍,發現以下幾行是罪魁禍首:

ConvertDocToDocx(file, Path.GetFileNameWithoutExtension(file) + ".docx");

當我保持原樣時,什么也不會創建。 但是當我像以下示例一樣手動添加它時:

ConvertDocToDocx("Z:\\PREE\\1-ABC\\Alan Wernick\\new.doc", "Z:\\PREE\\1-ABC\\Alan Wernick\\new.docx");

它將創建文件的確切位置。 為什么會這樣呢?

Path.GetFileNameWithoutExtension()僅返回文件名,而不返回完整路徑。

您最終將值new.docx作為第二個參數傳遞給ConvertDocToDocx ,而不是完整文件路徑。 它可能正在將它寫入磁盤中的某個地方 ,但是誰知道確切的位置。


也可以使用Path.GetDirectoryName()獲取完整目錄:

var baseFile =
   Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file));

ConvertDocToDocx(file, string.Concat(baseFile, ".docx"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM