2011-09-20 181 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); 
相關問題