Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a range text selection macro

Status
Not open for further replies.

charliew001

Technical User
Jun 4, 2012
8
US
Hi,

First time poster and have very limited programming knowledge. I need help with the below macro to modify it to begin the range with a start word "a." ; I have a lot of multiple choice test questions I need to put into an excel chart and I want to make the formatting easier. Basically, I want the macro to be able to select all text that begins with "a." and ends the selection at the end of the paragraph.

Below macro I was trying to modify.

Sub SelectRange()
Dim rngParagraphs As Range
Set rngParagraphs = ActiveDocument.Range( _
Start:=ActiveDocument.Paragraphs(1).Range.Start, _
End:=ActiveDocument.Paragraphs(4).Range.End)
rngParagraphs.Select
End Sub

thank you all in advance.
 


Hi,

Excel does not have Paragraph objects, as does Word. Excel is a spreadsheet application, not a text application like Word.

Also, Excel Charts are primarily numeric summary reports, where the display has positioning that is relative to some numeric value(s)

Please explain in more detail WHAT you are trying to do, rather than HOW you think it ought to be done.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thank you for your reply.

I mistakenly forgot to add that all the text I am trying to select is currently in a microsoft word document. I want to select the paragraphs that start with "a." and ends at the paragraph object at the end of paragraph of which "a." started and paste that text into an excel chart I've already created. My text document will have 100 + paragraphs of which "a." starts.
 


I'd loop thru the Paragraphs collection, something like...
Code:
    Dim pgh As Paragraph
    
    For Each pgh In ThisDocument.Paragraphs
      With pgh
        If Left(.Range.Text, 2) = "a." And Left(Right(.Range.Text, 3), 2) = "a." Then
            Debug.Print .Range.Text
        End If
      End With
    Next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sorry when I said I have limited programming knowledge, I really mean I have none. I pasted only your code into the VBA developer but nothing happend when I ran the macro. Please advise. Thanks

Code:
Sub Aselection()
'
' Aselection Macro
'

Dim pgh As Paragraph
For Each pgh In ThisDocument.Paragraphs
With pgh
If Left(.Range.Text, 2) = "a." And Left(Right(.Range.Text, 3), 2) = "a." Then
Debug.Print .Range.Text
End If
End With
Next

End Sub
 


Debug.Print .Range.Text

puts data in the IMMEDIATE WINDOW in the VB Editor -- View Menu

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 

You can replace:
[tt]
Debug.Print .Range.Text
[/tt]
with
[tt]
MsgBox .Range.Text
[/tt]
to see the text in a message box.

Have fun.

---- Andy
 


I think that you want to strip the "a." from the paragraph text, like this maybe...
Code:
Sub ParseParagraphs()
    Dim pgh As Paragraph, a, i As Integer, sPgh As String
    
    For Each pgh In ThisDocument.Paragraphs
      With pgh
        sPgh = ""
        If Left(.Range.Text, 2) = "a." And Left(Right(.Range.Text, 3), 2) = "a." Then
            
            a = Split(.Range.Text, "a.")
            
            For i = 1 To UBound(a) - 1
                sPgh = sPgh & a(i) & "a."
            Next
        
        End If
      End With
      
      If sPgh <> "" Then _
          sPgh = Left(sPgh, Len(sPgh) - 2)
      Debug.Print sPgh
    Next
End Sub
This code would account for a paragraph like this.
[tt]
First
a.second a.
third
a.fourth a.
a.Now is the time for all good men to come to the aid of their friends in Peoria.a.
[/tt]
and output
[tt]
second

fourth
Now is the time for all good men to come to the aid of their friends in Peoria.

[/tt]

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sorry I am still not getting this to work. Undoubtedly due to my lack of knowledge in this area. I have attached a copy of some of my work of which I am trying to edit.

1. Regarding hepatitis B transmission, which of the following is not true?

a. When fulminant hepatitis B occurs, the mortality rate is approximately 60%.
b. Hepatitis B may be transmitted by blood or blood products.
c. The virus may be found in semen and vaginal secretions.
d. The incubation period is typically four to six weeks.

Correct Answer. The incubation period for hepatitis B is actually four to 20 weeks. The mortality of fulminant hepatitis B is approximately 60%. The virus may be transmitted by blood or blood products and found in semen and vaginal secretions.

