Strange behavior in cross-referencing

B

Bonsai Bill

I use inserts extensively so I generated a form and wrote macros to insert
figures, tables, etc. I also wrote similar macro to cross-reference the
inserts.

In the cross-referencing form I have text box to tell which figure or table
number is to be used. Everything works fine, usually. But sometimes the
numbering goes wrong when cross-referencing, either while I have Track
Changes on or have them on. For example I enter 8 for figure number and macro
inserts 4. Stepping through macro the value remains correct going to
InsertCrossReference.

If using the macro to insert caption the correct number is shown, e.g. 8.8.
But I try inserting manually, Word offers Figure 1.8! However, I can select
the correct cross-reference. I have tried exiting program, and loading
another file and then returning to problem file but that made no difference

Can someone please offer a way to avoid this strange behavior? I am using
Word XP. Thanks for your help.

The simple code for cross-referencing is:

RStr = Str(Num) ' Num and myType come from form
If myType = "f" Then
myStr = "Figure " + RStr
Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:=RStr, InsertAsHyperlink:=False, _
IncludePosition:=False
Selection.TypeText Text:=" "
FigCt = frmCrRef.tbNum.Value + 1
frmCrRef.tbNum.Value = Str(FigCt)
End If
 
M

macropod

Hi Bonsai Bill,

Two things I'd suggest that might help:
1. Turn off Track Changes while your macro is running
2. Accept the tracked changes for all existing fields.

For the former, you could use code like:
Option Explicit
Dim TrkStatus As Boolean ' Track Changes flag

MyMacro()
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
' Turn Off Screen Updating
Application.ScreenUpdating = False
'..........Your main routine goes here
' Restore original Track Changes status
.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End With
End Sub

And here's a macro for the latter:
Sub AcceptTrackedFields()
Dim oRange As Word.Range ' All Range objects - includes ranges in the body, headers, footers & shapes
Dim Fld As Field ' Field Object
With ActiveDocument
' Loop through all range objects and accept tracked changes on fields
For Each oRange In .StoryRanges
Do
For Each Fld In oRange.Fields
Fld.Select
Selection.Range.Revisions.AcceptAll
Next
Set oRange = oRange.NextStoryRange
Loop Until oRange Is Nothing
Next
End With
End Sub
 
B

Bonsai Bill

Hi macropod,

Thanks for your quick response. I had macro to update all fields with track
changes off but having fields updated didn't help. I found some strange track
change markup elsewhere after updating the fields. Once I cleared that the
macro ran okay!???

I've incorporated your suggestion about turning off track changes when
running either insertion or cross-referencing. Maybe that will eliminate
future problems.

Thanks for your help! You have been big help to me several times now and I
really appreciate it. I am sure that I will back for more help in the future.
VBA for Excel is straightforward for me but Word applications are pretty
tricky.

macropod said:
Hi Bonsai Bill,

Two things I'd suggest that might help:
1. Turn off Track Changes while your macro is running
2. Accept the tracked changes for all existing fields.

For the former, you could use code like:
Option Explicit
Dim TrkStatus As Boolean ' Track Changes flag

MyMacro()
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
' Turn Off Screen Updating
Application.ScreenUpdating = False
'..........Your main routine goes here
' Restore original Track Changes status
.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End With
End Sub

And here's a macro for the latter:
Sub AcceptTrackedFields()
Dim oRange As Word.Range ' All Range objects - includes ranges in the body, headers, footers & shapes
Dim Fld As Field ' Field Object
With ActiveDocument
' Loop through all range objects and accept tracked changes on fields
For Each oRange In .StoryRanges
Do
For Each Fld In oRange.Fields
Fld.Select
Selection.Range.Revisions.AcceptAll
Next
Set oRange = oRange.NextStoryRange
Loop Until oRange Is Nothing
Next
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Bonsai Bill said:
I use inserts extensively so I generated a form and wrote macros to insert
figures, tables, etc. I also wrote similar macro to cross-reference the
inserts.

In the cross-referencing form I have text box to tell which figure or table
number is to be used. Everything works fine, usually. But sometimes the
numbering goes wrong when cross-referencing, either while I have Track
Changes on or have them on. For example I enter 8 for figure number and macro
inserts 4. Stepping through macro the value remains correct going to
InsertCrossReference.

