VBA Exception? VBA Re-Write?

buggabooed

Board Regular
Joined
Aug 17, 2012
Messages
112
I have a VBA Script in Word that looks for the information I need (a beginning of a sentence), copies the entire sentence until it hits a ".", and then repeats for all instances. I did not create this script as I'm not that awesome ( I had help from here), and it worked great until the vendor sent in wrinkles this time around. What is happening now, is that there are instances mid-sentence of things like "1.5" and such that have a period in the middle, and it is seeing that and ending the sentence right there instead of the actual end of the sentence. Is there a way around this, like an exception, or another way to do this? I'm not real VBA saavy, or really code saavy for that matter. Any help would be appreciated. Thank you. I have the script below.

Sub ExportL2RintoExcel()
'Set reference in Tools >> References to MS Excel
Dim doc As Document
'Both Word and Excel have a "range" object
'but with completely different methods and properties
'so we need to make it obvious which we're working with
Dim WDrngAll As Word.Range
Dim WDrngFind As Word.Range
Dim WDrngText As Word.Range
Dim appXL As Excel.Application
Dim XLwkb As Excel.Workbook
Dim XLwks As Excel.Worksheet
Dim x As Long, y As Long, z As Long
'Initiate objects
Set doc = ActiveDocument
Set WDrngAll = doc.Content
x = 1
z = 0
'First, count all instances of "L2R" in the text
With WDrngAll.Find
Do While .Execute(FindText:="L2R", Forward:=True) = True
z = z + 1
Loop
End With
If MsgBox("I find " & z & " instances of L2R. Does this look right? " & _
"Click Yes to continue; click No to quit.", vbYesNo) = vbYes Then

'All is good, so let's go!
'Dim an array for our info
z = z + 2
ReDim aryExport(z, 2) As String

'In Word VBA, Find searchs a range of text for a string.
'If found, that range shrinks to encompass only that string.
'So we need to set up a series of ranges: the whole doc content,
'a portion of the content to search, and the part to put in the array.
'Could probably be a bit leaner, but this keeps me away from mistakes!

'Since we searched this and found text, we don't know what it encompasses,
'so we'll reset it
Set WDrngAll = doc.Content
'Our search range is going to be reset within our loop to stretch from
'the end of the Text range to the end of the All range. This way, the
'range keeps shrinking to encompass only from the end of where we just
'came from to the end of the document. To start, then, we need a Text
'range that is just one single point at the top of the doc.
Set WDrngText = WDrngAll.Duplicate
WDrngText.Collapse wdCollapseStart

'Start the search loop
Do
'Initiate Find range
Set WDrngFind = doc.Range(WDrngText.End, WDrngAll.End)
'Find text; TRUE if found
If WDrngFind.Find.Execute(FindText:="L2R", Forward:=True) = True Then
'Found one; WDrngFind now contains only the first found "L2R"
'so we need to stretch the range to encompass the whole word
WDrngFind.MoveEnd wdWord, 1
'Load that into the array
aryExport(x, 1) = Trim(WDrngFind.Text)
'Get the text following
'(I could just keep resetting WDrngFind, but it's too prone to
'human error that I can avoid by using another range object)
Set WDrngText = doc.Range(WDrngFind.End, WDrngFind.End)
'WDrngText is now a single point at the end of the found L2R word
'Move the end forward until we see a period
WDrngText.MoveEndUntil "."
WDrngText.MoveEnd wdCharacter, 1
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)

x = x + 1

'Done here; the Find range resets at the top of the loop
Else
'L2R not found; end loop
Exit Do
End If
Loop

'At this point, we should have an array full of text elements.
'In the first positions are the L2R words. In the second positions
'are all the text blocks following those words.
'Now we write into Excel.

'Check for open Excel instance; if none, create one
On Error Resume Next
Set appXL = GetObject(, "Excel.Application")
On Error GoTo 0
If appXL Is Nothing Then _
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
'Create workbook; get first sheet
Set XLwkb = appXL.Workbooks.Add
Set XLwks = XLwkb.Worksheets("Sheet1")

'Stop display updating for faster writing
appXL.ScreenUpdating = False

'Write array elements
For y = 1 To x
XLwks.Cells(y + 1, 1) = aryExport(y, 1)
XLwks.Cells(y + 1, 2) = aryExport(y, 2)
Next y

