Link to home
Start Free TrialLog in
Avatar of JasonLattin
JasonLattin

asked on

Word Macro for Insert Cross-Reference

I need some help with macros that I can use in Word 2010.

Could you help me create two macros to accomplish the steps below? If you can help with one, I may be able to tweak it to create the second version.

I plan to associate the macros with buttons and add the buttons to the Quick Access Toolbar (QAT). This will make it easier to insert the two types of cross-references.

I have tried recording a macro, but the steps aren’t recorded unless I complete the entire process, that is, select the item to be inserted as the cross-reference. I want the macro to stop before actually inserting the cross-reference, which will make it possible for me to manually select the reference to be inserted.

I also tried recording the macro to the point of actually inserting a cross-reference and then removing the unwanted steps. The removal of the steps created an error in the macro that I can’t resolve.

If I use the macros, I can limit the number of selections I have to make when I insert cross references.

Steps for Figure Cross-Reference


Insert tab. Click Cross-reference.
Reference type (select) Figure.
Insert reference to (select) Only label and number
Insert as hyperlink selected (yes)

Stop

At this point, I will manually select the appropriate figure number (heading for the next version of the macro) and click Insert.


Steps for Heading Cross-Reference


Insert Cross-reference.
Reference type (select) Heading
Insert reference to (select) Heading text
Insert as hyperlink selected (yes)

Stop


The macros should run regardless of the values selected as the reference type and the insert reference. That is, because the selected values are persisted each time the Cross-Reference dialog box is closed, running the macro should not fail when the persisted state is the same as the stated defined by the macro.



The code that I was able to record for the figure cross-reference is below.


Sub CrossRefFigure()
'
' CrossRefFigure Macro
' Opens Cross Reference dialog box. Selects the reference type FIGURE,
' insert reference to only label and number, insert as hyperlink options.
' Macro stops here for user input of the figure number to be used.
'
    Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _
        wdOnlyLabelAndNumber, ReferenceItem:="1", InsertAsHyperlink:=True, _
        IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

End Sub

I modified the code and removed the ReferenceItem:=”1” reference. The idea is to have the macro stop at this point so that I can select the figure number to be inserted. However, I got an error.

THANK YOU FOR YOUR ASSISTANCE
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Without getting into the code, can you describe what value the macro should be adding, i.e, what is it to do that the manual process doesn't?
Avatar of JasonLattin
JasonLattin

ASKER

Graham,

Thanks for your reply.

I want to automate the process of opening the Cross-Reference dialog box and selecting specific options related to reference type and the item to be referenced. I put together the attached CrossRef1.doc. Does it help?
CrossRef1.doc
This macro demonstrate opening the dialogue and shows how (theoretically) to preset the arguments.
Sub XRef()
    Dim dlg As Dialog
    
    Set dlg = Dialogs(wdDialogInsertCrossReference)
    With dlg
        .ReferenceType = "Figure"
        .ReferenceKind = wdOnlyLabelAndNumber
        '.ReferenceItem =
        .InsertAsHyperLink = False
        '.InsertPosition =
        '.SeparateNumbers =
        '.SeparatorCharacters =

        .Show '(open dialogue and perform the insertion)

        'Display '(open dialogue. Don't perform the actions, but you can use the parameter in some .InsertCrossReference code, thus:
        ' Selection.InsertCrossReference ReferenceType:=.ReferenceType, ReferenceKind:= .REferenceKind, ...
       
        '.Execute 'Just do the insertion without showing the dialogue

    End With
End Sub

Open in new window


However in my experiments (Word 2007), while InsertAsHyperLink is effective, the other settings are ignored.  When the dialogue opens most of the other settings merely reflect what was chosen previously.

If you have a lot of this work to do it might be easier to find some characteristic about the references in common (e.g.paragraph style), and to create a loop without using the dialogue. If there is no existing common property, you could highlight each one, run such a macro, and the reset the highlighting.
I see what you mean about the settings being ignored. The specific setting that is ignored but I need is .ReferenceKind = wdOnlyLabelAndNumber.

It's okay that the dialog box displays what was selected previously.

As it is, the code will be helpful if you can help me solve one problem I found in testing.

When I open the dialog box with the macro, I cannot switch to Word and work. If I use the normal functionality to open the dialog box, I can keep the dialog box open and work in Word as needed, i.e., switch between Word and the dialog box.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK. We gave it our best shot.

I was hopeful we could find a workable solution. That's just the way it goes sometimes.

I thank you for your help.
While the solution was not what I had hoped for, the effort directed toward the problem is much appreciated, and I appreciate the timely responses. It's the efforts that make the solution excellent as far as I am concerned.

Thank you again.