PDA

View Full Version : [SOLVED:] Insert Cross Reference Dialog



Paul_Hossler
05-31-2016, 08:16 AM
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

SamT
05-31-2016, 01:54 PM
Record a macro. That will give all possible parameters. Use all of them in your code.

Paul_Hossler
05-31-2016, 02:18 PM
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

16297

SamT
05-31-2016, 05:28 PM
Researching:

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

A rather long discussion of this frustrating issue: http://answers.microsoft.com/en-us/office/forum/officeversion_other-word/changing-the-cross-referencing-default-to-label/18390a17-4edd-46ed-b6f5-851466df6c18?page=1

Another failed attempt: http://answers.microsoft.com/en-us/office/forum/office_2010-customize/create-a-macro-for-cross-references/6de38e33-a1e0-4a04-93da-55d7d9a1de4c?tab=question&status=AllReplies#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 (https://msdn.microsoft.com/en-us/library/office/ff821703.aspx) method to execute the settings in a dialog box without displaying the dialog box.

And This:
Note Use the Update (https://msdn.microsoft.com/en-us/library/office/ff821385.aspx) 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 (https://msdn.microsoft.com/en-us/library/office/ff840767.aspx) object, you need to identify the individual dialog box. This is done by using the Dialogs (https://msdn.microsoft.com/en-us/library/office/ff191958.aspx) property with a WdWordDialog (https://msdn.microsoft.com/en-us/library/office/ff836540.aspx) 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.

Paul_Hossler
05-31-2016, 06:25 PM
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

SamT
05-31-2016, 06:43 PM
Can you:

This = InputBox(...)
This.InsertCrossReference ReferenceType:="Fi

Paul_Hossler
05-31-2016, 06:55 PM
.ReferenceType seems to work reliabily ( = 'Figure')

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

SamT
05-31-2016, 08:44 PM
Whew. Glad you got it.

Now you need to tell those Microsoft forums.

Ya wanna share the final code?

gmayor
05-31-2016, 09:34 PM
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/office/forum/officeversion_other-word/changing-the-cross-referencing-default-to-label/18390a17-4edd-46ed-b6f5-851466df6c18?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

Paul_Hossler
06-01-2016, 05:54 AM
@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

SamT
06-01-2016, 06:22 AM
the number of {TAB}Up, Up, Up. . . then down as required?