0

I have a problem about using c# in word automation. My problem is I want to replace part of text in a textbox, ex: "ABC 12345" and replace 12345 by "123", as a result, "ABC 123" But I don't know how to get part of text in textbox, I use

object firstshape = 1;
string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text;

to get the original text in textbox,but i don't know how to get the range of part text. Is there any solution to get any range of text in textbox? thanks a lot in advance!!!

3
  • 3
    Why not use string.Replace like: text = text.Replace("12345","123"); Commented Feb 19, 2013 at 6:37
  • or you could try remove and insert Commented Feb 19, 2013 at 6:45
  • 1
    You can try to ask google Commented Feb 19, 2013 at 6:47

3 Answers 3

1

You can use replace like this

    string Replace = "12345"; 
    string ReplaceWith = "123"
    text = text.Replace("12345","123")
Sign up to request clarification or add additional context in comments.

2 Comments

I know this method. My problem is the string "12345" will change day by day. Therefore, I have to use the position(Last 5 characters) to replace the text, sorry for describing my problem unclearly! Thanks in advance again!
I think you can read this calls methods msdn.microsoft.com/en-us/library/system.string.aspx but you can use split...
0

To get last 5 characters use this:

string text = w_Doc.shapes.get_Item(ref firstshape).TextFrame.TextRange.Text;
text = text.Substring(text.Length - 5, 5);
text = text.Replace(text, "123"); //to replace

Comments

0

Use Linq

string text = "ABC 12345";
string toReplace = text.Split().SkipWhile(x => x == "ABC").First();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.