2
一個單詞文檔具有多個頁面。如何使用VB.Net將這些頁面分成單獨的文檔?VB.Net將單詞文檔分解爲單獨的文檔(分頁符作爲分隔符)
我希望自動執行此過程。
我使用ms教程進行基本學習:http://support.microsoft.com/kb/316383 但我不知道如何在文檔中查找分頁符並將該頁面的內容移動到單獨的文檔中。
一個單詞文檔具有多個頁面。如何使用VB.Net將這些頁面分成單獨的文檔?VB.Net將單詞文檔分解爲單獨的文檔(分頁符作爲分隔符)
我希望自動執行此過程。
我使用ms教程進行基本學習:http://support.microsoft.com/kb/316383 但我不知道如何在文檔中查找分頁符並將該頁面的內容移動到單獨的文檔中。
解決方案:
Private Sub ParseWordDoc(ByVal Filename As String, ByVal NewFileName As String)
Dim WordApp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
Dim BaseDoc As Microsoft.Office.Interop.Word.Document
Dim DestDoc As Microsoft.Office.Interop.Word.Document
Dim intNumberOfPages As Integer
Dim intNumberOfChars As String
Dim intPage As Integer
'Word Constants
Const wdGoToPage = 1
Const wdStory = 6
Const wdExtend = 1
Const wdCharacter = 1
'Show WordApp
WordApp.ShowMe()
'Load Base Document
BaseDoc = WordApp.Documents.Open(Filename)
BaseDoc.Repaginate()
'Loop through pages
intNumberOfPages = BaseDoc.BuiltInDocumentProperties("Number of Pages").value
intNumberOfChars = BaseDoc.BuiltInDocumentProperties("Number of Characters").value
For intPage = 1 To intNumberOfPages
If intPage = intNumberOfPages Then
WordApp.Selection.EndKey(wdStory)
Else
WordApp.Selection.GoTo(wdGoToPage, 2)
Application.DoEvents()
WordApp.Selection.MoveLeft(Unit:=wdCharacter, Count:=1)
End If
Application.DoEvents()
WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()
WordApp.Selection.Copy()
Application.DoEvents()
'Create New Document
DestDoc = WordApp.Documents.Add
DestDoc.Activate()
WordApp.Selection.Paste()
DestDoc.SaveAs(NewFileName & intPage.ToString & ".doc")
DestDoc.Close()
DestDoc = Nothing
WordApp.Selection.GoTo(wdGoToPage, 2)
Application.DoEvents()
WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()
WordApp.Selection.Delete()
Application.DoEvents()
Next
BaseDoc.Close(False)
BaseDoc = Nothing
WordApp.Quit()
WordApp = Nothing
End Sub
幸得 「Jay Taplin」
你有沒有看這個[SO問題](http://stackoverflow.com/questions/825254/word-automation-detect- if-page-break-is-necessary)它適用於C#,但應該給你一個想法。還可以看看這個[論壇](http://social.msdn.microsoft.com/Search/en-US/vstudio?query=word%20page%20breaks&rq=meta:Search.MSForums.ForumID(0e69520a-0af0 -4085-a8c4-c5f21ce20e01)+ site:microsoft.com&rn = Visual + Studio + Tools + for + Office +論壇) –
謝謝。但對我沒有多大幫助。我也搜索並找到了解決方案。我不知道這是不是很好的解決方案,但它有效。解答在下面作爲答案發布。如果您發現任何可以改進的地方,請發佈。 –