2011-09-20 102 views
4

方案:我有大约14000个文档需要从“Microsoft Word 97 - 2003 Document”转换为“Microsoft Word Document”。换句话说升级到2010格式(.docx)。有没有办法将Word文档升级到2010

问题:有没有一种简单的方法来使用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` 

它让我再选择的路径找到子文件夹内的所有DOC文件当前功能。该代码并不重要,所有用于转换的文件都在lstFiles中。目前唯一的问题是,即使只转换10个文档也需要很长时间。我应该每个文档使用一个单词应用程序,而不是重复使用它?有什么建议么?

此外,它会在大约2或3次转换后打开单词并开始闪烁但不断转换。

EDIT3:调整为稍高于一点的代码,它运行更干净。花1min10sec转换8个文件。考虑到我有14000我需要转换此方法将需要相当长的时间。

EDIT4:再次更改了代码。现在使用线程池。似乎跑得快一点。仍然需要在更好的计算机上运行以转换所有文档。或者按文件夹缓慢进行。任何人都可以想出任何其他方式来优化这个?

+0

我想知道使用线程,但是当我跑你的代码的第一个版本,我看到它只用一个线程就占用了我的两个核心的100%,所以我不认为并行化会像更快的计算机那样帮助解决问题。你使用什么样的电脑? –

+0

Windows XP x86,Intel Pentium Dual CPU @ 2.00GHZ,3.25 GB RAM。工作电脑... – Gage

+0

除了我的是64位的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左右。 –

回答

2

我跑你的代码在本地,与只是一些小修改改善了追踪和时间安排,并且“仅”用了13.73秒来完成12个文件。这将在4小时内处理你的14,000。我使用双核处理器在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

您可以使用免费的Office文件转换器。

这里介绍的设置:

http://technet.microsoft.com/en-us/library/cc179019.aspx

有一个文件列表的设置。

+0

我使用该程序尝试,但它只能转换为.docx文件,但它仍然在兼容模式打开给我转换选项(如果是有道理的) – Gage

+0

那么你将不得不在Office 2007中尝试打开他们打开一个问题2007年的文件。你将得到相同的提示。 –

+0

我甚至设置FullUpgradeOnOpen = 1,但它仍然只是将文件重命名为.docx。不实际转换它。 – Gage

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); 
相关问题