I am working on word application.
My task is to read doc file's content and translate it to another language then save at exact postion in doc file.
In short, translate content and don't change the format of doc file.
Firstly, I read doc file, extracted sentences and saved it to List object.
public static void GetSentencesAtContent()
{
int count = WordDoc.Content.Sentences.Count;
if (m_list != null)
m_list.Clear();
m_list = new List<SentencesInfo>();
for (int i = 1; i <= count; i++)
{
SentencesInfo m_info;
m_info.start = WordDoc.Content.Sentences[i].Start;
m_info.end = WordDoc.Content.Sentences[i].End;
string m_str = WordDoc.Content.Sentences[i].Text.Trim();
m_info.text = m_str.Replace("\r\a", "");
m_list.Add(m_info);
}
}
I tried to replace all sentences in doc with random sentences in order to test the replacing.
public static void replace(int index, string text )
{
int p = WordDoc.Content.Sentences.Count;
object start, end;
start = WordDoc.Content.Sentences[index].Start;
end = WordDoc.Content.Sentences[index].End;
Range rng;
rng = WordDoc.Range(ref start, ref end);
rng.Text = text;
rng.Select();
WordDoc.Save();
}
public static void replaceAll()
{
m_testlist = new List<string>();
m_testlist.Add("hi!");
m_testlist.Add("how r u.");
m_testlist.Add("fine day.");
m_testlist.Add("speak.");
m_testlist.Add("8 million.");
m_testlist.Add("stand up.");
m_testlist.Add("victory.");
System.Random a = new Random(System.DateTime.Now.Millisecond);
int count = m_list.Count;
for (int i = 0 ; i < count; i++)
{
string m_str = m_list[i].text;
int RandKey = a.Next(count - 1);
replace(i + 1, m_testlist[RandKey] + "\r\n");
}
}
</string>
I write a function which replaces one sentence by new value. (I did not use the start and end inofrmation of sentences, since I can get it by sentence's index when replacing).
I placed the function in a loop, where I tried to replace I sentences in doc file by random value.
But the problem is : when there are many sentences in one line, my code break one line (there are more than one sentences in one line) into several lines after replacement, each line has one sentence.
To avoid this I removed the "\r\n" from the code :
replace(i + 1, m_testlist[RandKey]);
but at this time another problem occured, which is that the sentences number is deacresing, so there is error in the loops when the last few sentences is replacing.
When I read sentences at first time I removed "\r\a" from the sentences, if I did not do that when I save sentences in XElement in XAML it shows error which says unable to recognize simbol 0x07 something like that (expecially when sentences came from table, each cell's text is one sentences , and there are always "\r\n" after each cell's content).
Look at GetSentencesAtContent function.
How should I deal with that task?
Read doc file , translate it then save it to same file?
During translation I should send doc file content to XAML .
As I said:
XAML shows error don't recognizing 0x07.
during replacing in loops sentences the sentences number s deacresing and which cause runtime error.
Replacing sentences caused changing lines in doc file.
How can I fix those problem and finish the task?
is there anyone who give advice ?