PDA

View Full Version : Paragraphing formating



1with49
10-10-2005, 10:40 AM
I have a requirement to be able to put tags around a paragraph that determine its jusfiication IE <L> , <R> , <J>. IE

<L> this is a paragraph full of text etc etc etc </L>

So basically I need to examine the first and last word in each paragraph for the tags the justify, I know how to justify a paragraph but cant really find the relationship between words and paragraph in the object structure to test against....help anyone?

1with49
10-10-2005, 11:52 AM
Dim test As range
Dim testString As String

Set test = ActiveDocument.Paragraphs(1).range(Start:=ActiveDocument.Paragraphs(1).rang e.Start, End:=ActiveDocument.Paragraphs(1).range.End)

testString = test.Words(1) + test.Words(1) + test.Words(2)
If testString = "<<l>>" Then
ActiveDocument.Paragraphs.Alignment = wdAlignParagraphLeft
End If


I think this is similar to what iam trying to achieve!!

TonyJollans
10-10-2005, 02:18 PM
Do you want to put tags around a paragraph, or justify a paragraph acording to tags already there?

fumei
10-10-2005, 06:55 PM
Yes, it is very unclear which way you are trying to go with this. It makes a HUGE difference.

1with49
10-11-2005, 08:23 AM
Sub paragraphFormatting()
Dim paragraphCount As Integer
Dim paragraphCounter As Integer
paragraphCount = ActiveDocument.Paragraphs.Count
paragraphCounter = 1
Do While paragraphCounter <= paragraphCount
Set myRange = ActiveDocument.Paragraphs(paragraphCounter).range
With myRange

If .Words.Count > 6 Then
If (myRange.Words(1) = "<<" And myRange.Words(2) = "l" And Left(myRange.Words(3), 2) = ">>") Then
If Left(myRange.Words(myRange.Words.Count - 1), 2) = ">>" And myRange.Words(myRange.Words.Count - 2) = "l" And Right(myRange.Words(myRange.Words.Count - 3), 3) = "<</" Then
ActiveDocument.Paragraphs(paragraphCounter).Alignment = wdAlignParagraphLeft
End If
End If

If (myRange.Words(1) = "<<" And myRange.Words(2) = "r" And Left(myRange.Words(3), 2) = ">>") Then
If Left(myRange.Words(myRange.Words.Count - 1), 2) = ">>" And myRange.Words(myRange.Words.Count - 2) = "r" And Right(myRange.Words(myRange.Words.Count - 3), 3) = "<</" Then
ActiveDocument.Paragraphs(paragraphCounter).Alignment = wdAlignParagraphRight
End If
End If

If (myRange.Words(1) = "<<" And myRange.Words(2) = "j" And Left(myRange.Words(3), 2) = ">>") Then
If Left(myRange.Words(myRange.Words.Count - 1), 2) = ">>" And myRange.Words(myRange.Words.Count - 2) = "j" And Right(myRange.Words(myRange.Words.Count - 3), 3) = "<</" Then
ActiveDocument.Paragraphs(paragraphCounter).Alignment = wdAlignParagraphCenter
End If
End If
End If
paragraphCounter = paragraphCounter + 1
End With
Loop



End Sub

I did achieve this in the end............strange what a good nights sleep can do...... thanks again

fumei
10-11-2005, 10:44 AM
Uh.....

You never answered, which would have been polite.

You can do this easier - now that I figured out the answer myself.
Sub FormatByTags()
Dim oPara As Word.Paragraph
For Each oPara In ActiveDocument.Paragraphs
If Left(oPara.Range.Words(1), 1) = "<" Then
Select Case UCase(oPara.Range.Words(2).Text)
Case "L"
oPara.Range.ParagraphFormat.Alignment = _
wdAlignParagraphLeft
Case "J"
oPara.Range.ParagraphFormat.Alignment = _
wdAlignParagraphJustify
Case "R"
oPara.Range.ParagraphFormat.Alignment = _
wdAlignParagraphRight
End Select
End If
Next
End Sub

This does the following:

1. checks each paragraph in the document to see if the first word is "<"

2. If it is, checks the second word to see if it is L, J, or R. It makes it UpperCase for the check. This is just in case it may be l, j, or r. The reason for checking the SECOND word is that Word considers "<" a word in itself. Even if there is no space - as in "<J>" . The "<" is Words(1), the "J" is Words(2).

3. makes the current paragraph align according to if it is L, J, or R.

I did it this way, because if your paragraphs start with the tags....why use the end tag at all? Why bother? You can get all the information from the opening tag.

Also, why go through a paragraph count when you can simply loop through the paragraphs themselves?

Have fun.

Please note that even if you have "<<<<<<R>>>>>>", the R is STILL considered Words(2). In other words (pun intended):

<<<<J>>>> - Words(1) = "<<<<"
<<J>> - Words(1) = "<<"
<J> - Words(1) = "<"

Just one of the oddities of Word.

1with49
10-12-2005, 05:43 AM
Hey thanks, I will try this instead as it is a bit more readable, ill remember to post replies and answers to my problems if I answer them before anyone else.........I could really do with working on my manners ;-)

fumei
10-12-2005, 05:46 AM
Hey, I am not really trying to be critical, or make anyone feel bad. It is just that when clear questions are asked, it is fair to ask that answers be returned.

Don't worry about it.

1with49
10-12-2005, 05:52 AM
Yeah I know how you mean, ill be sure to answer questions in the future, its ok!

fumei
10-12-2005, 05:53 AM
Excellent. Did the code work for you?

1with49
10-12-2005, 05:58 AM
I havent got round trying it yet, as I have come accross some major issues today with my VBA code ive written in the last ten days, and it isnt pretty!!

But it looks good and futher still exactly what I need....lets hope I can get the rest working

fumei
10-12-2005, 06:01 AM
some major issues today with my VBA code
That is a teaser for sure....what's up?

1with49
10-12-2005, 06:22 AM
basically I have a macro that performs actions based on tags in the word doc IE <<bold>> <<italics>>. The macro Ive written works but on large documents it just hangs.
Altho the document iam working on consists of one index document with word link fields about 30 other docs....And this operation has to be performed on each of them when the document is opened. If I run the macro on each doc seperatly then I dont have any issues, but when I try the whole things it just doesnt work.

Any ideas?

fumei
10-12-2005, 07:19 AM
Ah, I believe this is being covered by another thread. Linked documents are not opened in the normal way. Therefore your code does not operate on them.

You could build an array of the linked documents name and path, and then run through that array opening the files.
Dim oField As Word.Field
Dim SourceFiles() As String
Dim i As Integer
For Each oField In ActiveDocument.Fields()
ReDim Preserve SourceFiles(i)
SourceFiles(i) = oField.LinkFormat.SourceFullName
i = i + 1
Next

This builds the array. Now you have a string array, a list, of all the document names in the LINK fields. Using SourceFullName gets the full path. if you want you could use SourceName to just get the document name, without the path. This would only be useful if the linked documents are all in the same folder.

In any case, with the list, you can run through it and open the files.

Alternatively...depending on where and your code is doing, you could use the linked documents name to fire code in them.

fumei
10-12-2005, 07:25 AM
Since this (new) topic has now moved to another thread (appropriately), could you mark this one as Solved?