If using the macro to insert caption the correct number is shown, e.g. 8.8.
But I try inserting manually, Word offers Figure 1.8! However, I can select
the correct cross-reference. I have tried exiting program, and loading
another file and then returning to problem file but that made no difference

Can someone please offer a way to avoid this strange behavior? I am using
Word XP. Thanks for your help.

The simple code for cross-referencing is:

RStr = Str(Num) ' Num and myType come from form
If myType = "f" Then
myStr = "Figure " + RStr
Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:=RStr, InsertAsHyperlink:=False, _
IncludePosition:=False
Selection.TypeText Text:=" "
FigCt = frmCrRef.tbNum.Value + 1
frmCrRef.tbNum.Value = Str(FigCt)
End If
 
B

Bonsai Bill

Hi Macropod,

Are you still checking this posting? I hope so because I am stuck on updating.

Wrote following code to update only caption and ref fields because other
object fields where I don't have source file get wiped out upon updating.


Max = ActiveDocument.Fields.Count
I = 1
For Each aField In ActiveDocument.Fields
If (ActiveDocument.Fields(I).Type) = 12 Then ' caption field
aField.Select
Selection.Range.Revisions.AcceptAll
End If '(ActiveDocument.Fields(I).Type) = 12

If (ActiveDocument.Fields(I).Type) = 3 Then ' cross-ref field
aField.LinkFormat.Update
' aField.Select
' Selection.Range.Revisions.AcceptAll
End If ' (ActiveDocument.Fields(I).Type) = 3
I = I + 1
If I > Max Then Exit For
Next aField

I tried both LinkFormat.Update and AcceptAll but code above doesn't update
equation numbers in caption nor cross-ref. Any suggestions?
Bonsai Bill said:
Hi macropod,

Thanks for your quick response. I had macro to update all fields with track
changes off but having fields updated didn't help. I found some strange track
change markup elsewhere after updating the fields. Once I cleared that the
macro ran okay!???

I've incorporated your suggestion about turning off track changes when
running either insertion or cross-referencing. Maybe that will eliminate
future problems.

Thanks for your help! You have been big help to me several times now and I
really appreciate it. I am sure that I will back for more help in the future.
VBA for Excel is straightforward for me but Word applications are pretty
tricky.

macropod said:
Hi Bonsai Bill,

Two things I'd suggest that might help:
1. Turn off Track Changes while your macro is running
2. Accept the tracked changes for all existing fields.

For the former, you could use code like:
Option Explicit
Dim TrkStatus As Boolean ' Track Changes flag

MyMacro()
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
' Turn Off Screen Updating
Application.ScreenUpdating = False
'..........Your main routine goes here
' Restore original Track Changes status
.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End With
End Sub

And here's a macro for the latter:
Sub AcceptTrackedFields()
Dim oRange As Word.Range ' All Range objects - includes ranges in the body, headers, footers & shapes
Dim Fld As Field ' Field Object
With ActiveDocument
' Loop through all range objects and accept tracked changes on fields
For Each oRange In .StoryRanges
Do
For Each Fld In oRange.Fields
Fld.Select
Selection.Range.Revisions.AcceptAll
Next
Set oRange = oRange.NextStoryRange
Loop Until oRange Is Nothing
Next
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


Bonsai Bill said:
I use inserts extensively so I generated a form and wrote macros to insert
figures, tables, etc. I also wrote similar macro to cross-reference the
inserts.

In the cross-referencing form I have text box to tell which figure or table
number is to be used. Everything works fine, usually. But sometimes the
numbering goes wrong when cross-referencing, either while I have Track
Changes on or have them on. For example I enter 8 for figure number and macro
inserts 4. Stepping through macro the value remains correct going to
InsertCrossReference.

If using the macro to insert caption the correct number is shown, e.g. 8.8.
But I try inserting manually, Word offers Figure 1.8! However, I can select
the correct cross-reference. I have tried exiting program, and loading
another file and then returning to problem file but that made no difference

Can someone please offer a way to avoid this strange behavior? I am using
Word XP. Thanks for your help.

The simple code for cross-referencing is:

RStr = Str(Num) ' Num and myType come from form
If myType = "f" Then
myStr = "Figure " + RStr
Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:=RStr, InsertAsHyperlink:=False, _
IncludePosition:=False
Selection.TypeText Text:=" "
FigCt = frmCrRef.tbNum.Value + 1
frmCrRef.tbNum.Value = Str(FigCt)
End If
 