I have a word document with about 400 of these questions. I want a macro that will select all the text that begins with "a." and ends the selection where it says after "60%." I assume this would be the paragraph object at the end of the sentence. I then need to transfer all the "a." answers onto an excel sheet which has a column designated for A answers. However, I only want the macro to select all the text in the document that begins with a. Hopefully the macro will be easy enough for me to modify so I can later do this with "b."; "c."; and so on.
Also for the immediate window, if I am to run the previous macros you had, am I suppose to paste all the text I have into that immediate window for the macro to work?

Thank you again for your help.
 


I missunderstood your initial requirement statement.
Code:
    Dim pgh As Paragraph, a, i As Integer, sPgh As String
    
    For Each pgh In ThisDocument.Paragraphs
      With pgh
        
        sPgh = ""
        
        If Left(.Range.Text, 2) = "a." Then
            
            a = Split(.Range.Text, "a.")
            
            For i = 1 To UBound(a) - 1
                sPgh = sPgh & a(i) & "a."
            Next
        
        End If
      End With
      
      If sPgh <> "" Then _
          sPgh = Left(sPgh, Len(sPgh) - 2)
      Debug.Print sPgh
    Next
Your input in Word...
[tt]
1. Regarding hepatitis B transmission, which of the following is not true?

a. When fulminant hepatitis B occurs, the mortality rate is approximately 60%.
b. Hepatitis B may be transmitted by blood or blood products.
c. The virus may be found in semen and vaginal secretions.
d. The incubation period is typically four to six weeks.
[/tt]
The OUTPUT in IMMEDIATE WINDOW
[tt]
When fulminant hepatitis B occurs, the mortality rate is approximately 60%.
[/tt]

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi skip,

Thank you for all your help again with this. You will be saving my wrists from carpal tunnel. One more thing though, I have the code inputted into microsoft VBA, but the immediate window doesn't show any output. I've attached a screenshot, hopefully this will let you see what is wrong.


Thanks

 
 http://postimage.org/image/uv8gp918n/

There is no MICROSOFT VBA.

There is your Word (alt+F11 toggles between application & VB Editor) VB Editor.

In the VB Project Explorer (ctr+R) you should have OBJECTS like Normal & Project(Document1). Your code should be in an object in Document1, or whatever your Word document is SAVED as.

Do a Debug > Compile Project first.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Im running the macro as you said and it seems to only be outputting the paragraph object and no text.
 

You must be using a MULTI-LEVEL LIST?

There ain't no "a." in a Word List!!!!!!!!!!

You ain't got no paragraph beginning with "a."!!!!!!!

Ain't, ain't, ain't!!!!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Is it ANY kind of LIST feature?

If so, NADA!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I am not sure why Skip is posting code for that array like that.

The OP has posted a slightly different question at VBA Express. He has taken the point at this one, and is btrying to get it to work. However, I am pointing him back here.
Code:
Sub aselect()
    Dim pgh As Paragraph, a, i As Integer, sPgh As String
    For Each pgh In ThisDocument.Paragraphs
        With pgh
            If Left(.Range.Text, 2) = "a." Then
                a = Split(.Range.Text, "a.")
                sPgh = sPgh & a(1) & vbCrLf
'                Next
            End If
        End With
    Next
    MsgBox sPgh
End Sub
This collects all a. answers ninto ONE string. This is - I suspect - what the OP wishes to achieve.

Gerry
 


I assumed that each qualifying paragraph was handled separately in some sort of Excel chart (that I don't understand), rather than ALL the qualifying paragraphs being treated as one.

Secondly, if the OP is using ANY Word List feature, single or multi-level, the a., b., c. etc is not part of the text of ANY paragraph. If that is so, I am at a loss!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Yes, well we do not really know what the OP is doing...so...

Gerry
 
Although Skip, I do have to say that:
Code:
   a = Split(.Range.Text, "a.")  
' if the paragraph is a. blah blah, then
' the Split returns
a(0) = a.
a(1) = blah blah
' and therefore.......  
   For i = 1 To UBound(a) - 1
may need a Step -1

However, the most important reason the OP is getting nothing is...
Code:
For Each pgh
  With pgh    
        sPgh = ""
which means that the string sPgh is made "" evry time! So sure the a. puts a value in, but b., c. d. empties it,

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top