有没有一种方法可以将Word文档升级到2010版?

4

场景:我有大约14000个Word文档需要从“Microsoft Word 97-2003文档”转换为“Microsoft Word文档”,也就是升级到2010格式(.docx)。

问题:是否有一种简单的方法可以使用API或其他方式来完成这项工作?

注意:我只能找到一个微软程序将文档转换为.docx,但它们仍然以兼容模式打开。如果它们可以直接转换为新格式,那就太好了。与打开旧文档并给您转换选项时获得的相同功能。

编辑:刚刚发现http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.convert.aspx正在研究如何使用它。

EDIT2:这是我当前用于转换文档的函数

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
    FolderBrowserDialog1.ShowDialog()
    Dim mainThread As Thread
    If Not String.IsNullOrEmpty(FolderBrowserDialog1.SelectedPath) Then
        lstFiles.Clear()

        DirSearch(FolderBrowserDialog1.SelectedPath)
        ThreadPool.SetMaxThreads(1, 1)
        lstFiles.RemoveAll(Function(y) y.Contains(".docx"))
        TextBox1.Text += "Conversion started at " & DateTime.Now().ToString & Environment.NewLine
        For Each x In lstFiles
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ConvertDoc), x)
        Next

    End If
End Sub
Private Sub ConvertDoc(ByVal path As String)
    Dim word As New Microsoft.Office.Interop.Word.Application
    Dim doc As Microsoft.Office.Interop.Word.Document
    word.Visible = False

    Try
        Debug.Print(path)
        doc = word.Documents.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        doc.Convert()

    Catch ex As Exception
        ''do nothing
    Finally
        doc.Close()
        word.Quit()
    End Try

End Sub`

它让我选择一个路径,然后查找子文件夹中的所有文档文件。那段代码不重要,所有需要转换的文件都在lstFiles中。目前唯一的问题是即使只转换10个文档,它也需要很长时间才能完成。我应该使用每个文档一个单词应用程序,而不是重复使用它吗?有什么建议吗?
还有,它在大约转换了2或3个文档后打开Word并开始闪烁,但仍在继续转换。
编辑:上面的代码稍作修改,运行得更加流畅了。但是将8个文件转换需要1分10秒,考虑到我需要转换14000个文件,这种方法需要相当长的时间。
编辑:代码再次更改。现在使用线程池,似乎运行速度更快。仍需在更好的电脑上运行以转换所有文档。或者按文件夹慢慢进行转换。有人能想到其他优化方法吗?

我曾考虑使用线程,但当我运行了你的第一个代码版本时,我发现它只使用了一个线程就占用了我的两个核心的100%,因此我认为并行化不会像更快的计算机那样有助于解决问题。你用的是什么样的电脑? - Sam Skuce
Windows XP x86,Intel Pentium Dual CPU @ 2.00GHZ,3.25 GB RAM。工作计算机... - Gage
除了我的是x64 Windows 7,我们的情况非常相似。我想知道x86版本是不是比x64慢那么多,或者我们使用的是不同版本的office库。我正在使用“Microsoft Office 12.0 Object Library”版本2.4.0.0和“Microsoft Word 12.0 Object Library”版本8.4.0.0。另外,你转换的Word文档的平均大小是多少?我样本集中最大的大约是1 MB左右。 - Sam Skuce
另一个想法 - Office库实际上会在后台加载winword.exe的副本来完成其工作。也许Windows 7在进程启动和/或进程间通信方面做得更好。你有可以在其上运行它的Windows 7计算机吗? - Sam Skuce
@Sam Skuce,这些文档的大小在60kb到120kb之间,所以并不算大。我将在未来几年内(下一次更新)没有可以运行它的Windows 7机器。我使用线程是因为它限制了同时进行的转换数量。 - Gage
4个回答

2

2
我在本地运行了你的代码,只进行了一些小的编辑以提高跟踪和计时的准确性,它仅花费了13.73秒处理12个文件。这样处理14000个文件大约需要4小时时间。我在Windows 7 x64上使用双核处理器运行Visual Studio 2010。也许你可以使用更快的电脑?
以下是完整代码,在此窗体中只有一个按钮Button1和一个FolderBrowserDialog FolderBrowserDialog1:
Imports System.IO

Public Class Form1

Dim lstFiles As List(Of String) = New List(Of String)

Private Sub DirSearch(path As String)


    Dim thingies = From file In Directory.GetFiles(path) Where file.EndsWith(".doc") Select file

    lstFiles.AddRange(thingies)

    For Each subdir As String In Directory.GetDirectories(path)
        DirSearch(subdir)
    Next
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    FolderBrowserDialog1.ShowDialog()

    If Not String.IsNullOrEmpty(FolderBrowserDialog1.SelectedPath) Then
        lstFiles.Clear()

        DirSearch(FolderBrowserDialog1.SelectedPath)
        Dim word As New Microsoft.Office.Interop.Word.Application
        Dim doc As Microsoft.Office.Interop.Word.Document
        lstFiles.RemoveAll(Function(y) y.Contains(".docx"))
        Dim startTime As DateTime = DateTime.Now
        Debug.Print("Timer started at " & DateTime.Now().ToString & Environment.NewLine)
        For Each x In lstFiles
            word.Visible = False
            Debug.Print(x + Environment.NewLine)
            doc = word.Documents.Open(x)
            doc.Convert()
            doc.Close()
        Next
        word.Quit()
        Dim endTime As DateTime = DateTime.Now
        Debug.Print("Took " & endTime.Subtract(startTime).TotalSeconds & " to process " & lstFiles.Count & " documents" & Environment.NewLine)
    End If

End Sub
End Class

1

试试这个:

using Microsoft.Office.Interop
Microsoft.Office.Interop.Word.ApplicationClass word = new ApplicationClass();
object nullvalue = Type.Missing;
object filee = filename;
object file2 = String.Format("{0}{1}", filepath, "convertedfile.doc");
Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filee, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue);
        doc.SaveAs(ref file2, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue, ref nullvalue);

1

我尝试使用那个程序,但它只将文件转换为.docx格式,但仍以兼容模式打开,给我提供了转换选项(如果这有意义的话)。 - Gage
如果您尝试在Office 2007中打开它们,那么您将会遇到问题。尝试在2010年中打开一个2007文档,您将会得到相同的提示。 - Daniel A. White
我甚至设置了FullUpgradeOnOpen=1,但它仍然只将文件重命名为.docx。实际上并没有转换它。 - Gage
1
尝试将其重命名为 .zip。尝试打开它。 - Daniel A. White
1
根据Technet文章所述:“当用户在Word 2010中打开转换后的.docx文件时,该文件会以Word 2007兼容模式打开。OFC不支持将.doc文件转换为Word 2010 .docx格式。” 因此,该程序不会进行完全转换,尽管它似乎比简单重命名多做了一些工作。 - Sam Skuce

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接