VB.Net如何从带有图像的MS Word复制页面

时间:2016-11-12 14:01:27

标签: vb.net ms-word

我需要将ms word文档拆分为每个页面的单独文档 我有以下代码,但只有页面包含图像时才会失败 例如,第1页和第2页包含文本,第3页和第4页各包含一个图像。我需要将第3页和第4页保存为单个文档以及第1页和第2页 失败的一行是:

WordApp.Selection.Copy()

有错误:
System.Runtime.InteropServices.COMException:此方法或属性不可用,因为未选择任何文本。

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()

        '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()

            DestDoc = WordApp.Documents.Add
            DestDoc.Activate()
            WordApp.Selection.Paste()
            DestDoc.SaveAs("E:\0099-Education\WordAutomate_Sln\WordAutomate\bin\Debug\" & NewFileName & intPage.ToString() & ".docx")
            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()
        BaseDoc = Nothing

        WordApp.Quit()
        WordApp = Nothing

    End Sub

0 个答案:

没有答案