Consulting

Results 1 to 15 of 15

Thread: Paragraphing formating

  1. #1
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location

    Paragraphing formating

    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?

  2. #2
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location
    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!!

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Do you want to put tags around a paragraph, or justify a paragraph acording to tags already there?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes, it is very unclear which way you are trying to go with this. It makes a HUGE difference.

  5. #5
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location
    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

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Uh.....

    You never answered, which would have been polite.

    You can do this easier - now that I figured out the answer myself.
    [vba]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[/vba]

    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.

  7. #7
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location
    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 ;-)

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  9. #9
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location
    Yeah I know how you mean, ill be sure to answer questions in the future, its ok!

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Excellent. Did the code work for you?

  11. #11
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location
    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

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    some major issues today with my VBA code
    That is a teaser for sure....what's up?

  13. #13
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location
    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?

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.
    [vba]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[/vba]

    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.

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Since this (new) topic has now moved to another thread (appropriately), could you mark this one as Solved?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •