c# - 以编程方式将 Doc 转换为 Docx bug

标签 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
{
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,而不是完整的文件路径。它可能将其写入磁盘某个地方,但谁知 Prop 体在哪里。

<小时/>

使用Path.GetDirectoryName()来获取完整目录:

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

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

关于c# - 以编程方式将 Doc 转换为 Docx bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23002876/

相关文章:

c# - 在 .Select lambda 中使用 async/await

c# - SqlBulkCopy 抛出 "Operation is not valid due to the current state of the object"

c# - WPF 窗口位置绑定(bind)

c# - 如何为 Kentor idprovider 添加自定义元数据?

java - 添加默认包导入

c# - 该语言不支持“MethodName”

java - 以这种方式将子类转换为父类有意义吗?

java - 将 mysql 正则表达式转换为 java 正则表达式(和/或相反)

c# - 有没有办法以编程方式将数字签名添加到 word 文档中的 VBA 宏?

.net - 当我的 .net 程序集不再位于同一位置时,如何取消注册?