View Full Version : What continuous section contains my keyword?
agent86
12-02-2013, 08:45 AM
Word 2010
I have inserted paragraphs into a Word document. I enclosed it in sections. I now want to find the section index that contains the keyword and delete the section and its contents. The keyword will only appear in one section. Is there sample code that show how to do it? Thanks
macropod
12-02-2013, 04:38 PM
Cross-posted at: http://www.msofficeforums.com/word-vba/18837-find-keyword-section-delete-section.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
agent86
12-03-2013, 10:12 AM
This is what I did to remove the sections and text I inserted into the document.
strSearchString = "Inst#: " & ReportInstance
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearchString
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
' what section am I in?
If .Found = True Then
intSectionCount = ActiveDocument.Range(0, Selection.Range.End).Sections.Count
ActiveDocument.Sections(intSectionCount).Range.Delete
ActiveDocument.Sections(intSectionCount - 1).Range.Delete
End If
End With
agent86
12-03-2013, 11:12 PM
Correction...
You should not do this line
ActiveDocument.Sections(intSectionCount - 1).Range.Delete
It will delete the paragraph above the insertion.
macropod
12-03-2013, 11:55 PM
Simpler and more efficient:
strSearchString = "Inst#: " & ReportInstance
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearchString
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
If .Find.Found = True Then
.End = .Duplicate.Sections(1).Range.End
.Start = .Duplicate.Sections(1).Range.Start - 1
.Duplicate.Text = vbNullString
End If
End With
agent86
12-04-2013, 09:12 AM
That works fantastic and I can remove the second section with losing the existing data. Still testing to do but Thank you, Thank you, Thank you. I will also study to see exactly what is going on. I need to learn why mine goofs up an existing paragraph and yours does not.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.