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
 
Can't you create a sample document with dummy data?
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
A sample would be:

"L2Rxxx The cow will jump over the fence 1.5 times, yet it should really jump the full 2 times."

Then repeating many different ways 2000 times, with bunches and bunches of data within it that is not needed.
 
Upvote 0
Something strange has happened to the thread.

I can't get past the first page of posts unless I use Go Advance, as I'm doing now.

I can now see the 2nd page after posting this. Weird.
 
Upvote 0
Instead of finding . to get the end of the sentence try this.
Code:
 WDrngText.MoveEnd wdSentence

 'Put this text into the array
 aryExport(x, 2) = Trim(WDrngText.Text)
 
Upvote 0
I seem to reccall there was a reason we couldn't just .MoveEnd wdSentence, but I don't for the life of me remember why. If that is the case, then about the only hope there is (or what i would do, anyway), is:
-- extend to the period
-- extend one more character
-- test the last character

Here it gets dicey, depending on the actual formatting of the text as it's being worked on by the macro. We could say:
"if the character immediately after a period is a number, then
go one more;
if that's a space, then
continue to the next period."

But will that cover all of the potential bases?
-- Would the text ever break after the period but before the next number?
-- Would you ever have a line or paragraph break after the second number?
-- Will you ever enounter "1.2.3.4"?

If you could spend just a bit of time dummying up a few paragraphs so it's no longer proprietary and post that, we could do a lot more to help.

Ed
 
Upvote 0
Ed

I had a feeling wdSentence might no be the solution - way too easy.

I was also thinking about checking the next character, but checking for a space and taking that as indicating the end of the sentence has been reached.
 
Upvote 0
Thank you for weighing in guys. Ed you are right, it will not cover all the potential bases. There are plenty of scenarios where now there is a 1.2.3.4, or a (e.g. words), or even an (etc.). Please see the several examples below and let me know if that helps. This was not a problem the last two times, but I guess the company decided to "improve" things.

L2R113 When getting on-board a plane, and the leading edge of the staircase is in front of you mapped out, it shall display the notation of where the staircase begins (e.g. carpet, railing, etc.) in blue text on the left of the screen.

L2R1123 When the plane has taken off, there shall be knots labeled on the screen white, on the upper right of the screen.

L2R5180 When a non-enforceable (i.e. "warning") warning banner is displayed, you will hear an alert yo.

L2R1210 When a key or an extension time key (30 minutes, 45 minutes, 1 hours, 1.5 hours and 2 hours) is pressed, then things will happen that make the back office system do its thing.

An example of how it really looks when it comes in below.


  1. L2R28 </SPAN>When </SPAN></SPAN></SPAN>the on-board segment is HAPPY and</SPAN> </SPAN></SPAN>

  • the on-board segment has located the leading edge of stuff where the track data indicates there are things and</SPAN></SPAN></SPAN>
  • the on-board segment is not receiving a valid status of "you da man" and</SPAN></SPAN></SPAN>
  • the on-board segment is not sad, </SPAN></SPAN></SPAN>
the on-board segment shall set the on-board segment state to DEPRESSED.</SPAN> </SPAN></SPAN>
 
Upvote 0
Was there a reason we couldn't jump from L2R113 to L2R1123, and then back up to the period?

Ed
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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