简体   繁体   中英

How can I delete all text in a Word document using a macro?

I want to delete all the text in a Word document leaving the images/embedded files .

I know this should be easy, but can't a good example on the web.

Text is of Type 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM