PDA

View Full Version : Cannot get text selection macro to work properly



charliew001
06-05-2012, 07:23 PM
I asked a programmer on another forums to help me write a macro that will select all text that begin with "a." and ends the selection at the first paragraph object after "a." He said the macro should output the desired text into the immediate window but it seems like the only thing outputted is the paragraph object. When I told him this, he told me that I was using a list function to notate the different answers of "a."; "b."; "c."; but I was not. I have a word document with 400+ multiple choice test questions and I want the macro to only select all text that begin with "a." so that I may copy and paste it into an excel chart. I have very little knowledge about programming language, so any help is greatly appreciated.

Please let me know if below code needs to be modified or if anything is wrong.


Sub aselect()
'
' aselect Macro
'
'
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

End Sub

fumei
06-06-2012, 12:43 AM
Well there sure is something wrong, but it is not really Skip\s fault. Oh, and BTW, you should stay with a forum that is giving you help - not just jump someplace else.

You need to be precisely clear on what you want.

1. Blah blah question
a. this is the first a answer
b. who cares this is b.
c. and another I do not care about (for now)
d. yadda yadda

2. The second question
a. and the second a. answer
b. the second b whatever
c. falling asleep here
d. ho hum

Now if you execute: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 SubYou get...


this is the first a answer
and the second a. answer

That is, both the text for the a. answers. However, that is not exactly what
you want...now is it? Oh, and BTW, it works only if it is NOT a list...which you say it is not.

S the code above [b[collects[/b] each a. answwer, and strings them together. So it does answer your direct question. Except...you want to bring EACH a. answer into Excel. So you do NOT want them collected together now do you?

Or...do you?

Again though, please do not jump forums when you are working with somepone to get a solution. I am posting as well back into Tek-Tips.

Tinbendr
06-07-2012, 04:11 AM
Can you post a sample?

macropod
06-07-2012, 07:31 PM
Cross-posted at: http://www.tek-tips.com/viewthread.cfm?qid=1684941
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184