Modify content of a Field Code

R

Ron

Using Word 2007, with templates in 2003 format that contain Field Codes
created by the user: { = myBookmark \# $#,##0.00 }

Using .Net C#, to iterate the Fields collection and replace "myBookmark"
with a value retrieved from an external source. The idea is to replace the
bookmark, but retain the user's desired formatting.

Problem is that when the user creates the field code and uses the "Formula"
dialog, Word creates the Field Code as: { { = myBookmark \# $#,##0.00 } }.
[Embedded FC w/n FC]. The bookmark is replaced, but upon opening the output
document {10000} is displayed after toggling the field codes TWICE.

Code used:
foreach (Microsoft.Office.Interop.Word.Field thisField in oWordDoc.Fields)
{
thisRange = thisField.Code;
if (thisField.Type == Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty)
{
// bypass empty field code
}
else
{
thisRange.Text = thisRange.Text.Replace("`myBookmark", "10000");
thisField.Update();
}
}

Is there an approach to handle the outer field code properly?
 
G

Graham Mayor

I am not familiar with the syntax for .Net C# however there are several ways
of expressing the same formatted bookmark in Word eg

{ = myBookmark \# $#,##0.00 }
{ myBookmark \# $,0.00 }
{ Ref myBookmark \# $,0.00}
{ ={ myBookmark } \# $,0.00 }
{ ={ Ref myBookmark } \# $,0.00 }

The = (expression field) is not necessary to resolve or format a bookmarked
reference.

Using Word vba, which you can probably adapt (or at least be pointed along a
route you can follow), I would probably handle this as follows:

Dim sSw() As String
With ActiveDocument
For i = .Fields.Count To 1 Step -1
If .Fields(i).Type = wdFieldExpression Then
sSw = Split(.Fields(i).Code, "\#")
.Fields(i).Code.Text = "=10000" & " \#" & sSw(1)
End If
Next i
.Fields.Update
End With

where 10000 is the number with which you wish to replace the bookmark and
sSW(1) is the formatting switch from that field. Thus in the above examples
you would get the following results:

{=10000 \# $#,##0.00 }
{ myBookmark \# $,0.00 } (not changed)
{ Ref myBookmark \# $,0.00} (not changed)
{=10000 \# $,0.00 }
{=10000 \# $,0.00 }

respectively, which would translate to the following results:

$10,000.00
$1,000.00 (not changed)
$1,000.00 (not changed)
$10,000.00
$10,000.00

I don't know if this helps :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Ron

Graham -- thank you for the quick response, I'll give this idea a go.

Using Word 2007, the Field Code seems to be created in two different formats:

1) { myBookmark \# $#,###.00 } ... this is how the FC looks in the
document when it is created using: Ctrl + F9 / Right-click / Edit Field/
[Field Codes] button / and then in the Field Codes entry typing: myBookmark
\#, etc.

2) { { myBookmark \# $#,###.00 }} ... this is the result of the FC in the
document when it is created using: Ctrl + F9 / Right-click / Edit Field /
[Formula] button / and typing myBookmark in the Formula textbox, and
selecting the mask from the Number Format dropdown.

Would you have any idea as to why the nested { { } } entry is created?

Hopefully your code idea will get me around the nested entry.

thanks again,

--
Ron


Graham Mayor said:
I am not familiar with the syntax for .Net C# however there are several ways
of expressing the same formatted bookmark in Word eg

{ = myBookmark \# $#,##0.00 }
{ myBookmark \# $,0.00 }
{ Ref myBookmark \# $,0.00}
{ ={ myBookmark } \# $,0.00 }
{ ={ Ref myBookmark } \# $,0.00 }

The = (expression field) is not necessary to resolve or format a bookmarked
reference.

Using Word vba, which you can probably adapt (or at least be pointed along a
route you can follow), I would probably handle this as follows:

Dim sSw() As String
With ActiveDocument
For i = .Fields.Count To 1 Step -1
If .Fields(i).Type = wdFieldExpression Then
sSw = Split(.Fields(i).Code, "\#")
.Fields(i).Code.Text = "=10000" & " \#" & sSw(1)
End If
Next i
.Fields.Update
End With

where 10000 is the number with which you wish to replace the bookmark and
sSW(1) is the formatting switch from that field. Thus in the above examples
you would get the following results:

{=10000 \# $#,##0.00 }
{ myBookmark \# $,0.00 } (not changed)
{ Ref myBookmark \# $,0.00} (not changed)
{=10000 \# $,0.00 }
{=10000 \# $,0.00 }

respectively, which would translate to the following results:

$10,000.00
$1,000.00 (not changed)
$1,000.00 (not changed)
$10,000.00
$10,000.00

I don't know if this helps :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Using Word 2007, with templates in 2003 format that contain Field
Codes created by the user: { = myBookmark \# $#,##0.00 }

Using .Net C#, to iterate the Fields collection and replace
"myBookmark" with a value retrieved from an external source. The
idea is to replace the bookmark, but retain the user's desired
formatting.

Problem is that when the user creates the field code and uses the
"Formula" dialog, Word creates the Field Code as: { { = myBookmark
\# $#,##0.00 } }. [Embedded FC w/n FC]. The bookmark is replaced,
but upon opening the output document {10000} is displayed after
toggling the field codes TWICE.

Code used:
foreach (Microsoft.Office.Interop.Word.Field thisField in
oWordDoc.Fields) {
thisRange = thisField.Code;
if (thisField.Type ==
Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty) {
// bypass empty field code
}
else
{
thisRange.Text = thisRange.Text.Replace("`myBookmark", "10000");
thisField.Update();
}
}

Is there an approach to handle the outer field code properly?
 
G

Graham Mayor

If you are entering fields this way, then CTRL+F9 creates a set of field
boundaries (an empty field). When you use edit field from the right click
option then select a field type, you are essentially inserting another field
into that empty field, hence the nested entry.
Either type in the field content between the CTRL+F9 brackets or use Insert
Document Parts > Field to inert the field directly on to the page without
first pressing CTRL+F9. (you can add that command to the QAT).

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Graham -- thank you for the quick response, I'll give this idea a go.

Using Word 2007, the Field Code seems to be created in two different
formats:

1) { myBookmark \# $#,###.00 } ... this is how the FC looks in the
document when it is created using: Ctrl + F9 / Right-click / Edit
Field/ [Field Codes] button / and then in the Field Codes entry
typing: myBookmark \#, etc.

2) { { myBookmark \# $#,###.00 }} ... this is the result of the FC in
the document when it is created using: Ctrl + F9 / Right-click / Edit
Field / [Formula] button / and typing myBookmark in the Formula
textbox, and selecting the mask from the Number Format dropdown.

Would you have any idea as to why the nested { { } } entry is created?

Hopefully your code idea will get me around the nested entry.

thanks again,

I am not familiar with the syntax for .Net C# however there are
several ways of expressing the same formatted bookmark in Word eg

{ = myBookmark \# $#,##0.00 }
{ myBookmark \# $,0.00 }
{ Ref myBookmark \# $,0.00}
{ ={ myBookmark } \# $,0.00 }
{ ={ Ref myBookmark } \# $,0.00 }

The = (expression field) is not necessary to resolve or format a
bookmarked reference.

Using Word vba, which you can probably adapt (or at least be pointed
along a route you can follow), I would probably handle this as
follows:

Dim sSw() As String
With ActiveDocument
For i = .Fields.Count To 1 Step -1
If .Fields(i).Type = wdFieldExpression Then
sSw = Split(.Fields(i).Code, "\#")
.Fields(i).Code.Text = "=10000" & " \#" & sSw(1)
End If
Next i
.Fields.Update
End With

where 10000 is the number with which you wish to replace the
bookmark and sSW(1) is the formatting switch from that field. Thus
in the above examples you would get the following results:

{=10000 \# $#,##0.00 }
{ myBookmark \# $,0.00 } (not changed)
{ Ref myBookmark \# $,0.00} (not changed)
{=10000 \# $,0.00 }
{=10000 \# $,0.00 }

respectively, which would translate to the following results:

$10,000.00
$1,000.00 (not changed)
$1,000.00 (not changed)
$10,000.00
$10,000.00

I don't know if this helps :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Using Word 2007, with templates in 2003 format that contain Field
Codes created by the user: { = myBookmark \# $#,##0.00 }

Using .Net C#, to iterate the Fields collection and replace
"myBookmark" with a value retrieved from an external source. The
idea is to replace the bookmark, but retain the user's desired
formatting.

Problem is that when the user creates the field code and uses the
"Formula" dialog, Word creates the Field Code as: { { = myBookmark
\# $#,##0.00 } }. [Embedded FC w/n FC]. The bookmark is replaced,
but upon opening the output document {10000} is displayed after
toggling the field codes TWICE.

Code used:
foreach (Microsoft.Office.Interop.Word.Field thisField in
oWordDoc.Fields) {
thisRange = thisField.Code;
if (thisField.Type ==
Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty) {
// bypass empty field code
}
else
{
thisRange.Text = thisRange.Text.Replace("`myBookmark", "10000");
thisField.Update();
}
}

Is there an approach to handle the outer field code properly?
 
R

Ron

Graham,

Thanks for all your advice, it has helped a great deal.

kind regards,

--
Ron


Graham Mayor said:
If you are entering fields this way, then CTRL+F9 creates a set of field
boundaries (an empty field). When you use edit field from the right click
option then select a field type, you are essentially inserting another field
into that empty field, hence the nested entry.
Either type in the field content between the CTRL+F9 brackets or use Insert
Document Parts > Field to inert the field directly on to the page without
first pressing CTRL+F9. (you can add that command to the QAT).

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Graham -- thank you for the quick response, I'll give this idea a go.

Using Word 2007, the Field Code seems to be created in two different
formats:

1) { myBookmark \# $#,###.00 } ... this is how the FC looks in the
document when it is created using: Ctrl + F9 / Right-click / Edit
Field/ [Field Codes] button / and then in the Field Codes entry
typing: myBookmark \#, etc.

2) { { myBookmark \# $#,###.00 }} ... this is the result of the FC in
the document when it is created using: Ctrl + F9 / Right-click / Edit
Field / [Formula] button / and typing myBookmark in the Formula
textbox, and selecting the mask from the Number Format dropdown.

Would you have any idea as to why the nested { { } } entry is created?

Hopefully your code idea will get me around the nested entry.

thanks again,

I am not familiar with the syntax for .Net C# however there are
several ways of expressing the same formatted bookmark in Word eg

{ = myBookmark \# $#,##0.00 }
{ myBookmark \# $,0.00 }
{ Ref myBookmark \# $,0.00}
{ ={ myBookmark } \# $,0.00 }
{ ={ Ref myBookmark } \# $,0.00 }

The = (expression field) is not necessary to resolve or format a
bookmarked reference.

Using Word vba, which you can probably adapt (or at least be pointed
along a route you can follow), I would probably handle this as
follows:

Dim sSw() As String
With ActiveDocument
For i = .Fields.Count To 1 Step -1
If .Fields(i).Type = wdFieldExpression Then
sSw = Split(.Fields(i).Code, "\#")
.Fields(i).Code.Text = "=10000" & " \#" & sSw(1)
End If
Next i
.Fields.Update
End With

where 10000 is the number with which you wish to replace the
bookmark and sSW(1) is the formatting switch from that field. Thus
in the above examples you would get the following results:

{=10000 \# $#,##0.00 }
{ myBookmark \# $,0.00 } (not changed)
{ Ref myBookmark \# $,0.00} (not changed)
{=10000 \# $,0.00 }
{=10000 \# $,0.00 }

respectively, which would translate to the following results:

$10,000.00
$1,000.00 (not changed)
$1,000.00 (not changed)
$10,000.00
$10,000.00

I don't know if this helps :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Ron wrote:
Using Word 2007, with templates in 2003 format that contain Field
Codes created by the user: { = myBookmark \# $#,##0.00 }

Using .Net C#, to iterate the Fields collection and replace
"myBookmark" with a value retrieved from an external source. The
idea is to replace the bookmark, but retain the user's desired
formatting.

Problem is that when the user creates the field code and uses the
"Formula" dialog, Word creates the Field Code as: { { = myBookmark
\# $#,##0.00 } }. [Embedded FC w/n FC]. The bookmark is replaced,
but upon opening the output document {10000} is displayed after
toggling the field codes TWICE.

Code used:
foreach (Microsoft.Office.Interop.Word.Field thisField in
oWordDoc.Fields) {
thisRange = thisField.Code;
if (thisField.Type ==
Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty) {
// bypass empty field code
}
else
{
thisRange.Text = thisRange.Text.Replace("`myBookmark", "10000");
thisField.Update();
}
}

Is there an approach to handle the outer field code properly?
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Graham,

Thanks for all your advice, it has helped a great deal.

kind regards,

If you are entering fields this way, then CTRL+F9 creates a set of
field boundaries (an empty field). When you use edit field from the
right click option then select a field type, you are essentially
inserting another field into that empty field, hence the nested
entry.
Either type in the field content between the CTRL+F9 brackets or use
Insert > Document Parts > Field to inert the field directly on to
the page without first pressing CTRL+F9. (you can add that command
to the QAT).

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Graham -- thank you for the quick response, I'll give this idea a
go.

Using Word 2007, the Field Code seems to be created in two different
formats:

1) { myBookmark \# $#,###.00 } ... this is how the FC looks in the
document when it is created using: Ctrl + F9 / Right-click / Edit
Field/ [Field Codes] button / and then in the Field Codes entry
typing: myBookmark \#, etc.

2) { { myBookmark \# $#,###.00 }} ... this is the result of the FC
in the document when it is created using: Ctrl + F9 / Right-click /
Edit Field / [Formula] button / and typing myBookmark in the Formula
textbox, and selecting the mask from the Number Format dropdown.

Would you have any idea as to why the nested { { } } entry is
created?

Hopefully your code idea will get me around the nested entry.

thanks again,


I am not familiar with the syntax for .Net C# however there are
several ways of expressing the same formatted bookmark in Word eg

{ = myBookmark \# $#,##0.00 }
{ myBookmark \# $,0.00 }
{ Ref myBookmark \# $,0.00}
{ ={ myBookmark } \# $,0.00 }
{ ={ Ref myBookmark } \# $,0.00 }

The = (expression field) is not necessary to resolve or format a
bookmarked reference.

Using Word vba, which you can probably adapt (or at least be
pointed along a route you can follow), I would probably handle
this as follows:

Dim sSw() As String
With ActiveDocument
For i = .Fields.Count To 1 Step -1
If .Fields(i).Type = wdFieldExpression Then
sSw = Split(.Fields(i).Code, "\#")
.Fields(i).Code.Text = "=10000" & " \#" & sSw(1)
End If
Next i
.Fields.Update
End With

where 10000 is the number with which you wish to replace the
bookmark and sSW(1) is the formatting switch from that field. Thus
in the above examples you would get the following results:

{=10000 \# $#,##0.00 }
{ myBookmark \# $,0.00 } (not changed)
{ Ref myBookmark \# $,0.00} (not changed)
{=10000 \# $,0.00 }
{=10000 \# $,0.00 }

respectively, which would translate to the following results:

$10,000.00
$1,000.00 (not changed)
$1,000.00 (not changed)
$10,000.00
$10,000.00

I don't know if this helps :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Ron wrote:
Using Word 2007, with templates in 2003 format that contain Field
Codes created by the user: { = myBookmark \# $#,##0.00 }

Using .Net C#, to iterate the Fields collection and replace
"myBookmark" with a value retrieved from an external source. The
idea is to replace the bookmark, but retain the user's desired
formatting.

Problem is that when the user creates the field code and uses the
"Formula" dialog, Word creates the Field Code as: { { =
myBookmark \# $#,##0.00 } }. [Embedded FC w/n FC]. The bookmark
is replaced, but upon opening the output document {10000} is
displayed after toggling the field codes TWICE.

Code used:
foreach (Microsoft.Office.Interop.Word.Field thisField in
oWordDoc.Fields) {
thisRange = thisField.Code;
if (thisField.Type ==
Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty) {
// bypass empty field code
}
else
{
thisRange.Text = thisRange.Text.Replace("`myBookmark", "10000");
thisField.Update();
}
}

Is there an approach to handle the outer field code properly?
 

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