【问题标题】:How to remove specific pages from word document如何从word文档中删除特定页面
【发布时间】:2015-05-18 19:39:21
【问题描述】:

我正在使用vb.net,并且我正在编辑一个 Word 文档。

我只想从第 6 页(例如 )到文档末尾而不是整个文档中删除分页符。

我的代码适用于整个文档 - 我应该如何更改它?

Dim paragraphs As Word.Paragraphs
paragraphs = doc.Paragraphs

For Each paragraph As Word.Paragraph In paragraphs
    If paragraph.Range.Text.Trim() = String.Empty Then
        paragraph.Range.[Select]()
        wordapp.Selection.Delete()
    End If
Next

【问题讨论】:

    标签: vb.net ms-word


    【解决方案1】:

    这对我有用。这将从第 6 页中删除分页符(如果存在)。

    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Form1
    
        '~~> Define your Excel Objects
        Dim wrdApp As New Word.Application
        Dim wrdDoc As Word.Document
        '~~> Page NO
        Dim pgNo As Integer = 6
        Dim i As Integer = 0
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            wrdDoc = wrdApp.Documents.Open("C:\Users\Siddharth\Desktop\Document1.docx")
    
            '~~> Display Word
            wrdApp.Visible = True
    
            With wrdDoc
                For i = .Paragraphs.Count To 1 Step -1
                    If Asc(.Paragraphs(i).Range.Text) = 12 And _
                       .Paragraphs(i).Range.Information(Word.WdInformation.wdActiveEndPageNumber) = pgNo Then
                        .Paragraphs(i).Range.Delete()
                        Exit For
                    End If
                Next i
            End With
        End Sub
    End Class
    

    【讨论】:

    • 上面的代码已经测试过,试过了 :) 你有没有在这行设置页码Dim pgNo As Integer = 6
    • 是的,但问题是我需要从第 6 页到文档末尾。我试图这样做但没有成功: With doc For i = .Paragraphs.Count To 1 Step -1 If (Asc(.Paragraphs(i).Range.Text) = 12 And (.Paragraphs(i).Range.Information( Word.WdInformation.wdActiveEndPageNumber) > PageNumberToStart)) Then .Paragraphs(i).Range.Delete() End If Next i End With
    • 哦,然后修改循环中的代码。将= pgNo 更改为>= pgNo。保持原样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多