StyleRef field problem in VBA

N

npacker

I have been coding a document to automate the splitting of a table and
inserting a line that captures the Heading 1 number and the Heading 1
Text plus the word continued. At the location where I want to split the
table and insert the text, I run the code and it works just how I want
it. Example:

2.0 Some Heading Text, Continued

The very next time I run the code - farther down in the table, the
exact same code produces this,

, Continued Some Heading Text2.0

The code has reversed the text.

I can insert the next Heading 1, and again the code works just fine in
the table under this new Heading 1, but reverses at the next call of
the code. In the paragraph produced at the table split, the following
code creates the fields and the text.

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="StyleRef 1 \n"
Application.Run MacroName:="Project.Called.UpdateFields"
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _

PreserveFormatting:=False
Selection.TypeText Text:="StyleRef 1\t"
Application.Run MacroName:="Project.Called.UpdateFields"
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=", Continued"
Selection.Style = ActiveDocument.Styles("TableSplitCont")

Incidently, I had it working just fine just including the Heading text,
but the client wants the Heading number to appear also.

Any ideas on how I can keep the text in the correct order? Am I missing
something with the coded StyleRef field?

Nathan
 
H

Howard Kaikow

I've not looked at the code carefully, but a quick perusal leads me to
believe that you are assuming things about how field insertions work.

In some versions of Word, there was/is a bug that causes the insertion point
to be left before, instead of after, the just inserted field. As I recall,
this occurred for fields that are not visible. This problem drove nuts a
number of years ago until I stumbled upon a KB article describing the
problem. It was a major effort to go thru my code to avoid the problem.

So your problem could be with your insertion of the empty field or some
other field.

Carefully step thru the code and watch where the insertion point ends up
after each insertion.
You'll need to make sure that the insertion point is properly located before
each insertion.
 
N

npacker

Howard,

I think you are correct about the problem. Word seems to lose its mind
when it is tracking and updating multiple field insertions on one line.
Under deadline, I removed the code that immediately updated each field
as the field is created and inserted code at the end that did a whole
document update of all fields. This ploy may not be elegant, but it did
the job, and I was able to move on.
 
H

Howard Kaikow

Howard,

I think you are correct about the problem. Word seems to lose its mind
when it is tracking and updating multiple field insertions on one line.
Under deadline, I removed the code that immediately updated each field
as the field is created and inserted code at the end that did a whole
document update of all fields. This ploy may not be elegant, but it did
the job, and I was able to move on.

It's up to you to make sure the insertion point is where it should be when
using te Selection object.
The only safe thhing to do s tp make sure hat you set the insertion point
AFTER each field insertion.
Not always necessary, but will always work.
 

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