C# 以编程方式将Doc转换为Docx错误

C# 以编程方式将Doc转换为Docx错误,c#,java,interop,docx,doc,C#,Java,Interop,Docx,Doc,我正在尝试将服务器中的所有.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 { publi

我正在尝试将服务器中的所有.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");

它创建的文件正好位于它应该位于的位置。为什么会发生这种情况?

路径。GetFileName WithOutExtension()只返回文件名,而不是完整路径

最后将值
new.docx
作为第二个参数传递给
ConvertDocToDocx
,而不是完整的文件路径。它可能在某处将其写入磁盘,但谁知道确切的位置


使用
Path.GetDirectoryName()
也可以获取完整目录:

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

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

您将
false
指定给
True
的任何原因?这不完全是我的代码。我在网上找到了它,并修改了main方法以满足服务器的需要。@a您可能需要打印
file
Path.GetFileNameWithoutExtension(file)
的值。查看它是否与
“Z:\\PREE\\1-ABC\\Alan Wernick\\new.doc”
“Z:\\PREE\\1-ABC\\Alan Wernick\\new”