PDA

View Full Version : [SOLVED:] Selectively extract words from paragraphs and write them below certain paragraph



anilsharaf
11-04-2021, 07:59 PM
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.

gmayor
11-04-2021, 10:18 PM
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

anilsharaf
11-04-2021, 11:38 PM
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

gmayor
11-05-2021, 12:34 AM
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

anilsharaf
11-05-2021, 07:05 AM
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.

anilsharaf
11-05-2021, 07:41 AM
I changed Line32:

lngPara = oVar.value
to

oVar.value = lngPara
and this worked
Now it is working fine. Thanks a lot.