Consulting

Results 1 to 3 of 3

Thread: How to left and right justify elements in a footer range?

  1. #1

    How to left and right justify elements in a footer range?

    I have a routine that adds the page number and filename to a footer. The page number is left-justified (that works), but I am unable to persuade the filename to be right aligned by setting the appropriate tab stop. Presently I have to adjust the filename position manually when the macro has run. The offending code is (I imagine):

    rngFooter.Paragraphs.TabStops.ClearAll
        rngFooter.Paragraphs.TabStops.Add wdAlignTabLeft, wdAlignTabRight
    What should it be to left justify the page number and right justify the filename? Maybe I need to add the tab positions, but the routine is called by a variety of other routines, and the margin sizes will differ from call to call, so setting absolute tab stop positions will not work.

    Full routine is below.

    Thanks

    John Davidson


    Sub PageNosFileNameAdd_Footer()
    '   add page number and name to footer
    
    
        Dim rngFooter As Range
        Dim currDoc As Word.Document
        Dim field As field
    
    
        Set currDoc = Application.ActiveDocument
        
        currDoc.PageSetup.OddAndEvenPagesHeaderFooter = False
        Set rngFooter = currDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
    
    
        For Each field In rngFooter.Fields
            field.Delete            ' Remove existing fields
        Next
    
    
        rngFooter.Font.Name = "IndUni-T"
        rngFooter.Font.Size = 9  
        rngFooter.Paragraphs.TabStops.ClearAll
        rngFooter.Paragraphs.TabStops.Add wdAlignTabLeft, wdAlignTabRight
        
        rngFooter.Text = ""                             ' Delete any existing page number
        rngFooter.Fields.Add rngFooter, wdFieldPage
          
        rngFooter.Collapse wdCollapseEnd
        rngFooter.Text = vbTab & vbTab & removeExtension(currDoc.Name)
        
        currDoc.Activate
        currDoc.UndoClear
        DoEvents
    
    
    End Sub
    
    
    Private Function removeExtension(p_strFilename As String) As String
        Dim nPosn As Long
    
    
        nPosn = InStrRev(p_strFilename, ".")
        If nPosn > 0 Then
            removeExtension = Left(p_strFilename, nPosn - 1)
        Else
            removeExtension = p_strFilename
        End If
    
    
    End Function
    Last edited by johndavidson; 01-05-2015 at 05:11 AM.

  2. #2
    You are right about the offending code. You need to set the positions of the tab stops (or you could simply apply the default Footer style which already has the tab stops) before changing the font and inserting the text. However the following should work:

    Sub PageNosFileNameAdd_Footer()
    '   add page number and name to footer
    Dim rngFooter As Range
    Dim currDoc As Word.Document
    
        Set currDoc = Application.ActiveDocument
        currDoc.PageSetup.OddAndEvenPagesHeaderFooter = False
        Set rngFooter = currDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
        With rngFooter
            .Text = ""        ' Delete any existing page number
            .Fields.Add rngFooter, wdFieldPage
            .Collapse wdCollapseEnd
            .Text = vbTab & vbTab & removeExtension(currDoc.name)
        End With
        Set rngFooter = currDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
        rngFooter.Font.name = "IndUni-T"
        rngFooter.Font.Size = 9
        SetFooterTabs currDoc, rngFooter
        currDoc.Activate
        currDoc.UndoClear
        DoEvents
        Set rngFooter = Nothing
        Set currDoc = Nothing
    End Sub
    
    Sub SetFooterTabs(oDoc As Document, orng As Range)
    Dim lngCenter As Long
    Dim lngRight As Long
        lngCenter = (oDoc.PageSetup.PageWidth _
                     - oDoc.PageSetup.LeftMargin _
                     - oDoc.PageSetup.RightMargin) / 2
        lngRight = oDoc.PageSetup.PageWidth _
                   - oDoc.PageSetup.LeftMargin _
                   - oDoc.PageSetup.RightMargin
        'MsgBox PointsToCentimeters(lngCenter) & vbCr & PointsToCentimeters(lngRight)
        orng.ParagraphFormat.TabStops.ClearAll
        orng.ParagraphFormat.TabStops.Add _
                Position:=lngCenter, _
                Alignment:=wdAlignTabCenter, _
                Leader:=wdTabLeaderSpaces
        orng.ParagraphFormat.TabStops.Add _
                Position:=lngRight, _
                Alignment:=wdAlignTabRight, _
                Leader:=wdTabLeaderSpaces
    End Sub
    Last edited by gmayor; 01-05-2015 at 06:47 AM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks, I worked with your previous code, but I see that you have since updated it. It looks tidier, so I'll have another look. One thing I did notice and change in your previous version is that the tab stops need to be set AFTER adding the page number as a field, otherwise the tab-stop settings revert to the default when the page number field is added.

    Later - I've checked out your new code and it works perfectly. The only thing I changed was to add only the RH tab stop and omit the centre tab stop. Only one vbTab is then required. Since the page number is left-justified, the centre tab is not required, and very long file names are now accommodated as well without their going off-piste.

    Thanks v. much. Thread has been marked as solved.
    Last edited by johndavidson; 01-05-2015 at 08:53 PM.

Posting Permissions

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