'Reset display
appXL.ScreenUpdating = True

End If
MsgBox "Done!"
End Sub
 
Not that I remember Ed. May have been because we are putting it in Excel, and it was capturing each requirement all the way through, then pasting it all in Excel. But if not that, I don't think so.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this --

Find this section:
Code:
Set WDrngText = doc.Range(WDrngFind.End, WDrngFind.End)
'WDrngText is now a single point at the end of the found L2R word
'Move the end forward until we see a period
WDrngText.MoveEndUntil "."
WDrngText.MoveEnd wdCharacter, 1
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)

and replace it with this:
Code:
'''Set WDrngText = doc.Range(WDrngFind.End, WDrngFind.End)
''''WDrngText is now a single point at the end of the found L2R word
''''Move the end forward until we see a period
'''WDrngText.MoveEndUntil "."
'''WDrngText.MoveEnd wdCharacter, 1
''''Put this text into the array
'''aryExport(x, 2) = Trim(WDrngText.Text)
'***************************
'This section changed 1/29/13
'
Set WDrngText = doc.Range(WDrngFind.End, WDrngAll.End)
'WDrngText stretches from the end of the L2R word to the end of the doc
'Now we find the next L2R word
If WDrngText.Find.Execute(FindText:="L2R", Forward:=True) = True Then
'Found the next L2R
'Reset WDrngText to encompass text
WDrngText.SetRange Start:=WDrngFind.End, End:=WDrngText.Start
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)
Else
'No other L2R; go to end of doc
WDrngText.SetRange Start:=WDrngFind.End, End:=WDrngAll.End
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)
End If
'
'***************************

(Yes, the new code does contain the old section, but commented out. Just make sure the old section is deleted and replaced by the new section.)

This should grab the text between the end of one L2RXXX and the beginning of the next one. If it can't find a next one, it grabs the text from the end of L2RXXX and the end of the document.

Eh, hopefully it does ....
Ed
 
Upvote 0
Hi Ed. Thank you very much for this. I ran it, but it blew up at:

Set WDrngText = doc.Range(WDrngFind.End, WDrngAll.End)

I'm sure I did something wrong but am not seeing it. It gave a run time error 91; object variable or with block variable not set.
 
Upvote 0
Here is the new code -

Sub ExportL2RintoExcel()
'Set reference in Tools >> References to MS Excel
Dim doc As Document
'Both Word and Excel have a "range" object
'but with completely different methods and properties
'so we need to make it obvious which we're working with
Dim WDrngAll As Word.Range
Dim WDrngFind As Word.Range
Dim WDrngText As Word.Range
Dim appXL As Excel.Application
Dim XLwkb As Excel.Workbook
Dim XLwks As Excel.Worksheet
Dim x As Long, y As Long, z As Long
'Initiate objects
Set doc = ActiveDocument
Set WDrngAll = doc.Content
x = 1
z = 0
'First, count all instances of "L2R" in the text
With WDrngAll.Find
Do While .Execute(FindText:="L2R", Forward:=True) = True
z = z + 1
Loop
End With
If MsgBox("I find " & z & " instances of L2R. Does this look right? " & _
"Click Yes to continue; click No to quit.", vbYesNo) = vbYes Then

'All is good, so let's go!
'Dim an array for our info
z = z + 2
ReDim aryExport(z, 2) As String

'In Word VBA, Find searchs a range of text for a string.
'If found, that range shrinks to encompass only that string.
'So we need to set up a series of ranges: the whole doc content,
'a portion of the content to search, and the part to put in the array.

'Since we searched this and found text, we don't know what it encompasses,
'so we'll reset it
Set WDrngAll = doc.Content
'Our search range is going to be reset within our loop to stretch from
'the end of the Text range to the end of the All range. This way, the
'range keeps shrinking to encompass only from the end of where we just
'came from to the end of the document. To start, then, we need a Text
'range that is just one single point at the top of the doc.
Set WDrngText = WDrngAll.Duplicate
WDrngText.Collapse wdCollapseStart

'Start the search loop
Do
'''Set WDrngText = doc.Range(WDrngFind.End, WDrngFind.End)
''''WDrngText is now a single point at the end of the found L2R word
''''Move the end forward until we see a period
'''WDrngText.MoveEndUntil "."
'''WDrngText.MoveEnd wdCharacter, 1
''''Put this text into the array
'''aryExport(x, 2) = Trim(WDrngText.Text)
'***************************
'This section changed 1/29/13
'
Set WDrngText = doc.Range(WDrngFind.End, WDrngAll.End)
'WDrngText stretches from the end of the L2R word to the end of the doc
'Now we find the next L2R word
If WDrngText.Find.Execute(FindText:="L2R", Forward:=True) = True Then
'Found the next L2R
'Reset WDrngText to encompass text
WDrngText.SetRange Start:=WDrngFind.End, End:=WDrngText.Start
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)
Else
'No other L2R; go to end of doc
WDrngText.SetRange Start:=WDrngFind.End, End:=WDrngAll.End
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)
End If
'
'***************************
Loop

