RagingRhino
RagingRhino

Reputation: 13

VBA: Replace text based on formatting

I have a table in a Word file A which contains a bunch of different Contents. Which I just copy using VBA into another Word or PowerPoint file B. So far that is not a problem.

However, since file A is a working sheet, people sometimes cross stuff out, which means: it should be removed, but for the record it stays in there first. In the final version it shouldnt be displayed, so in the process of copying everything in a different file, the crossed out text should be removed.

To break it down to the technical stuff: I want to select text in a Word document, and then remove all text that has a certain formatting.

Maybe there is a special selection possibility or a way to iterate through all characters and test for formatting.

Upvotes: 1

Views: 1959

Answers (2)

user1641172
user1641172

Reputation:

The best way to do this without suffering severe performance iterating characters or paragraphs in vba is to use find and replace.

You can do this in vba as follows, note I have wrapped all the actions in a custom undo record, then you can call your current vba routine with CopyDocumentToPowerPoint and the word document will be restored to the state it was before the macro ran (crossed out text remains in word, but is not pasted to powerpoint).

'wrap everything you do in an undo record
Application.UndoRecord.StartCustomRecord "Move to powerpoint"

With ActiveDocument.Range.Find
    .ClearFormatting
    .Font.StrikeThrough = True
    .Text = ""
    .Replacement.Text = ""
    .Execute Replace:=wdReplaceAll
End With

'copy to powerpoint and whatever else you want
CopyDocumentToPowerPoint

Application.UndoRecord.EndCustomRecord

'and put the document back to where you started
ActiveDocument.Undo

Upvotes: 1

ZygD
ZygD

Reputation: 24356

It is possible to go character-by-character and remove those which have the strikethrough font enabled on them (the ones which are crossed out) in MS Word. However, as far as I know, there is no such possibility to detect a strike-through font in MS PowerPoint.

If you just need to delete the text which has the strikethrough font on it in the selected text only, you can use this Word macro:

Sub RemoveStrikethroughFromSelection()
    Dim char As Range
    For Each char In Selection.Characters
        If char.Font.StrikeThrough = -1 Then
            char.Delete
        End If
    Next
End Sub

If more integrated to copying a Word table to another Word document and PowerPoint presentation, the following code might be useful. It first pastes the table to a new Word file, then removes unnecessary characters, and after that pastes this new table to PowerPoint.

Sub CopyWithoutCrossedOutText()
    Dim DocApp As Object: Set DocApp = CreateObject("Word.Application")
    Dim PptApp As Object: Set PptApp = CreateObject("PowerPoint.Application")
    Dim Doc As Object: Set Doc = DocApp.Documents.Add
    Dim Ppt As Object: Set Ppt = PptApp.Presentations.Add
    Dim c As Cell
    Dim char As Range
    DocApp.Visible = True
    PptApp.Visible = True

    'Copying Word table to the 2nd Word document
    ThisDocument.Tables(1).Range.Copy
    Doc.ActiveWindow.Selection.Paste

    'In the 2nd Word document - removing characters having strikethrough font enabled on them
    For Each c In Doc.Tables(Doc.Tables.Count).Range.Cells
        For Each char In c.Range.Characters
            If char.Font.StrikeThrough = -1 Then
                char.Delete
            End If
        Next
    Next

    'Copying the table from the 2nd Word document to the PowerPoint presentation
    Doc.Tables(1).Range.Copy
    Ppt.Slides.Add(1, 32).Shapes.Paste

End Sub

Upvotes: 0

Related Questions