M

macropod

Hi Bonsai Bill,

For the fields to which you don't have access the source file, the solution would be to lock them, rather than update them. But, if
you do that, you'll need to remember to unlock them again before sending the fiel back to whoever has the source files - otherwise
the document still won't update to reflect any changes in those files.

To lock a field you'd use code to test either the source app or the source filename and then, if the field matches your criteria,
lock it.

--
Cheers
macropod
[MVP - Microsoft Word]


Bonsai Bill said:
Hi Macropod,

Are you still checking this posting? I hope so because I am stuck on updating.

Wrote following code to update only caption and ref fields because other
object fields where I don't have source file get wiped out upon updating.


Max = ActiveDocument.Fields.Count
I = 1
For Each aField In ActiveDocument.Fields
If (ActiveDocument.Fields(I).Type) = 12 Then ' caption field
aField.Select
Selection.Range.Revisions.AcceptAll
End If '(ActiveDocument.Fields(I).Type) = 12

If (ActiveDocument.Fields(I).Type) = 3 Then ' cross-ref field
aField.LinkFormat.Update
' aField.Select
' Selection.Range.Revisions.AcceptAll
End If ' (ActiveDocument.Fields(I).Type) = 3
I = I + 1
If I > Max Then Exit For
Next aField

I tried both LinkFormat.Update and AcceptAll but code above doesn't update
equation numbers in caption nor cross-ref. Any suggestions?
Bonsai Bill said:
Hi macropod,

Thanks for your quick response. I had macro to update all fields with track
changes off but having fields updated didn't help. I found some strange track
change markup elsewhere after updating the fields. Once I cleared that the
macro ran okay!???

I've incorporated your suggestion about turning off track changes when
running either insertion or cross-referencing. Maybe that will eliminate
future problems.

Thanks for your help! You have been big help to me several times now and I
really appreciate it. I am sure that I will back for more help in the future.
VBA for Excel is straightforward for me but Word applications are pretty
tricky.

macropod said:
Hi Bonsai Bill,

Two things I'd suggest that might help:
1. Turn off Track Changes while your macro is running
2. Accept the tracked changes for all existing fields.

For the former, you could use code like:
Option Explicit
Dim TrkStatus As Boolean ' Track Changes flag

MyMacro()
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
' Turn Off Screen Updating
Application.ScreenUpdating = False
'..........Your main routine goes here
' Restore original Track Changes status
.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End With
End Sub

And here's a macro for the latter:
Sub AcceptTrackedFields()
Dim oRange As Word.Range ' All Range objects - includes ranges in the body, headers, footers & shapes
Dim Fld As Field ' Field Object
With ActiveDocument
' Loop through all range objects and accept tracked changes on fields
For Each oRange In .StoryRanges
Do
For Each Fld In oRange.Fields
Fld.Select
Selection.Range.Revisions.AcceptAll
Next
Set oRange = oRange.NextStoryRange
Loop Until oRange Is Nothing
Next
End With
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


I use inserts extensively so I generated a form and wrote macros to insert
figures, tables, etc. I also wrote similar macro to cross-reference the
inserts.

In the cross-referencing form I have text box to tell which figure or table
number is to be used. Everything works fine, usually. But sometimes the
numbering goes wrong when cross-referencing, either while I have Track
Changes on or have them on. For example I enter 8 for figure number and macro
inserts 4. Stepping through macro the value remains correct going to
InsertCrossReference.

If using the macro to insert caption the correct number is shown, e.g. 8.8.
But I try inserting manually, Word offers Figure 1.8! However, I can select
the correct cross-reference. I have tried exiting program, and loading
another file and then returning to problem file but that made no difference

Can someone please offer a way to avoid this strange behavior? I am using
Word XP. Thanks for your help.

The simple code for cross-referencing is:

RStr = Str(Num) ' Num and myType come from form
If myType = "f" Then
myStr = "Figure " + RStr
Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:=RStr, InsertAsHyperlink:=False, _
IncludePosition:=False
Selection.TypeText Text:=" "
FigCt = frmCrRef.tbNum.Value + 1
frmCrRef.tbNum.Value = Str(FigCt)
End If
 

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