Consulting

Results 1 to 6 of 6

Thread: Selectively extract words from paragraphs and write them below certain paragraph

  1. #1

    Selectively extract words from paragraphs and write them below certain paragraph

    I am a teacher. I have paragraphs in my document. I read them and whenever I find a difficult word. I want to write them below certain paragraphs so that I may write meaning of them for my students. I use the following 2 procedures but there must be a better way. This procedure is forgatable and leading to mistakes over time. The task may be done a better way but I don't know how?. In this Procedure: I have to (1) Know TargetParagraphIndex. (2) Open Vbe and write para no (In example below it is 414) (3) Open word and start task (4)This procedure writes words in Reverse Order I mean what should come first goes to last in example below 'teacher' must be first word.
    There must be a better way to do this task. Thanks in advance.
    MSOffice2007.

    For Example: extracted words from above paragraphs:
    procedure =
    Question =
    paragraphs =
    teacher =

    First: I use the following procedure to know the paragraph in which the difficult words are to be written:
    MsgBox ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count
    Then: I open VBE and write paragraph number in the following procedure. And then start selecting difficult words one by one and run procedure.The Procedure is:
    Sub CopyPasteAtSpecifiedPara()
    'Useful for Difficult Words to copy _
    and write Hindi Meanings.
    'alt w
    
    
    'Copy Word in which the cursor is:
        Set cRng = Selection.Words(1)
        cRng.Copy
        
    'Set Paste Range
        Set Prange = ActiveDocument.Paragraphs(414).Range
        Prange.Collapse wdCollapseStart
        Prange.Paste
        
    'Insert after extracted word space equal space
    Prange.InsertAfter " " & "=" & " "
    
    
    Prange.Collapse wdCollapseStart
    Prange.InsertAfter vbCr
    End Sub
    There must be a better way. Thanks.

  2. #2
    What determines which paragraph the words are inserted after?
    The words are inserted in reverse order because they are always inserted after that paragraph. If you want them in the correct order you need to increment the value of the paragraph number by 1 each time you add a word e.g. as below.
    Have you considered using footnotes or comments?
    Sub CopyPasteAtSpecifiedPara()
    Dim oRng As Range, pRange As Range
    Dim lngPara As Long
    Dim oVar As Variable
        'Useful for Difficult Words to copy _
         and write Hindi Meanings.
        lngPara = 414
        For Each oVar In ActiveDocument.Variables
            If oVar.Name = "Para" Then
                lngPara = oVar.value
                Exit For
            End If
        Next oVar
        Set oRng = Selection.Words(1)
        Set pRange = ActiveDocument.Paragraphs(lngPara).Range
        pRange.Collapse wdCollapseStart
        pRange.Text = oRng.FormattedText & " " & "= " & vbCr
        ActiveDocument.Variables("Para").value = lngPara + 1
        Set oVar = Nothing
        Set oRng = Nothing
        Set pRange = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thank gmayor. It is now writting in correct order.

    My answer to Your Question "What determines which paragraph the words are inserted after?"

    I want it to be inserted after the paragrph text "dwxx"

    Example:
    Dw12
    teacher =
    paragraphs =
    Question =
    whenever =
    students =
    procedure =
    also I want to change the Font Properity after "equal mark" to
    .Name = "Kruti Dev 010
    .size = 12

  4. #4
    The following should do that:
    Sub CopyPasteAtSpecifiedPara()
    Dim oRng As Range, pRange As Range
    Dim lngPara As Long
    Dim oVar As Variable
    Dim oPara As Paragraph
    Dim bFound As Boolean
        For Each oPara In ActiveDocument.Paragraphs
            If LCase(oPara.Range.Words(1)) Like "dw##" Then
                lngPara = ActiveDocument.Range(0, oPara.Range.End).Paragraphs.Count
                bFound = True
                Exit For
            End If
        Next oPara
        If bFound = False Then
            MsgBox "Paragraph not found", vbCritical
            GoTo lbl_Exit
        End If
        'Useful for Difficult Words to copy _
         and write Hindi Meanings.
        For Each oVar In ActiveDocument.Variables
            If oVar.Name = "Para" Then
                lngPara = oVar.value
                Exit For
            End If
        Next oVar
        Set oRng = Selection.Words(1)
        Set pRange = ActiveDocument.Paragraphs(lngPara).Range
        With pRange
            .Collapse wdCollapseStart
            .Text = oRng.Text & " " & "= " & vbCr
            .Start = pRange.End - 2
            .Font.Name = "Kruti Dev 010"
            .Font.Size = 12
        End With
        ActiveDocument.Variables("Para").value = lngPara + 1
    lbl_Exit:
        Set oVar = Nothing
        Set oRng = Nothing
        Set pRange = Nothing
        Set oPara = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    The value of lngpara should change when I go to other paras of my document. But value of lngPara depends on value of oVar.This oVar Variable holds the value even after I close the document. This was the problem even since your first post. Thanks for giving time.

  6. #6
    I changed Line32:
    lngPara = oVar.value
    to
    oVar.value = lngPara
    and this worked
    Now it is working fine. Thanks a lot.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •