replace range.text

B

bcarsto

I have some c# code to fiind and replace a sequence of text in a text box.
The code uses a range to find the start and end delimters of the text to be
replaced, adjusts the range to only include the text to replace, then
replacing the text with a different value. What happen is that more than the
desired text is replaced.

attached is the code illustrating the issue. What am I doing wrong. please
help. It should be runnable if anyone want to give it a try.

Microsoft.Office.Interop.Publisher._Application pub = new
Microsoft.Office.Interop.Publisher.ApplicationClass();
Microsoft.Office.Interop.Publisher.Document doc =
pub.NewDocument(Microsoft.Office.Interop.Publisher.PbWizard.pbWizardNone, 0)
doc.Pages[1].Shapes.AddTextbox(Microsoft.Office.Interop.Publisher.PbTextOrientation.pbTextOrientationHorizontal,
10,10,500,300);

Microsoft.Office.Interop.Publisher.Shape Shape1 =
doc.Pages[1].Shapes[1];//Item(iShp, VT_I2);
Microsoft.Office.Interop.Publisher.TextRange TRange
=Shape1.TextFrame.TextRange;

TRange.Text = "Shipper #:«SHIPPER#»(text following shipper)";

// replace text between delimteres
int istart = TRange.Text.ToString().IndexOf("«");
int iend = TRange.Text.ToString().IndexOf("»", istart) + 1;

// szDbgOrigText = "Shipper #:«SHIPPER#»(text following shipper)"
string szDbgOrigText = TRange.Text;

TRange.Collapse(
Microsoft.Office.Interop.Publisher.PbCollapseDirection.pbCollapseStart);
TRange.MoveStart(
Microsoft.Office.Interop.Publisher.PbTextUnit.pbTextUnitCharacter, istart);
TRange.MoveEnd(
Microsoft.Office.Interop.Publisher.PbTextUnit.pbTextUnitCharacter, iend -
istart);

// szDbgOrigText = "«SHIPPER#»"
string szDbgOrigRange = TRange.Text;

// replace the text with its value
TRange.Text = "Peter";

// expand the text area to the entire "story"
TRange.Expand( Microsoft.Office.Interop.Publisher.PbTextUnit.pbTextUnitStory);

//szDbgNewText = Shipper #:peterowing shipper)
string szDbgNewText = TRange.Text;

doc.SaveAs("c:\\result.pub",
Microsoft.Office.Interop.Publisher.PbFileFormat.pbFilePublication, true);
doc.Close();
pub.Quit();
System.Diagnostics.Process.Start("c:\\result.pub");

Thanks,
bob
 
B

bcarsto

It turns out that tjhis is only a problem with Publisher 2007. Publisher
r2003 works fine.
 
B

bcarsto

Microsoft has confirmed that this is a bug in Publisher 2007

bcarsto said:
It turns out that tjhis is only a problem with Publisher 2007. Publisher
r2003 works fine.

bcarsto said:
I have some c# code to fiind and replace a sequence of text in a text box.
The code uses a range to find the start and end delimters of the text to be
replaced, adjusts the range to only include the text to replace, then
replacing the text with a different value. What happen is that more than the
desired text is replaced.

attached is the code illustrating the issue. What am I doing wrong. please
help. It should be runnable if anyone want to give it a try.

Microsoft.Office.Interop.Publisher._Application pub = new
Microsoft.Office.Interop.Publisher.ApplicationClass();
Microsoft.Office.Interop.Publisher.Document doc =
pub.NewDocument(Microsoft.Office.Interop.Publisher.PbWizard.pbWizardNone, 0);
doc.Pages[1].Shapes.AddTextbox(Microsoft.Office.Interop.Publisher.PbTextOrientation.pbTextOrientationHorizontal,
10,10,500,300);

Microsoft.Office.Interop.Publisher.Shape Shape1 =
doc.Pages[1].Shapes[1];//Item(iShp, VT_I2);
Microsoft.Office.Interop.Publisher.TextRange TRange
=Shape1.TextFrame.TextRange;

TRange.Text = "Shipper #:«SHIPPER#»(text following shipper)";

// replace text between delimteres
int istart = TRange.Text.ToString().IndexOf("«");
int iend = TRange.Text.ToString().IndexOf("»", istart) + 1;

// szDbgOrigText = "Shipper #:«SHIPPER#»(text following shipper)"
string szDbgOrigText = TRange.Text;

TRange.Collapse(
Microsoft.Office.Interop.Publisher.PbCollapseDirection.pbCollapseStart);
TRange.MoveStart(
Microsoft.Office.Interop.Publisher.PbTextUnit.pbTextUnitCharacter, istart);
TRange.MoveEnd(
Microsoft.Office.Interop.Publisher.PbTextUnit.pbTextUnitCharacter, iend -
istart);

// szDbgOrigText = "«SHIPPER#»"
string szDbgOrigRange = TRange.Text;

// replace the text with its value
TRange.Text = "Peter";

// expand the text area to the entire "story"
TRange.Expand( Microsoft.Office.Interop.Publisher.PbTextUnit.pbTextUnitStory);

//szDbgNewText = Shipper #:peterowing shipper)
string szDbgNewText = TRange.Text;

doc.SaveAs("c:\\result.pub",
Microsoft.Office.Interop.Publisher.PbFileFormat.pbFilePublication, true);
doc.Close();
pub.Quit();
System.Diagnostics.Process.Start("c:\\result.pub");

Thanks,
bob
 

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