none
Problem to export numbers to word doc. RRS feed

  • Question

  • Hi

    when i export numbers into word doc, it reverse those numbers and save to word! for example if i have this string "I have 1/5 number", it export "I have 5/1 number".

    also i reverse my string from "1/5" to "5/1" but still it export "5/1"!

    Note : i'm working on rtl languages (persian or arabic).

    thanks in advance


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx

    • Moved by Lisa Zhu Friday, August 31, 2012 7:16 AM Word related (From:Visual C# General)
    Thursday, August 30, 2012 8:23 AM

All replies

  • Hi Hmed,

    could you please post ur code,

    then only we can understand the problem


    programmer on .net

    Thursday, August 30, 2012 9:36 AM
  • Hi

    of course :

    private void button1_Click(object sender, EventArgs e)
    {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("(2ق م کره بادام زمینی + 2عدد  بیسکویت) +  (نیم لیوان شیر)");
                sb.AppendLine("30گرم نان + 1/5 تخم مرغ آب پز یا 1 تخم مرغ نیمرو");
                string text = this.CorrectInvalidNumbers(sb.ToString());
                WriteInfoToWordDoc(this._wordFile, sb.ToString());
    }
    
    private string CorrectInvalidNumbers(string text)
            {
                string regexPattern = @"\d/5";
                Regex r = new Regex(regexPattern);
                MatchCollection matches = r.Matches(text);
                foreach (Match m in matches)
                {
                    string[] strs = m.Value.Split(new char[] { '/' });
                    if (strs.Length == 2)
                    {
                        string helper = strs[0];
                        strs[0] = strs[1];
                        strs[1] = helper;
                    }
                    string replace = strs[0] + "/" + strs[1];
                    text = Regex.Replace(text, m.Value, replace);
                }
                
                return text;
            }
    
            public static bool WriteInfoToWordDoc(object fileName, string replaceWith)
            {
                bool result = false;
                object missing = System.Reflection.Missing.Value;
    
                // setup Word.Application class.
                Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    
                // setup Word.Document class we'll use
                Microsoft.Office.Interop.Word.Document wordDoc = null;
    
                object readOnly = false;
                object isVisible = false;
    
                try
                {
                    // set word to be not visible
                    wordApp.Visible = false;
    
                    // open the word document
                    wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing,
                        ref missing, ref missing);
    
                    // activate the document
                    wordDoc.Activate();
    
                    FindAndReplace(wordApp, "x", replaceWith);
    
                    // save the document to correct destination
                    wordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing);
    
                    // close the document
                    wordDoc.Close(ref missing, ref missing, ref missing);
                    result = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
    
                return result;
            }
    
            private static bool FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
            {
                bool result = false;
                object matchCase = true;
                object matchWholeWord = true;
                object matchWildCards = false;
                object matchSoundsLike = false;
                object matchAllwordForms = false;
                object forward = true;
                object format = false;
                object matchKashida = false;
                object matchDiacritics = false;
                object matchAlefHamza = false;
                object matchControl = false;
                object readOnly = false;
                object visible = true;
                object replace = 2;
                object wrap = 1;
                result = wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllwordForms,
                    ref forward, ref wrap, ref format, ref replaceWithText, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza,
                    ref matchControl);
                return result;
            }


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx

    Thursday, August 30, 2012 12:32 PM
  • Hi Hamed ,

    From your description , I ‘d like to move this post to  the most related forum .

    There has  more experts , so you will get  better support  and  may have more luck getting answers .

    Thanks for your understanding .

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, August 31, 2012 7:15 AM