PDA

View Full Version : Trimming Junk from a Range



MWE
02-19-2006, 05:13 PM
I am running Word2000. I have a VBA created table that copies paragraphs from various places in the doc into the table. The original paragraphs have some leading and trailing "junk" that I wish to "trim".

Initially I wrote a TrimJunk function that trimmed off chars with ASCII values < 33 from both ends of a text string and then set the table cell's range = TrimJunk(targetParagraph). It worked fine, but I lost all the other formatting.

So, how do I trim off junk from the start and end of a range and keep all the formatting for the rest of the range.

Thanks

mdmackillop
02-19-2006, 06:14 PM
Paragraph formatting is held in the Pilcrow. If you shorten your range by one character from the end, I think you should retain the formatting.
Regards
MD

MWE
02-19-2006, 06:39 PM
Paragraph formatting is held in the Pilcrow. If you shorten your range by one character from the end, I think you should retain the formatting.
Regards
MD
Thanks. Sorry to be dense, but what is the Pilcrow? I thought it was just a paragraph "symbol".

How do I shorten the range by one character at a time? I have tried several approaches, but the compiler balks at them

mdmackillop
02-19-2006, 06:49 PM
The Pilcow is the name of the paragraph symbol. The following will exclude the Pilcrow from the paragraph containing the cursor.


Dim MyPara As Range
Set MyPara = Selection.Paragraphs(1).Range
MyPara.SetRange Start:=MyPara.Start, End:=MyPara.End - 1
MyPara.Select

MWE
02-19-2006, 08:32 PM
The Pilcow is the name of the paragraph symbol. The following will exclude the Pilcrow from the paragraph containing the cursor.


Dim MyPara As Range
Set MyPara = Selection.Paragraphs(1).Range
MyPara.SetRange Start:=MyPara.Start, End:=MyPara.End - 1
MyPara.Select

Thanks.

The code you provided does not seem to reduce the size of the range; at least as determined by the Len function: Index = Len(.Range)
.Range.SetRange Start:=.Range.Start, End:=.Range.End - 1
MsgBox Index & vbTab & Len(.Range)
The length before and after are the same; but perhaps the the formatting stuff is invisible to the function. I even tried chopping of 10 characters and nothing happened.

Whatever, it does not eliminate any of the "non-text" stuff.

Part of the problem is that some of the orig paragraphs are parts of numbered lists. When the para is copied, the list stuff comes along and renumbers itself on the fly. So it might have been, say, #8 in the original list and it is #1 in the table cell. It would be OK if the # were at least correct.

mdmackillop
02-19-2006, 08:33 PM
Can you post your TrimJunk function?

Dave
02-19-2006, 11:56 PM
Maybe just replace the junk with ""'s? That seems like it would keep your formatting the same. I'm not sure but it may be worth a try. Dave

TonyJollans
02-20-2006, 02:59 AM
Hi MWE,

Several points here :)

1. Malcolm's code will only work on a Range variable. You cannot change the Range of a Paragraph (I'm guessing your code is inside a With Paragraph)

2. What exactly is the 'junk' you are trying to trim?

3. All the formatting is, as already said, effectively in the pilcrow.

4. If you have numbered paragraphs, the number is not normal text, it is part of the formatting saying, in effect, use the next number in the sequence. If you copy the paragraph - and its formatting - you will be copying that instruction rather than the number and you will get a different number as you have seen.

5. Finally, what I think you may want - an addition to Malcolm's code to include character formatting - you'll have to use your own references for the source paragraph and target table cell, of course.

Dim MyPara As Range
Set MyPara = Selection.Paragraphs(1).Range
MyPara.SetRange Start:=MyPara.Start, End:=MyPara.End - 1
MyPara.Copy
ActiveDocument.Tables(1).Range.Cells(1).Range.PasteAndFormat wdPasteDefault

MWE
02-20-2006, 07:55 AM
Can you post your TrimJunk function?
The original TrimJunk trims any characters whose Ascii # is less than 33 from the end of a text string. Function TrimJunk(Text) As String
'
' trims off junk from end of text string
'
TrimJunk = Text
ExamineText:
If Asc(Right(TrimJunk, 1)) <= 33 Then
TrimJunk = Left(TrimJunk, Len(TrimJunk) - 1)
GoTo ExamineText
End If

End Function I originally wrote it when I was copying just the "text" from the target para to the target cell with something like .Range = TargetParaThis method simply copies the text in TargetPara's Range to the Range of the target cell. I noticed that most of the cells had linefeeds at the end of the text and, thus, each cell had an additional blank line at the bottom. So I wrote TrimJunk to get rid of trailing junk and the corresponding new "copY' statement was .Range = TrimJunk(TargetPara) That worked a lot better and all the extra junk was gone. And I can live with that. But the preferred result is that the "extra" junk is gone but the text formatting remains. So characters that were bold still are, etc.

So that was when I posted to the forum inquiring about ways to do this.

If I use a standared copy/past, I get all the formatting (good) but also all the junk (bad). As mentioned above, I have tried to chop off trailing "characters" from the Range, but that does not seem to work. Also as mentioned above, I could live with the extra linefeeds, the numbers (from numbered lists, etc) and similar stuff, but the fact that the numbers are incorrect when pasted is not acceptable.

MWE
02-20-2006, 08:07 AM
Tony: thanks for the reply ...

Hi MWE,

Several points here :)

1. Malcolm's code will only work on a Range variable. You cannot change the Range of a Paragraph (I'm guessing your code is inside a With Paragraph) true



2. What exactly is the 'junk' you are trying to trim? see previous post with TrimJunk function code shown



3. All the formatting is, as already said, effectively in the pilcrow. drat!



4. If you have numbered paragraphs, the number is not normal text, it is part of the formatting saying, in effect, use the next number in the sequence. If you copy the paragraph - and its formatting - you will be copying that instruction rather than the number and you will get a different number as you have seen. drat! drat!



5. Finally, what I think you may want - an addition to Malcolm's code to include character formatting - you'll have to use your own references for the source paragraph and target table cell, of course.

Dim MyPara As Range
Set MyPara = Selection.Paragraphs(1).Range
MyPara.SetRange Start:=MyPara.Start, End:=MyPara.End - 1
MyPara.Copy
ActiveDocument.Tables(1).Range.Cells(1).Range.PasteAndFormat wdPasteDefault my system does not recognize "wdPasteDefault"

Thanks again. It appears that I can have either text only without junk or all formatting with junk ... what a choice :banghead:

TonyJollans
02-20-2006, 09:40 AM
I'm not sure about what is in what version of Word off the top of my head. What happens if you replace the last line of my code with simply .. ActiveDocument.Tables(1).Range.Cells(1).Range.Paste

Dave
02-20-2006, 09:41 AM
Thought of another possibility. Maybe something like this? Dave

Dim TempStr As String, BigString As String
'Trim f(x)
'Set MyPara = etc...
MyPara.Select
TempStr = Wapp.Selection.Text
'store in bigstring in loop if required
BigString = BigString + TempStr
'end loop
ActiveDocument.Tables(1).Range.Cells(1) = BigString


edit: If you change your range to a string you don't want to use the trim f(x) because you need the format. Are you trying to get rid of that square thingee when you copy and paste? Pilcrow? If that is the issue, changing the range to a string fixes this.