0

I'm working on a Powerpoint slide, where I few texts are listed. I have to search for these texts in a Word Document which has a lot of Headings and Texts. After I find the title text, I need to copy the text under the Heading and paste in a new document.

Basically, the VBA coding has to be done in the Powerpoint VBA, with two documents in the background for searching text and pasting it in another.

I've opened the word doc. But searching the text in it and selecting it for copying to another document is what I've not been able to do. Kindly help me.

  • Are you searching for the a part of the text in that chapter or are you searching for the heading directly, i.e. are these "texts" in your ppt equal to the headings or are they part of that chapter in the word doc? – LocEngineer Jul 08 '15 at 12:26
  • The "texts" are parts of the chapter in the word doc. I aim at selecting few lines of text, after our search word is detected. And, copy the selected few lines to a new document. @LocEngineer – mew2sharan Jul 08 '15 at 14:34

1 Answers1

0

I see. The following is not exactly elegant since it uses Selection which I always try to avoid but it is the only way I know to achieve such a thing.

Disclaimer 1: this is made in Word VBA, so you will need a slight adaption, like set a reference to Word, use a wrdApp = New Word.Application object and declare doc and newdoc explicitely as Word.Document.

Disclaimer 2: Since you search for text instead of the respective heading, beware that this will find the first occurence of that text so you better not have the same text in several chapters. ;-)

Disclaimer 3: I cannot paste anymore! :-( My clipboard is set, it pastes elsewhere but I just cannot paste in here. Code follows with first edit, hopefully in a minute...

Edit: yepp, pasting works again. :-)

Sub FindChapter()

Dim doc As Document, newdoc As Document
Dim startrange As Long, endrange As Long
Dim HeadingToFind As String, ChapterToFind As String

ChapterToFind = "zgasfdiukzfdggsdaf" 'just for testing

Set doc = ActiveDocument
Set newdoc = Documents.Add
doc.Activate
Selection.HomeKey unit:=wdStory

With Selection
    With .Find
        .ClearFormatting
        .Text = ChapterToFind
        .MatchWildcards = False
        .MatchCase = True
        .Execute
    End With

    If .Find.Found Then
    '**********
    'Find preceding heading to know where chapter starts
    '**********
        .Collapse wdCollapseStart
        With .Find
            .Text = ""
            .Style = "Heading 1"
            .Forward = False
            .Execute
            If Not .Found Then
                MsgBox "Could not find chapter heading"
                Exit Sub
            End If
        End With

        .MoveDown Count:=1
        .HomeKey unit:=wdLine
        startrange = .Start

        '*********
        'Find next heading to know where chapter ends
        '*********
        .Find.Forward = True
        .Find.Execute
        .Collapse wdCollapseStart
        .MoveUp Count:=1
        .EndKey unit:=wdLine
        endrange = .End

        doc.Range(startrange, endrange).Copy
        newdoc.Content.Paste
        newdoc.SaveAs2 doc.Path & "\" & HeadingToFind & ".docx", wdFormatFlatXML
    Else
        MsgBox "Chapter not found"
    End If

End With


End Sub

Edit: If you need to search for a "feature" that will be in some table in column 1 with the description in column 2 and you need that description in a new doc, try this:

Sub FindFeature()

Dim doc As Document, newdoc As Document
Dim FeatureToFind As String
Dim ro As Long, tbl As Table

FeatureToFind = "zgasfdiukzfdggsdaf"   'just for testing

Set doc = ActiveDocument
Set newdoc = Documents.Add
doc.Activate
Selection.HomeKey unit:=wdStory

With Selection
    With .Find
        .ClearFormatting
        .Text = FeatureToFind
        .MatchWildcards = False
        .MatchCase = True
        .Execute
    End With

    If .Find.Found Then
        Set tbl = Selection.Tables(1)
        ro = Selection.Cells(1).RowIndex
        tbl.Cell(ro, 2).Range.Copy
        newdoc.Range.Paste
    End If
End With


End Sub

Edit: Slight adaptation so you can paste without overwriting existing content in newdoc: Instead of newdoc.Range.Paste just use something along the line of this:

 Dim ran As Range
 Set ran = newdoc.Range
 ran.Start = ran.End
 ran.Paste
LocEngineer
  • 2,847
  • 1
  • 16
  • 28
  • Hi, (@LoCEngineer) Thanks a lot for the help. The code works the way you said. I've been trying to understand how this happens. And now, if I have to make some changes and search only the headings, do I add ".Style = "Heading 1"" within the .Find section? And If I have have search inside tables for a text, and copy the description (which is usually the next column), what do I have to change? How do I directly search and copy from tables inside a word doc? @LocEngineer – mew2sharan Jul 09 '15 at 09:50
  • 1) For searching Headings you must change the code on various parts. You must search for style "Heading 1" (or 2 or whatever you use) but you must also alter the .MoveDown / .MoveUp parts; do you need to search for text or heading now, it makes quite a difference. 2.) For Cells: you need to save a) the table you are in, e.g. `Set tbl = Selection.Tables(1)` as well as your row `Selection.Cells(1).Row` and move from there. Please specify what **exactly** you need. – LocEngineer Jul 09 '15 at 10:33
  • My first requirement was to copy a paragraph. Now, There are two columns inside a table in the word doc. First column has features and the second column has description. I will be have a set of features in a ppt. I have to find the same feature from ppt inside this table in the word doc, and copy the corresponding description to to new document. @LocEngineer. I hope am clear now – mew2sharan Jul 09 '15 at 13:17
  • Thanks for the updated code. I'm planning on using this as a function, so that I can use it whenever i want to look for a text and add it to another document, I can call this. Will that be ok? I think we might get problems in adding the current text to an existing doc with text, when I call the function multiple times and writing into the same doc again and again. Like, we will have to add it at the end of the existing text inside the doc, without erasing the available text. What do you think? @LocEngineer – mew2sharan Jul 10 '15 at 11:54
  • You can do with this whatever you want for whatever you need this. If you want to append. I'll ad a little something to the code above so you don't overwrite existing content when pasting. – LocEngineer Jul 10 '15 at 12:09
  • Yes. I actually want to append the data everytime and Not overwrite them. I will have to collect all the details in a doc – mew2sharan Jul 13 '15 at 08:01
  • Try with the added bit above. If you come across problems, paste the code you have been using and where exactly the problem occurs. – LocEngineer Jul 13 '15 at 08:26