Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post

    Headers, fields, StyleRef, and perhaps macros

    This is a question about fields in headers, which might become a more technical question about fields and macros.

    I’m writing a book about Vintage Port. There is a chapter about each ‘shipper’. Chapters begin with a style ChapterTitle, containing the name of the shipper, such as “Gould Campbell”. As some information is in two columns, sections are not the same as chapter boundaries. So I have not put each Chapter in a separate section.

    Naturally, the header currently has a field { STYLEREF "ChapterTitle" }

    • How do I suppress this on the first page of a chapter? Such that if the page contains (starts with) a paragraph in the style ChapterTitle, the header is blank?

    The reader could be given better navigational guidance. There is a ¶ style VintageYear, which contains strings such as “1881 Gould Campbell Produced and Sold” (summarising the detail which follows). I’d like the Header to be something like “Gould Campbell, 1881-1892”, as that helps the reader navigate.
    ◊ The “Gould Campbell” comes from the { STYLEREF "ChapterTitle" }
    ◊ The “, ” is a string constant.
    ◊ The “1881” would be the first four characters of a { STYLEREF "VintageYear" }.
    ◊ The “-” is a string constant.
    ◊ The 1892 would be the first four characters of a { STYLEREF "VintageYear" \l }.
    ◊ If the reader will allow some notational slippage, obviously if 1881=1892 then only one is shown, and not the hyphen.

    • Please suggest how this might be done?
    • How do I prune to the first four characters?
    • How test whether 1881=1892? (For some later vintages there are multiple ‘1980’s, so mustn’t test whether only one ¶.)

    Indeed, there’s more complexity still: the last VintageYear style typically says “Later vintages”, which means post-1986 Vintages. So there could be header fragments of “1977-”, if a page starts on 1977 and ends on “Later…”, or just “Later Vintages if that’s the whole page. And sometimes, after that, comes a style ChapterSubsectionTitle containing a string “Data Conflicting with List of Gould Campbell Vintages”, necessitating further header complexity.

    This could be most easily solved with a field saying {*WordVBAfunctionHeaderText*}, which could interrogate what’s on the page and construct the header.

    • Is this possible?
    • How?

    Thank you.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by jdaw1 View Post
    This is a question about fields in headers, which might become a more technical question about fields and macros.

    I’m writing a book about Vintage Port. There is a chapter about each ‘shipper’. Chapters begin with a style ChapterTitle, containing the name of the shipper, such as “Gould Campbell”. As some information is in two columns, sections are not the same as chapter boundaries. So I have not put each Chapter in a separate section.

    Naturally, the header currently has a field { STYLEREF "ChapterTitle" }

    • How do I suppress this on the first page of a chapter? Such that if the page contains (starts with) a paragraph in the style ChapterTitle, the header is blank?
    Simple: use a Separate Section for each chapter, with a 'different first page' layout and have the STYLEREF field only on the second & subsequent pages.
    Quote Originally Posted by jdaw1 View Post
    The reader could be given better navigational guidance. There is a ¶ style VintageYear, which contains strings such as “1881 Gould Campbell Produced and Sold” (summarising the detail which follows). I’d like the Header to be something like “Gould Campbell, 1881-1892”, as that helps the reader navigate.
    ◊ The “Gould Campbell” comes from the { STYLEREF "ChapterTitle" }
    ◊ The “, ” is a string constant.
    ◊ The “1881” would be the first four characters of a { STYLEREF "VintageYear" }.
    ◊ The “-” is a string constant.
    ◊ The 1892 would be the first four characters of a { STYLEREF "VintageYear" \l }.
    ◊ If the reader will allow some notational slippage, obviously if 1881=1892 then only one is shown, and not the hyphen.
    About the only way you could achieve anything like this would be to use a macro to apply a unique character Style to just the year strings throughout the document, then use fields along the lines of what you have suggested to pull just the year data into the header. Your ', ' constant would, of course be typed, and field coding could be used to conditionally show/hide the '-YYYY' part where the years are the same. For example:
    {STYLEREF "Year"}{IF{STYLEREF "Year"}<>{STYLEREF "Year" \l} "-{STYLEREF "Year" \l}}
    where Year is the Style's name.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following User Says Thank You to macropod For This Useful Post:

    jdaw1 (2016-11-12)

  4. #3
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Thank you. That all makes sense, even if wasn’t quite as neat as hoped (entailing much faffing with column-number section breaks).

    Simple script aide: paragraph p of document. What p.… gets the string comprising characters i to i+3 of the ¶?

    Thank you.

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I don't know of a way to truncate the result of a StyleRef field to a certain number of characters.

    The only workaround I know of is to apply a character style to just the characters you want in the styleref result. This is described in http://wordfaqs.mvps.org/styleref.htm. You could easily automate the tagging of those paragraphs since a macro has no problem identifying the left side characters of a paragraph.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by jdaw1 View Post
    Simple script aide: paragraph p of document. What p.… gets the string comprising characters i to i+3 of the ¶?
    I have no idea what you mean. In English, please...
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #6
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by macropod View Post
    I have no idea what you mean. In English, please...
    Sorry. Assume that p is a paragraph of the document. I’d like the sub-item that is th string comprising part of the paragraph: characters i to i+3. This oculd be the whole ¶ as a string, on which I do string manipulation, or it could be the four-character string plucked directly from the ¶.

    Thank you.

  8. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    For that, you could use a macro such as:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      .InsertBefore vbCr
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[^13][0-9]{4}>"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        .Start = .Start + 1
        .Style = "Strong"
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    ActiveDocument.Range.Characters.First.Delete
    Application.ScreenUpdating = True
    End Sub
    As coded, the macro applies the 'Strong' Style to any paragraph that begins with exactly 4 digits.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. The Following User Says Thank You to macropod For This Useful Post:

    jdaw1 (2016-11-12)

  10. #8
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Thank you. The following works well on a test document. Before I dare apply it to my master (which is backed up, but the danger is a failure to notice a problem), please comment on:
    Code:
    Option Explicit
    
    Sub SubSubSectionYYYY()
        With ActiveDocument
            Dim st As Style
    
            On Error GoTo StyleExists
            Set st = .Styles.Add("NavigationYYYY", wdStyleTypeCharacter)
            With st
                ' Perhaps test formatting
                ' st.Font.Color = RGB(0, 204, 0)
            End With  ' st
    StyleExists:
    
            With .Range
                With .Find
                    .MatchWildcards = True
                    .Text = "<[12][0789][0-9][0-9]>"  ' Alas "<(17|18|19|20)[0-9][0-9]>" seems not to work
                    .Format = True
                    .Style = "VintageYearChar Char"  ' "VintageYear" then "VintageYearChar Char"
                    .Forward = True
                    .Wrap = wdFindStop
                    .Execute
                End With  ' .Find
                Do While .Find.Found
                    .Style = "NavigationYYYY"
                    .Collapse wdCollapseEnd
                    .Find.Execute
                Loop
            End With  ' .Range
        End With  ' ActiveDocument
    End Sub  ' SubSubSectionYYYY

  11. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    It appears you have some evil 'char char' styles in your document. I suggest you clean them out before they corrupt your document. See: http://homepage.swissonline.ch/cindy...p.htm#CharStyl
    A more flexible macro is:
    Code:
    Sub CleanStyles()
    Dim oSty As Style
    With ActiveDocument
      On Error Resume Next
      For Each oSty In .Styles
        If oSty.Linked = True Then
          .Styles.Add Name:="TmpSty"
          oSty.LinkStyle = "TmpSty"
          .Styles("TmpSty").Delete
        End If
        If oSty.BuiltIn = False Then oSty.Delete
      Next
    End With
    End Sub
    Once you've done that, you should be able to replace:
    .Style = "VintageYearChar Char"
    with:
    .Style = "VintageYear"

    Other than that, I'd be inclined to replace:
    .Text = "<[12][0789][0-9][0-9]>"
    with:
    .Text = "<[12][0-9]{3}>"
    and replace:
    .Style = "NavigationYYYY"
    with:
    If (CLng(.Text) > 1700) And (CLng(.Text) < Format(Now, "YYYY")) Then .Style = "NavigationYYYY"
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  12. The Following User Says Thank You to macropod For This Useful Post:

    jdaw1 (2016-11-12)

  13. #10
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by macropod View Post
    It appears you have some evil 'char char' styles in your document. I suggest you clean them out before they corrupt your document. See: [url]http://homepage.swissonline.ch/cindymeister/MyFavTip.htm#CharStyl
    A more flexible macro is:
    Code:
    Sub CleanStyles()
    Dim oSty As Style
    With ActiveDocument
      On Error Resume Next
      For Each oSty In .Styles
        If oSty.Linked = True Then
          .Styles.Add Name:="TmpSty"
          oSty.LinkStyle = "TmpSty"
          .Styles("TmpSty").Delete
        End If
        If oSty.BuiltIn = False Then oSty.Delete
      Next
    End With
    End Sub
    Wilco.

    Is it that all character styles are evil, or only some?

    Some of my paragraphs have within them two styles

    1878 Gould Campbell Produced and Sold

    There is a paragraph style, doing ¶-level stuff, and the “1878 Gould Campbell” also has a char style. Obviously the new being-applied character style will have to match the old.

    Quote Originally Posted by macropod View Post
    Other than that, I'd be inclined to replace:
    .Text = "<[12][0789][0-9][0-9]>"
    with:
    .Text = "<[12][0-9]{3}>"
    and replace:
    .Style = "NavigationYYYY"
    with:
    If (CLng(.Text) > 1700) And (CLng(.Text) < Format(Now, "YYYY")) Then .Style = "NavigationYYYY"
    Sure, but why? Why have an If that can fail rather than letting Find do the work?

  14. #11
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by macropod View Post
    Code:
        If oSty.Linked = True Then
    Quote Originally Posted by Microsoft
    Compile error:

    Method or data member not found
    Microsoft Word for Mac Version 15.28 (161110)

  15. #12
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    All char char Styles are problematic and tend to proliferate.

    Why you'd get that error on a Mac isn't clear.

    As for:
    Why have an If that can fail rather than letting Find do the work?
    Because the Find (whether yours or mine) isn't foolproof (e.g. any 4-digit number beginning with 10, 27,28,29 would not be a valid year, nor would 2016-2099) . The If test ensures the year is after 1700 and before whatever the current year is when you run the macro.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  16. The Following User Says Thank You to macropod For This Useful Post:

    jdaw1 (2016-11-12)

  17. #13
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    Thank you the help: expert and prompt.

    For the interest of readers, my header now says
    Code:
    {STYLEREF "ChapterTitle" \* MERGEFORMAT }: { IF { STYLEREF "ChapterSubSectionTitle" }="Data Conflicting with *" "{ IF { STYLEREF "NavigationYYYY" }<>{ STYLEREF "NavigationYYYY" \l }"{ STYLEREF NavigationYYYY" }-{ STYLEREF "NavigationYYYY" \l };" } conflicting data" "{ STYLEREF "NavigationYYYY" }{ IF { STYLEREF "NavigationYYYY" }<>{ STYLEREF "NavigationYYYY" \| } "-{ STYLEREF "NavigationYYYY" \l }" }" \* MERGEFORMAT }
    (Typos caused by OCR — couldn’t work out how to copy code across.)

    There might be a later version.

  18. #14
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    For a macro to generate text representations of field coding, see: http://www.gmayor.com/export_field.htm#FieldToText
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  19. #15
    Lounger jdaw1's Avatar
    Join Date
    Dec 2010
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    My problem is more complicated than I thought. Yes, I know, not unusual.

    Vintages are generally part of text that is red and bold. There are two (not the just one mentioned above) types of ¶ in which they might appear. First has a bold ¶ style. Second doesn’t. Applying a VintageNavigation character style therefore doesn’t work: Word is not CSS. Applying bold to one unbolds the other!

    Also, applying bold is less elegant than selecting the bold-variant types face. But with one ¶ style being sans-serif and one serif the character style just can’t do that.

    ¿So, can StylerRef take two styles? Can StyleRef apply an Or-type question to them? Alternatively, can I StyleRef one, StyleRef the other, and without ¶ numbering know which appeared later? Then I could have two different character styles (say VintageNavigationFull and VintageNavigationPartial). Those styles could be optimised for the ¶ styles over which they are applied. And my headers could still work.

    What a faff!

    And thank you again for the already excellent comments.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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