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

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

I know this should be easy, but it cannot be a good example on the Internet.

+4
source share
1 answer

The text is of type wdSelectionNormal

So, if you iterate over all the characters in a document and delete the “characters” that are of type 2 when selected. This will do the trick.

It doesn't have to be really fast, but it works.

This example answers 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 
+2
source

Source: https://habr.com/ru/post/1390267/


All Articles