方案:我有大約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:再次更改了代碼。現在使用線程池。似乎跑得快一點。仍然需要在更好的計算機上運行以轉換所有文檔。或者按文件夾緩慢進行。任何人都可以想出任何其他方式來優化這個?
我想知道使用線程,但是當我跑你的代碼的第一個版本,我看到它只用一個線程就佔用了我的兩個核心的100%,所以我不認爲並行化會像更快的計算機那樣幫助解決問題。你使用什麼樣的電腦? –
Windows XP x86,Intel Pentium Dual CPU @ 2.00GHZ,3.25 GB RAM。工作電腦... – Gage
除了我的是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左右。 –