按特定页面范围拆分Word文档

时间:2017-07-27 18:04:32

标签: vb.net ms-word

我是stackoverflow的新手,我对vb.net非常了解。但是我陷入了一些非常简单的事情

我想使用vb.net拆分word文档。

这是我试图拆分它并且它按我想要的方式工作。此代码逐页拆分文档。

Value  Depth

12       0

13       1

14       1

25       2

20       2

21       2

16       2

23       3

24       3

但是我试图调整它以便我可以按页面范围拆分它。例如。我想从50页的文档中提取3-7页。但我无法这样做。

尝试以下但我无法让它发挥作用。我做错了什么?

Imports Word = Microsoft.Office.Interop.Word

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oWordApp As New Word.Application
        Dim oWordMainDoc As Word.Document
        Dim docSingle As Word.Document
        Dim rngPage As Word.Range
        Dim strNewFileName As String = ""

        Dim iCurrentPage As Integer, iPageCount As Integer

        oWordMainDoc = oWordApp.Documents.Open(Filename:=TextBox1.Text, ReadOnly:=True)
        oWordMainDoc.ActiveWindow.View.Type = 3

        rngPage = oWordMainDoc.Range

        iCurrentPage = 1

        iPageCount = oWordMainDoc.Content.ComputeStatistics(2)

        Do Until iCurrentPage > iPageCount
            If iCurrentPage = iPageCount Then
                rngPage.End = oWordMainDoc.Range.End
            Else
                oWordApp.Selection.GoTo(1, 1, iCurrentPage + 1)
                rngPage.End = oWordApp.Selection.Start
            End If

            docSingle = oWordApp.Documents.Add

            rngPage.Copy()
            docSingle.Range.PasteAndFormat (19)
            docSingle.Range.Find.Execute(FindText:="^m", ReplaceWith:="")

            strNewFileName = Replace(oWordMainDoc.FullName, ".doc", "_" & iCurrentPage & "_.doc")

            docSingle.SaveAs (strNewFileName)
            iCurrentPage = iCurrentPage + 1
            docSingle.Close()
            rngPage.Collapse (0)
        Loop
    End Sub
End Class

0 个答案:

没有答案