'At this point, we should have an array full of text elements.
'In the first positions are the L2R words. In the second positions
'are all the text blocks following those words.
'Now we write into Excel.

'Check for open Excel instance; if none, create one
On Error Resume Next
Set appXL = GetObject(, "Excel.Application")
On Error GoTo 0
If appXL Is Nothing Then _
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
'Create workbook; get first sheet
Set XLwkb = appXL.Workbooks.Add
Set XLwks = XLwkb.Worksheets("Sheet1")

'Stop display updating for faster writing
appXL.ScreenUpdating = False

'Write array elements
For y = 1 To x
XLwks.Cells(y + 1, 1) = aryExport(y, 1)
XLwks.Cells(y + 1, 2) = aryExport(y, 2)
Next y

'Reset display
appXL.ScreenUpdating = True

End If
MsgBox "Done!"
End Sub
 
Upvote 0
The thing that is worrying me is that it is going L2R to L2R, but at times there is junk in-between the next set of L2R's, that I lovingly referred to as junk in a previous post. Will that capture that text too?
 
Upvote 0
The thing that is worrying me is that it is going L2R to L2R, but at times there is junk in-between the next set of L2R's, that I lovingly referred to as junk in a previous post. Will that capture that text too?

Yes, it will. And unless you can find some way to positively identify the "junk, we may not be able to get rid of it.

it blew up at:

Set a breakpoint at that line, or insert Stop (on a line all by itself) just before it.
Then open View >> Locals Window and look at WDrngFind and WDrngAll.
Are they populated? Are they valid? Is one no longer set?

Ed
 
Upvote 0
Hmmm, well hopefully that junk can be worked around.

Module1 has a blank under value and is a type of Module1/Module1.
doc has a blank under value and is a type of Document/Document.
WDrngAll has a blank under value and is a type of range/range.
WDrngFind has a value of "nothing" and is a type of range.
WDrngText has is blank under value and is a type of range/range.
appXL has a value of "nothing" and is a type of application.
XLwkb has a value of "nothing" and is a type of workbook.
XLwks has a value of "nothing" and is a type of worksheet.
x has a value of 1 and is a type of long.
y has a value of 0 and is a type of long.
z has a value of 2663 and is a type of long.
aryExport has a blank under value and is a type of String (0 to 2663, 0 to 2).


Let me know if that helps or you need more.
 
Upvote 0
By the way, I just want to say thank you for the continued efforts Ed. I really appreciate everything you are doing to help me so very much. I couldn't be doing this without you.
 
Upvote 0
The new code got put in the wrong place.
Try this:

