vba 如何使用宏删除 Word 文档中的所有文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8806355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I delete all text in a Word document using a macro?
提问by Garrett Hall
I want to delete all the text in a Word document leaving the images/embedded files.
我想删除 Word 文档中的所有文本,留下图像/嵌入文件。
I know this should be easy, but can't a good example on the web.
我知道这应该很容易,但在网络上不能成为一个很好的例子。
采纳答案by Franck LEVEQUE
Text is of Type wdSelectionNormal
文本类型为 wdSelectionNormal
So if you iterate throught all characters of document and delete the "characters" which are of type 2 when selected. It will do the trick.
因此,如果您遍历文档的所有字符并删除选择时属于类型 2 的“字符”。它会解决问题。
This should not be really speedy, but it work.
这应该不是很快,但它有效。
This example responds to simple cases :
此示例响应简单的情况:
Dim curCharIndex As Integer
Dim charCount As Integer
curCharIndex = 1
charCount = ActiveDocument.Characters.Count
While curCharIndex <= charCount
ActiveDocument.Characters(curCharIndex).Select
If Selection.Type = wdSelectionNormal Then
Selection.Delete
charCount = charCount - 1
Else
'Skip it
curCharIndex = curCharIndex + 1
End If
Wend