Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    New Lounger
    Join Date
    Dec 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Bookmarks & PageRef (2003)

    I'm writing a fairly complex book in Word which describes the use of over 200 special codes used to create documents.
    For the exercise, you may liken these codes to HTML tags.

    The main part of the book is organised into Chapters such as Page Commands, Paragraph Commands, Line Commands, etc..

    Within each chapter are Topics, where appropriate Codes are described in simplified detail with examples. E.G. under Page Commands will be Page Numbering, Page Headers, Page Footers, etc..

    So far, so good.

    An Appendix contains an alphabetically list of these Codes in a two column table. The first column contains the Code and a brief but technical description. I want the second column to show the Page Number within the main book where the simplified explanation resides.

    For those who know Word, this is a relatively simple thing to achieve. Create a Bookmark at the beginning of each Topic, then in the table insert a "PageRef" field which produces the appropriate page number in the document.

    So what's the problem?

    The Word document is actually created by a 3rd party application. Furthermore, the content is changing on a fairly regular basis as new Codes are added, or even simply when I re-write or amend a Topic.

    Net result? I have to re-create all the BookMarks and PageRefs.

    What would I like?

    1) An automated method of producing and inserting Word Bookmarks against a specific Style. In this case, it would be "Heading 4".

    2) Given that I can, create "Conditional Text" which would be hidden in the 3rd party file, but appear in the Table Column in Word, I want to use that text, to match up with the BookMark list, and effectively create a PageRef field.

    Let my try to give an example.

    An Topic might be "Arabic v Roman" and this wording appears as the Topic Title to which a Heading 4 Style has been applied. That's fine, it comes into Word as Heading 4.

    The Code(s) are actually "[pnta]" and "[pntr]", and so in the Table I could produce "hidden text", say "Arabic v Roman", which would again come into Word.

    I want to be able to first create and insert a Bookmark called "Arabic_v_Roman" in the Word file, and then go down the table matching "Arabic v Roman" to the Bookmark, replacing the text with the PageRef Field. (Note that Bookmarks must have underscores replacing spaces in the Bookmark names.)

    And I need to do that something like 230 times automatically, and a number of times in a day/week/month when I'm working on the book.

    Does anyone have any ideas?

    George.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmarks & PageRef (2003)

    Welcome to Woody's Lounge.

    Could you create and attach a small demo document (with dummy text) to illustrate the starting situation and the desired result? The file should be less than 100 KB; if it's too large, create a zip file from it and attach that.

  3. #3
    New Lounger
    Join Date
    Dec 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bookmarks & PageRef (2003)

    I hope the attached helps. First 2 pages show general layout of one topic and table entries.
    Second two pages have Bookmark defined, and PagRef in table as I want it, so questions are.
    1) Is there a way (with 200+ topics) to automatically generate a Bookmark, which is the same name as the Heading 4 Topic Title (bearing in mind underscores have to replace spaces)?
    2) Is there a way to automate the PageRef process using the text is columm 2 of the table to look up the Bookmark list and apply the PageRef accordingly?
    George
    Attached Files Attached Files

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmarks & PageRef (2003)

    See the macro below.

    Notes:
    - Make sure that the only Heading 4 paragraphs are the topics you want to include. In particular, there shouldn't be any blank Heading 4 paragraphs, and page breaks and section breaks should not have Heading 4 as style.
    - Make a backup copy of the document before running the macro.

    Sub CreateBookmarksEtc()
    Dim colHeadings As New Collection
    Dim strText As String
    Dim varItem As Variant

    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .Text = ""
    .ClearFormatting
    .Format = True
    .Style = wdStyleHeading4
    Do While .Execute = True
    Selection.MoveEnd Unit:=wdCharacter, Count:=-1
    strText = Replace(Selection.Text, " ", "_")
    colHeadings.Add Item:=Selection.Text
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.Bookmarks.Add Name:=strText
    Loop
    .ClearFormatting
    .Format = False
    For Each varItem In colHeadings
    Selection.HomeKey Unit:=wdStory
    .Execute FindText:="ALPHABETIC SUMMARY OF CODES"
    Selection.Collapse Direction:=wdCollapseEnd
    .Text = varItem
    Do While .Execute = True
    Selection.TypeText Text:="See page "
    Selection.Fields.Add Range:=Selection.Range, _
    Type:=wdFieldPageRef, _
    Text:=Replace(varItem, " ", "_")
    Loop
    Next varItem
    End With

    Set colHeadings = Nothing
    End Sub

  5. #5
    New Lounger
    Join Date
    Dec 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bookmarks & PageRef (2003)

    Thank you for taking such an interest. But please bear with me here. Actually working with the nuts and bolts of Macros is fairly new to me.

    I believe I have successfully attached the macro to the document, but when I run it, I'm getting a Run-Time error 5828 Bad Bookmark Name. When I Debug, it is highlighting line 17. Selection.Bookmarks.Add Name:=strText <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

    I presume I'm doing something wrong, or not doing something I should have, but I'm just not sure where to begin. <img src=/S/confused3.gif border=0 alt=confused3 width=45 height=45>

    George.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmarks & PageRef (2003)

    Apparently, one of the Heading 4 paragraphs contains text that - after replacing spaces with underscores - results in an invalid bookmark name. While the macro is paused, and the offending line is highlighted in yellow, you can inspect the value of variables by hovering the mouse over them. What do you see if you hover the mouse over strText?
    You can also switch back (using the Windows task bar or by pressing Alt+Tab) to the Word document, to see what the current selection is.

    You can terminate macro execution by clicking the Reset button on the toolbar.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmarks & PageRef (2003)

    Excellent, thank you. If George copies your function into the modile, he can replace

    strText = Replace(Selection.Text, " ", "_")

    with

    strText = strBookmarkName(Selection.Text)

    This should be more robust.

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Bookmarks & PageRef (2003)

    I have a standard procedure for converting strings to valid bookmark names, there may be an easier way but this works for me.

    <code>
    Private Function strBookMarkName(strInString As String) As String

    Dim strTemp As String
    Dim strChar As String
    Dim i As Integer
    For i = 1 To Len(strInString)
    strChar = Mid(strInString, i, 1)
    Select Case strChar
    Case "a" To "z", "A" To "Z"
    strTemp = strTemp & strChar
    Case "0" To "9"
    If Len(strTemp) > 1 Then strTemp = strTemp & strChar
    Case " "
    strTemp = strTemp & "_"
    Case Else
    End Select
    Next i
    strBookMarkName = strTemp
    End Function
    </code>

    StuartR

    Edited by StuartR to add "0" To "9" as allowed characters in a bookmark

  9. #9
    New Lounger
    Join Date
    Dec 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bookmarks & PageRef (2003)

    Hans, Stuart,

    Taking things in sequence - Hans, I have to say that I am absolutely delighted. You are a genius indeed. Thank you!

    I do however have two relatively minor problems, the second of which I cannot figure out.

    1) Obviously punctuation in "Heading 4" headings is a no no, so full stops, dashes and brackets are out. Pity, but I'm sure I can work round that.

    2) In the attached test file, I cannot for the life of me figure out why "Conditional Eject or Skip Line" does not work properly in the table at the end of the document.. If you run the Macro, you will see what I mean when you look at what appears in the table's right column.

    Stuart, Hans, this is my first serious venture into the code behind Macros, so all this is a massive learning exercise. Hence I have no idea where in Hans's original macro the code should be copied to.

    Finally, Hans, I am not sure what your amended line does?

    George.
    Attached Files Attached Files

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmarks & PageRef (2003)

    With Stuart's code, you can have punctuation in your Heading 4 text, as long as the text in the table corresponds exactly.

    I know why the problem with Conditional Eject Or Skip occurs: on page 3, there is a heading Conditional Eject. When searching the table, ALL text Conditional Eject is replaced - also that in Conditional Eject Or Skip . If you have only one item in the table referring to each heading, we can stop after one replacement. This might be a problem - in your demo document, "vertical centering" occurs multiple times in the second column of the table.

    See attached modified version (zipped)
    Attached Files Attached Files

  11. #11
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Bookmarks & PageRef (2003)

    The problem with "Conditional Eject or Skip Line" is that it is preceded by "Conditional Eject". When the code processes the former line it replaces the "Conditional Eject " in "Conditional Eject or Skip Line" so that it reads "See page 3 or Skip Line".

    To fix this would need a major change in the code to check for the exact contsnts of the cell, rather than just searching for the text. OR change the order of your headings so that "Conditional Eject or Skip Line" comes before "Conditional Eject"

    To use the strBookMarkName function, simply copy the code and paste the whole thing, from <code>Private Function</code>... to ...<code>End Function</code> to the very end of your existing code (After the <code>End Sub</code>). Then replace the original line that says <code>strText = Replace(Selection.Text, " ", "_")</code> with <code>strText = strBookmarkName(Selection.Text)</code>

    StuartR

  12. #12
    New Lounger
    Join Date
    Dec 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bookmarks & PageRef (2003)

    Many thanks again.

    No point in me claiming to understand the code at the moment, but the revised Macro obviously works. That said, earlier Headings repeated the first two words?

    Vertical Centering was indeed there twice. Once with a space between words, and once with an underscore. It was an an earlier test, when I was not sure what was required to be in that column.

    I still ned to figure out where to add Stuart's code, but now have the courage and enthusiasm to look at this in detail tomorrow. (It's 10.30pm here in the UK)

    Many thanks again.

    George.

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmarks & PageRef (2003)

    I incorporated Stuart's function in the version I attached in zipped form.

  14. #14
    New Lounger
    Join Date
    Dec 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bookmarks & PageRef (2003)

    Can't sleep - too excited! BUT

    I copied and pasted the revised macro into my actual working template.

    However, I'm getting a "Compile error, Expected End Function". When I click OK, it yellow highlights the line below marked with asterisks ******************

    Sorry, am I being thick, or just too tired to function?

    George.

    Sub CreateBookmarksEtc()
    Dim colHeadings As New Collection
    Dim strText As String
    Dim varItem As Variant

    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .Text = ""
    .ClearFormatting
    .Format = True
    .Wrap = wdFindStop
    .Style = wdStyleHeading4
    Do While .Execute = True
    Selection.MoveEnd Unit:=wdCharacter, Count:=-1
    strText = strBookMarkName(Selection.Text)
    colHeadings.Add Item:=Selection.Text
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.Bookmarks.Add Name:=strText
    Loop
    For Each varItem In colHeadings
    ActiveDocument.Tables(1).Columns(2).Select
    .ClearFormatting
    .Format = False
    .Wrap = wdFindStop
    .Text = varItem
    If .Execute = True Then
    Selection.TypeText Text:="See page "
    Selection.Fields.Add Range:=Selection.Range, _
    Type:=wdFieldPageRef, _
    Text:=strBookMarkName(CStr(varItem))
    End If
    Next varItem
    End With
    ActiveDocument.Tables(1).Columns(2).Select
    Selection.Fields.Update

    Set colHeadings = Nothing
    End Sub

    Private Function strBookMarkName(strInString As String) As String ****************************
    Dim strTemp As String
    Dim strChar As String
    Dim i As Integer
    For i = 1 To Len(strInString)
    strChar = Mid(strInString, i, 1)
    Select Case strChar
    Case "a" To "z", "A" To "Z"
    strTemp = strTemp & strChar
    Case "0" To "9"
    If Len(strTemp) > 1 Then strTemp = strTemp & strChar
    Case " "
    strTemp = strTemp & "_"

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bookmarks & PageRef (2003)

    You simply haven't copied the complete code. There are more lines to the function; it ends with End Function.

Page 1 of 3 123 LastLast

Posting Permissions

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