简体   繁体   English

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

[英]Programatically Converting Doc to Docx bug

I am trying to convert all the .doc documents in a server to a .docx format in order to modify them programatically using Java. 我试图将服务器中的所有.doc文档转换为.docx格式,以便使用Java以编程方式对其进行修改。 I am not very adept at C#, but I was able to find this program and modify it for my needs. 我不太擅长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;
        }
    }
}
}

Every time I run it, the new docx files are not being created. 每次运行它时,都不会创建新的docx文件。 I was able to narrow down the problem and I found the following line to be the culprit: 我能够缩小问题的范围,发现以下几行是罪魁祸首:

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

When I leave the line as it is, nothing is created. 当我保持原样时,什么也不会创建。 But when I manually add it like the following example: 但是当我像以下示例一样手动添加它时:

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

It creates the file exactly where its supposed to be. 它将创建文件的确切位置。 Why is this happening? 为什么会这样呢?

Path.GetFileNameWithoutExtension() only returns the name of the file, not the full path. Path.GetFileNameWithoutExtension()仅返回文件名,而不返回完整路径。

You end up passing the value new.docx as the second parameter to ConvertDocToDocx , not the full file path. 您最终将值new.docx作为第二个参数传递给ConvertDocToDocx ,而不是完整文件路径。 It's probably writing it to disk somewhere , but who knows exactly where. 它可能正在将它写入磁盘中的某个地方 ,但是谁知道确切的位置。


Use Path.GetDirectoryName() to get the full directory as well: 也可以使用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