用VBA删除Word文档中的空白页 [英] Deleting Blank Pages in Word Doc with VBA

查看:174
本文介绍了用VBA删除Word文档中的空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编写一个函数来删除 Word 文档中的空白页.什么都不会被删除.如果有人可以看看,我将不胜感激.

Trying to write a function to delete blank pages in a Word doc. Nothing gets deleted. I appreciate it if somebody can take a look.

Public Function DeleteBlankPages(wd As Word.Document, wdApp As Word.Application)  
    Dim par As Paragraph
    For Each par In wd.Paragraphs
        If IsEmpty(par.Range.Text) Then
            par.Range.Select
            wdApp.Selection.Delete
        End If
    Next par       
End Function

推荐答案

在 VBA 语言参考中查找 IsEmpty 的定义.它不像你想象的那样.

Look up the definition of IsEmpty in the VBA language reference. It doesn't do what you imagine.

判断是否有文字内容的正确方法是检查字符数.在 VBA 中,这通常通过函数 Len (=length) 完成.您会认为比较应该是 0(零),但段落并非如此,因为 Word 段落总是包含它的段落标记(ANSI 13).

The correct way to find out if there is textual content is to check the number of characters. In VBA, this is typically done with the function Len (=length). You'd think that it the comparision should be to 0 (zero), but that's not the case for a paragraph, because a Word paragraph always contains it's paragraph mark (ANSI 13).

此外,无需选择段落或范围即可删除它,只需直接在par.RangeDelete方法即可>.(这意味着您也不需要传递 Word.Application 对象.

Also, no need to select the paragraph or range in order to delete it, just use the Deletemethod directly on the par.Range. (Which means you also don't need to pass a Word.Application object.

另请注意,您的代码不会对页面执行任何操作,只会对段落执行任何操作...它可能删除空页面,具体取决于内容的格式,但重命名函数并评论它应该如何工作.

Also note that your code doesn't do anything to pages, only to paragraphs... It could deleted empty pages, depending on how things are formatted, but it might be wise to rename the Function and comment how it's supposed to work.

更像这样:

Public Function DeleteBlankPages(wd As Word.Document)  
    Dim par As Paragraph
    For Each par In wd.Paragraphs
        If Len(par.Range.Text) <= 1 Then
            par.Range.Delete
        End If
    Next par       
End Function

这篇关于用VBA删除Word文档中的空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