场景:我有大约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个文件,这种方法需要相当长的时间。
编辑:代码再次更改。现在使用线程池,似乎运行速度更快。仍需在更好的电脑上运行以转换所有文档。或者按文件夹慢慢进行转换。有人能想到其他优化方法吗?