Consulting

Results 1 to 11 of 11

Thread: Insert Cross Reference Dialog

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location

    Insert Cross Reference Dialog

    In Word 2016, I have a simple macro tied to a ribbon tab to .Show the Insert Cross References dialog with some preset selections:

    'Figure'
    'Label and Number'

    so that the first time it displays the 2 above choices are set (most common case)

    However, the last used Reference Kind is always selected. So it might be correct (or the way I want it) the first time, but if I select 'Page Number' as an option (occasionally) the next time the macro/dialog remembers that and I have to change it

    How do I clear previous choices and get a fresh start using the parameters within the macro?


    Sub InsertCrossReferenceFigure()
        Dim iButton As Long
        Selection.Collapse
        'https://msdn.microsoft.com/en-us/library/office/ff845471.aspx
        'ReferenceType , ReferenceKind, ReferenceItem, InsertAsHyperLink, InsertPosition, SeparateNumbers, SeparatorCharacters
        With Application.Dialogs(wdDialogInsertCrossReference)
            .ReferenceType = "Figure"
             .ReferenceKind = wdOnlyLabelAndNumber
            .InsertAsHyperlink = 0
            iButton = .Show     '   always -2
        End With
    End Sub
    Last edited by Paul_Hossler; 05-31-2016 at 11:23 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Record a macro. That will give all possible parameters. Use all of them in your code.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Did that

    It doesn't

    1. I want the dialog box to display so I can choose which Figure and what aspects of the Figure to include in the cross ref

    Application.Dialogs(wdDialogInsertCrossReference)

    2. with my choices for the dropdowns

            .ReferenceType = "Figure" 
            .ReferenceKind = wdOnlyLabelAndNumber 
            .InsertAsHyperlink = 0
    3. The (some of) dropdown choices are remembered from one call to the macro to another, and not set by the Dialogs call

    a. Run the macro, and select [Only Label and Number] if necessary (should be selected by macro), [Insert], and [Close]
    b. Run the macro, and select [Page Number], [Insert], and [Close]
    c. Run the macro, and [Page Number] is still selected. The wdOnlyLabelAndNumber is ignored

    d. This is after the third call. [Page Number] is still selected

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Researching:

    Possible arguments
    ReferenceType , ReferenceKind, ReferenceItem, InsertAsHyperLink, InsertPosition, SeparateNumbers, SeparatorCharacters

    A rather long discussion of this frustrating issue: http://answers.microsoft.com/en-us/o...6df6c18?page=1

    Another failed attempt: http://answers.microsoft.com/en-us/o...llReplies#tabs

    Several people say
    With Dialogs(wdDialogInsertCrossReference) 
         SendKeys "%t{Down 2}%r{Down 2}{ENTER}" 
         .Show 
     End With
    I came across this for you to experiment with:
    Use the Execute method to execute the settings in a dialog box without displaying the dialog box.

    And This:
    Note Use the Update method to ensure that the dialog box values reflect the current values. It may be necessary to use the Update method if you define a dialog box variable early in your macro and later want to return or change the current settings.


    This:
    Prior to returning or changing a dialog box setting using the Dialog object, you need to identify the individual dialog box. This is done by using the Dialogs property with a WdWordDialog constant. After you have instantiated a Dialog object, you can return or set options in the dialog box. The following example displays the right indent from the Paragraphs dialog box. (Font = red by me)
                         
     Sub ShowRightIndent() 
     Dim dlgParagraph As Dialog 
     Set dlgParagraph = Dialogs(wdDialogFormatParagraph) 
     MsgBox "Right indent = " & dlgParagraph.RightIndent 
    End Sub




    My thoughts: I would try Instantiating an Object variable to the Dialog, Setting the parameters as desired, Updating it, then Showing or Executing it.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    I did try SendKeys (not reliable and changes NumLock)

    I tried .Update in several places in the InsertCrossReferenceFigure macro and it didn't seem to make any difference, the ReferenceKind was remembered

    Macro2 is recorded with my choices and can be run correctly every time, BUT I can't select the item to cross reference, so it really doesn't help


    Sub InsertCrossReferenceFigure()
        Dim oDialog As Dialog
        Selection.Collapse
        'https://msdn.microsoft.com/en-us/library/office/ff845471.aspx
        'ReferenceType , ReferenceKind, ReferenceItem, InsertAsHyperLink, InsertPosition, SeparateNumbers, SeparatorCharacters
        Set oDialog = Application.Dialogs(wdDialogInsertCrossReference)
        With oDialog
            .ReferenceType = "Figure"
            .ReferenceKind = wdOnlyLabelAndNumber
            .InsertAsHyperlink = 0
            .Show
        End With
    End Sub
    
    Sub Macro2()
        Selection.InsertCrossReference ReferenceType:="Figure", _
            ReferenceKind:=wdOnlyLabelAndNumber, _
            ReferenceItem:="1", InsertAsHyperlink:=False, _
            IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Can you:
    This = InputBox(...)
    This.InsertCrossReference ReferenceType:="Fi
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    .ReferenceType seems to work reliabily ( = 'Figure')

    .ReferenceKind seems to remember what was there instead of the macro preset
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Whew. Glad you got it.

    Now you need to tell those Microsoft forums.

    Ya wanna share the final code?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    The general consensus is that this VBA function has been broken for some time and has never been fixed. There is an old thread about it at http://answers.microsoft.com/en-us/o...6df6c18?page=2 . Sendkeys seems the only viable solution e.g.

    Sub InsertCrossReferenceFigure()
        Dim oDialog As Dialog
        Selection.Collapse 0
        Set oDialog = Application.Dialogs(wdDialogInsertCrossReference)
        With oDialog
            .ReferenceType = "Figure"
            SendKeys "{TAB}{DOWN 2}{TAB}"
            .InsertAsHyperlink = 0
            .Show
        End With
    End Sub
    Last edited by gmayor; 05-31-2016 at 09:45 PM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    @SamT and Graham

    The SendKeys workaround seems unreliable and inconsistent also.

    a. Since the ReferenceKind from the previous use was remembered, the number of {TAB} is also going to vary to get back the [Label and Number]

    b. Keeps flipping NumLock status


    I have 2 x-ref macros, one for Tables and one for Figures (just for Ribbon convenience). Both call the dialog, and both display the same issue, but ReferenceType works in both of the macros, and ReferenceKind doesn't.

    At least MS Word is consistent


    I'll just have to live with it
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    the number of {TAB}
    Up, Up, Up. . . then down as required?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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