Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
4.83/5 (6 votes)
See more:
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 ?
Posted
Updated 15-Mar-11 22:39pm
v2
Comments
Dalek Dave 16-Mar-11 4:40am    
Edited for Spelling, Grammar and Syntax.
Good question though.
Alimjan Yasin 16-Mar-11 5:33am    
I solved the problem. it is here
rng = WordDoc.Range(ref start, ref end);
changed as
rng = WordDoc.Range(ref start, ref (end -1));
and it fixed.
wizardzz 16-Mar-11 13:35pm    
Good job, taking the initiative and not giving up!
Toli Cuturicu 16-Mar-11 16:13pm    
Post the solution as an answer if it works!

1 solution

when replace the sentences ,we need to be carefull with ending position.

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();


it will replace sentences ending mark if we use like above.
it have to be used like below.

start = WordDoc.Content.Sentences[index].Start;
end = WordDoc.Content.Sentences[index].End - 1;
Range rng;
rng = WordDoc.Range(ref start, ref end);
rng.Text = text;
rng.Select();

each sentences in doc file looks like has its ending mark, we have to remain the mark , only replace its content.
so it worked well.
my solution is :
at document level get all sentences in doc file first.
the replace all of them.
by doing this you can get content fo pure text, tables. so you need not to warry about spearate handling pure text and tables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900