Problem With Inserting and Formatting StyleRef Fields

W

Wayne Herbert

I want to insert two styleref fields, separted by a hyphen,into a
header so that I can get a dictionary like heading for the first and
last entries on a page.

What I want to get is FIRSTWORD-LASTWORD. What I am instead getting
when I automate the process is -LASTWORDFIRSTWORD. I am clearly
missing something (quite new to this), and I don't know what.

First, I recorded a macro in Word which set up what I wanted:

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"STYLEREF FirstThree ", PreserveFormatting:=True
Selection.TypeText Text:="-"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"STYLEREF FirstThree \l ", PreserveFormatting:=True

The fields looked good on my sample, hand created document. Next I
translated this into Delphi Object Pascal calls as follows.

// select the page header
Word.ActiveWindow.ActivePane.View.SeekView :=
wdSeekCurrentPageHeader;
// write out the first styleref field
OleVar := wdFieldEmpty;
OleVar2 := WideString('STYLEREF FirstThree ');
OleVar3 := true;
Word.Selection.Fields.Add(Word.Selection.Range, OleVar, OleVar2,
OleVar3);
// write out the hyphen
Word.Selection.TypeText(WideString('-'));
// write out the second styleref field
OleVar2 := WideString('STYLEREF FirstThree \l ');
Word.Selection.Fields.Add(Word.Selection.Range, OleVar, OleVar2,
OleVar3);

However, when I execute this code, I get jumbled output.

Once I do get this straightened out, I would like to be able to select
the whole thing so that I can apply a font and bolding to the styleref
fields and the hyphen.

Pointers in the right direction would be appreciated.
 
W

Wayne Herbert

There appears to be an anomaly in Word (2002). If the styleref fields
are defined before there is an entry in the main document body that
uses the style specified in the style ref, then the fields come out
jumbled.

If there is at least one entry in the main document which uses the
style ref, then the placement of the styleref fields works properly.
Actually, just issuing the style change to the style seems to 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