Cross Reference Menu from Within a Macro

P

Pehr

I have written, by manipulating a recorded macro, the following macro that is
designed to keep the text in a window from moving after inserting a
cross-reference. That to avoid a very annoying behaviour of MS Word to move
the text in a window when a cross-reference is inserted.

The only problem is that the macro only inserts the paragraph number "1".
That is what was picked up form the recorded macro. It is the statement
'ReferenceItem:="1"' that causes that behaviour. I want to change it so that
I can tell the macro which paragraph number to insert the refernce to, e.g.,
by putting up the cross reference menu and then returning the paragraph
number. How is that done?

Here is the macro:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 8/22/06 by Pehr Jansson July 21 2006
'
currentscroll = ActiveWindow.ActivePane.VerticalPercentScrolled
currentdoc = ActiveWindow.Document
ReferenceItemToInsert =
ActiveWindow.Document.GetCrossReferenceItems(wdRefTypeNumberedItem)

Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberRelativeContext, ReferenceItem:="1", _
InsertAsHyperlink:=True, IncludePosition:=False
ActiveWindow.ActivePane.VerticalPercentScrolled = currentscroll
End Sub
 
J

Jean-Guy Marcil

Pehr was telling us:
Pehr nous racontait que :
I have written, by manipulating a recorded macro, the following macro
that is designed to keep the text in a window from moving after
inserting a cross-reference. That to avoid a very annoying behaviour
of MS Word to move the text in a window when a cross-reference is
inserted.

The only problem is that the macro only inserts the paragraph number
"1". That is what was picked up form the recorded macro. It is the
statement 'ReferenceItem:="1"' that causes that behaviour. I want to
change it so that I can tell the macro which paragraph number to
insert the refernce to, e.g., by putting up the cross reference menu
and then returning the paragraph number. How is that done?

Here is the macro:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 8/22/06 by Pehr Jansson July 21 2006
'
currentscroll = ActiveWindow.ActivePane.VerticalPercentScrolled
currentdoc = ActiveWindow.Document
ReferenceItemToInsert =
ActiveWindow.Document.GetCrossReferenceItems(wdRefTypeNumberedItem)

Selection.InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberRelativeContext, ReferenceItem:="1", _
InsertAsHyperlink:=True, IncludePosition:=False
ActiveWindow.ActivePane.VerticalPercentScrolled = currentscroll
End Sub

Try:

'_______________________________________
Dim currentscroll As Long

currentscroll = ActiveWindow.ActivePane.VerticalPercentScrolled

Dialogs(wdDialogInsertCrossReference).Show

ActiveWindow.ActivePane.VerticalPercentScrolled = currentscroll
'_______________________________________

By the way, it is always better to declare variables with Dim statements and
to avoid unnecessary statements in the code, for example, you originally
had:
currentdoc = ActiveWindow.Document
but currentdoc is not used elsewhere in the code (unless you posted only a
part of your code).

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Pehr

Jean-Guy Marcil said:
Pehr was telling us:
Pehr nous racontait que :


Try:

'_______________________________________
Dim currentscroll As Long

currentscroll = ActiveWindow.ActivePane.VerticalPercentScrolled

Dialogs(wdDialogInsertCrossReference).Show

ActiveWindow.ActivePane.VerticalPercentScrolled = currentscroll
'_______________________________________

By the way, it is always better to declare variables with Dim statements and
to avoid unnecessary statements in the code, for example, you originally
had:
currentdoc = ActiveWindow.Document
but currentdoc is not used elsewhere in the code (unless you posted only a
part of your code).

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org

Jean-Guy Merci Beaucoup!

that worked.

One question though. It leaves the dialog box open and active after I click
on "Insert". Any way to make it close the dialog box?

Many thanks.

BTW: when reading my own post I realized I had left some remnants in from my
experimentation. However, I make note of your recommendation to declare
variables.

Pehr
 
J

Jean-Guy Marcil

Pehr was telling us:
Pehr nous racontait que :
One question though. It leaves the dialog box open and active after
I click on "Insert". Any way to make it close the dialog box?

Not with code because when you use the Dialogs....Show line, this give the
focus to the dialog and the macro only gets it back after the dialog is
closed.
So the user has to hit the "Close" button.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Russ

Pehr,
You can declare a variable:
Dim myReferenceItem as Long
'Set it equal to a number such as...
myReferenceItem = 5

....And replace the hard coded 'ReferenceItem:="1"' that is in your original
recorded macro with the variable such as 'ReferenceItem:=
Cstr(myReferenceItem)' without the single quotes.

I will often record a macro and alter it to use variables to make the result
less hard coded and more versatile. In this case you can create multiple
cross references without opening the dialog box, as long as you know in
advance which "Items"(numbers) you want to reference.
Was that what you were trying to accomplish?
 
R

Russ

Pehr,
I now see that your recorded macro has an array variable:
ReferenceItemToInsert.
You could also substitute one of the items of the array like
'ReferenceItem:= ReferenceItemToInsert(3)' in the InsertCrossReference
statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top