Code:
Sub ExportL2RintoExcel()
'Set reference in Tools >> References to MS Excel
Dim doc As Document
'Both Word and Excel have a "range" object
'but with completely different methods and properties
'so we need to make it obvious which we're working with
Dim WDrngAll As Word.Range
Dim WDrngFind As Word.Range
Dim WDrngText As Word.Range
Dim appXL As Excel.Application
Dim XLwkb As Excel.Workbook
Dim XLwks As Excel.Worksheet
Dim x As Long, y As Long, z As Long
'Initiate objects
Set doc = ActiveDocument
Set WDrngAll = doc.Content
x = 1
z = 0
'First, count all instances of "L2R" in the text
With WDrngAll.Find
Do While .Execute(FindText:="L2R", Forward:=True) = True
z = z + 1
Loop
End With
If MsgBox("I find " & z & " instances of L2R. Does this look right? " & _
"Click Yes to continue; click No to quit.", vbYesNo) = vbYes Then
'All is good, so let's go!
'Dim an array for our info
z = z + 2
ReDim aryExport(z, 2) As String
'In Word VBA, Find searchs a range of text for a string.
'If found, that range shrinks to encompass only that string.
'So we need to set up a series of ranges: the whole doc content,
'a portion of the content to search, and the part to put in the array.
'Could probably be a bit leaner, but this keeps me away from mistakes!
'Since we searched this and found text, we don't know what it encompasses,
'so we'll reset it
Set WDrngAll = doc.Content
'Our search range is going to be reset within our loop to stretch from
'the end of the Text range to the end of the All range. This way, the
'range keeps shrinking to encompass only from the end of where we just
'came from to the end of the document. To start, then, we need a Text
'range that is just one single point at the top of the doc.
Set WDrngText = WDrngAll.Duplicate
WDrngText.Collapse wdCollapseStart
'Start the search loop
Do
'Initiate Find range
Set WDrngFind = doc.Range(WDrngText.End, WDrngAll.End)
'Find text; TRUE if found
If WDrngFind.Find.Execute(FindText:="L2R", Forward:=True) = True Then
'Found one; WDrngFind now contains only the first found "L2R"
'so we need to stretch the range to encompass the whole word
WDrngFind.MoveEnd wdWord, 1
'Load that into the array
aryExport(x, 1) = Trim(WDrngFind.Text)
'Get the text following
'(I could just keep resetting WDrngFind, but it's too prone to
'human error that I can avoid by using another range object)
'
'''Set WDrngText = doc.Range(WDrngFind.End, WDrngFind.End)
''''WDrngText is now a single point at the end of the found L2R word
''''Move the end forward until we see a period
'''WDrngText.MoveEndUntil "."
'''WDrngText.MoveEnd wdCharacter, 1
''''Put this text into the array
'''aryExport(x, 2) = Trim(WDrngText.Text)
'***************************
'This section changed 1/29/13
'
Set WDrngText = doc.Range(WDrngFind.End, WDrngAll.End)
'WDrngText stretches from the end of the L2R word to the end of the doc
'Now we find the next L2R word
If WDrngText.Find.Execute(FindText:="L2R", Forward:=True) = True Then
'Found the next L2R
'Reset WDrngText to encompass text
WDrngText.SetRange Start:=WDrngFind.End, End:=WDrngText.Start
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)
Else
'No other L2R; go to end of doc
WDrngText.SetRange Start:=WDrngFind.End, End:=WDrngAll.End
'Put this text into the array
aryExport(x, 2) = Trim(WDrngText.Text)
End If
'
'***************************
'
x = x + 1
'Done here; the Find range resets at the top of the loop
Else
'L2R not found; end loop
Exit Do
End If
Loop
'At this point, we should have an array full of text elements.
'In the first positions are the L2R words. In the second positions
'are all the text blocks following those words.
'Now we write into Excel.
'Check for open Excel instance; if none, create one
On Error Resume Next
Set appXL = GetObject(, "Excel.Application")
On Error GoTo 0
If appXL Is Nothing Then _
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
'Create workbook; get first sheet
Set XLwkb = appXL.Workbooks.Add
Set XLwks = XLwkb.Worksheets("Sheet1")
'Stop display updating for faster writing
appXL.ScreenUpdating = False
'Write array elements
For y = 1 To x
XLwks.Cells(y + 1, 1) = aryExport(y, 1)
XLwks.Cells(y + 1, 2) = aryExport(y, 2)
Next y
'Reset display
appXL.ScreenUpdating = True
End If
MsgBox "Done!"
End Sub

Ed
 
Upvote 0
I got a value out of range error. See below. I copied and pasted your window right over everything. I surely hope I did not do something wrong again.

Module1 has a blank under value and is a type of Module1/Module1.
doc has a blank under value and is a type of Document/Document.
WDrngAll has a blank under value and is a type of range/range.
WDrngFind has a blank under value and is a type of range.
WDrngText has is blank under value and is a type of range/range.
appXL has a value of "nothing" and is a type of application.
XLwkb has a value of "nothing" and is a type of workbook.
XLwks has a value of "nothing" and is a type of worksheet.
x has a value of 2662 and is a type of long.
y has a value of 0 and is a type of long.
z has a value of 2663 and is a type of long.
aryExport has a blank under value and is a type of String (0 to 2663, 0 to 2).


Let me know if that helps or you need more.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,667
Members
449,462
Latest member
Chislobog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top