PDA

View Full Version : Setrange somehow



Dave
03-26-2012, 11:32 PM
I unsuccessfully messed around with Word from XL to help this guy here...
http://www.mrexcel.com/forum/showthread.php?t=622143
I thought that eventually I'd stumble across some syntax to set a range between a repetitive "find" (ie. "Item No." in this .doc started each "chunk" of information). But, no luck. So, how to set a range between repeating text in a .doc would be my inquiry? The resulting "chunk" of data could then be manipulated for it's contents... or perhaps there's an easier way/tag to someone that is more able to help this guy. Dave

fumei
03-27-2012, 12:07 AM
Good heavens! That is crazy, and frankly I am not going to touch it. Look, there is a huge problem with just this...

There is no D Column (from the image of the Word doc.

It would be bad enough if the listing to go into D (in Excel) were not jumping around, BUT, a portion of one paragraph is from TWO separate columns. In fact the column data in Word is a logical mess.

Column A: Item No.; Quantity; Description; Manufacturer; Model; Dimensions; Specifications

Column B: bunch of item.

In your Excel example, Item No, Manufacturer, Model are top level names for columns. Dimensions and Model are not. Worse the TEXT "Dimensions" seemed to be needed to be included in the column D in Excel.

Trying to set up bookmarks for all of these is crazy, combining some headings and not others would take fussy fussy picky coding. Can it be done? Probably, but it sure is not worth my effort to do it. I hope someone else wants to take it on.

A glowing example of how to do bad design. Do you REALLY need ....gee, I just noticed that Model OEB 20 10 shows up in two places.

Nope. I do not have the time and patience to help much. Is the content in Word in a table?

Paul_Hossler
03-27-2012, 10:31 AM
The only thing that I could suggest you do is to save the word doc as a txt file, and then use Excel to read that TXT file, parsing as best you can to populate the Excel worksheet, possible by key words (Item No., Description, etc.)

Paul

fumei
03-27-2012, 04:17 PM
Yes, and then keep it as an ongoing Excel file.

defcon_3
03-27-2012, 06:01 PM
I agree save as text and export to excel. If you want to more accurate put delimiters on each entity eg. Manufacturer: So that excel will easily recognize each entity.

fumei
03-27-2012, 10:41 PM
Still have the issue of multiple placement of data - e.g. Model number shows in TWO places.

defcon_3
03-27-2012, 11:44 PM
@Dave
Was that manually inputted or it was generated from somewhere or some tools?

fumei
03-27-2012, 11:57 PM
BTW: the question was not answered.

Is the content in Word in a table?

Paul_Hossler
03-28-2012, 04:32 AM
Still have the issue of multiple placement of data - e.g. Model number shows in TWO places.

Yes, it'd be purely ugly.

You'd have to parse based on a block of lines making up one logical 'entry'.

Paul

Dave
03-28-2012, 08:13 AM
Thanks all for your input. I will provide a link at Mr. Excel. The multipage .doc has no tables, line or paragraph consistancy. It's only structure is that it contains repetitive chunks of information. It looks like the info was manually enterred. The expected output is jumbled and the task, I agree, would be "purely ugly"... and challenging is why I gave it a go. Anyways, my original inquiry about setting a range between repeating text is still unresolved. Is this possible? Dave

Tinbendr
04-04-2012, 06:52 AM
This is still pretty rough, but I think I answer your question.

There are problems with your source, too. Some of the sentences have soft returns instead of hard returns, so several lines get lumped together.

Sub ParseDataFile()
Dim aDoc As Document
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim RngStart As Long
Dim RngEnd As Long

Set aDoc = ActiveDocument
Set Rng1 = aDoc.Range
Set Rng2 = Rng1.Duplicate
'Esthetics
Selection.Collapse wdCollapseStart

'Create new doc for demo.
Set NewDoc = Documents.Add

RngStart = 0
Do
With Rng1.Find
.Text = "Item No."
.Wrap = wdFindStop
.Execute
End With
If Rng1.Find.Found Then
'set variable for later use.
RngEnd = Rng1.End + 1

'Define second range to search.
Set Rng2 = aDoc.Range(Rng1.End, aDoc.Range.End)

'Search for second 'Item No.'
With Rng2.Find
.Text = "Item No."
.Wrap = wdFindStop
.Execute
End With
If Rng2.Find.Found Then
'Second item found.
'Parse range.

'Item No.
ItemNo = FindIt(aDoc, Rng1.Start, Rng2.Start, "Item No.")

'Quantity
Quantity = FindIt(aDoc, Rng1.Start, Rng2.Start, "Quantity")

'Description
Desc = FindIt(aDoc, Rng1.Start, Rng2.Start, "Description")

'Manufacturer
Mfg = FindIt(aDoc, Rng1.Start, Rng2.Start, "Manufacturer")

'Specs
Specs = FindIt(aDoc, Rng1.Start, Rng2.Start, "Specifications")

NewDoc.Range.InsertAfter ItemNo & vbCr & _
Quantity & vbCr & _
Desc & vbCr & _
Mfg & vbCr & _
Specs & vbCr & vbCr
'Stop
End If
End If
'Move the search range over one word.
Rng1.MoveStart wdWord
'Set Start to End value for next block.
RngStart = RngEnd
'Reset Rng1 end to document end.
Rng1.End = Rng2.End
Loop While Rng1.Find.Found

'Selects the last found to the end of the document.
If RngStart < Rng2.End Then
aDoc.Range(RngStart, Rng2.End).Select
End If

End Sub
Function FindIt(MyDoc As Document, RngStart As Long, RngEnd As Long, SrchStrg As String) As String
Set Rng4 = MyDoc.Range(RngStart, RngEnd)
With Rng4.Find
.Text = SrchStrg
.Wrap = wdFindStop
.Execute
End With
If Rng4.Find.Found Then
Select Case SrchStrg
Case "Specifications"
Rng4.Collapse wdCollapseEnd
Rng4.End = RngEnd

Case Else
Rng4.Collapse wdCollapseEnd
Rng4.MoveEndUntil vbCr

End Select

FindIt = Trim(Rng4.Text)
End If
'Rng4.Select
'Stop
End Function