none
insert array of string with bullets in word doc using C#.net RRS feed

  • Question

  • I have word document template. I am copying that template doc to a new document and I am trying to write an array of strings with bullets into the document after a bookmark "Reject_Reason". I am able to write the array string into the word document with the bullets after the end of the paragraphs. But I need to write after the bookmark "Reject_Reason". I had tried the below code. Please let me know any options to write the array into the doc with bullets after the book mark.
    What I have tried:

    public void CreateRejectLettersNew()
            {
                application = new MSWord.Application();
                MSWord.Document originalDocument = null;
                MSWord.Document newDocument = null;
                folderpath = txtDestination.Text + "\\Output" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
               
                System.IO.Directory.CreateDirectory(folderpath);
     
                try
                {
                    int processcount = 1;
     
                    originalDocument = application.Documents.Open(strtxtwordTemplate.ToString().Trim());
                    newDocument = application.Documents.Add();
                   
                    MSWord.Range oRange = originalDocument.Content;
                    oRange.Copy();
                    newDocument.Content.PasteAndFormat(MSWord.WdRecoveryType.wdFormatOriginalFormatting);
                    string path = string.Empty;
                    
                    MSWord.Paragraph myAssets;
     
                    if (newDocument.Bookmarks.Exists("Reject_Reason"))
                    {
                        object name = "Reject_Reason";
                        Microsoft.Office.Interop.Word.Range myRng = newDocument.Bookmarks.get_Item(ref name).Range;
                        object ostart = myRng.Start + 0;
                        object oend = myRng.Start + 1;
                        
                        foreach (DataRow drExactRow in dtExact.Rows)
     
                        {
                            string strRejRsns = drExactRow["RejectReason"].ToString().Trim();
                            string[] strRejResArr = strRejRsns.Split(':');
                            path = folderpath + "\\" + drExactRow["IMAGENAME"].ToString() + ".docx";
     
                            //Get current range.
                            myAssets = newDocument.Content.Paragraphs.Add();/* this one writes after all the paragraphs at the end of the document but I need to write it after the "Reject_Reason" bookmark.*/
                            myRng = myAssets.Range;
     
                            //New paragraph for strings with bullets.
                            myAssets = newDocument.Content.Paragraphs.Add();                        
                            myAssets.Range.ListFormat.ApplyBulletDefault();
     
    
                            foreach (string str in strRejResArr)
                            {
                                myAssets.Range.InsertBefore(str);
                            }
     
    
                        }
                    }
                    newDocument.SaveAs(path);
                    newDocument.Save();
     
    
                }
                
                catch (COMException ex)
                {
                    Log.Fatal(ex.ToString());
     
                    throw;
     
                }
                catch (Exception ex)
                {
                    Log.Fatal(ex.ToString());
                    throw;
                }
                finally
                {
                    Log.Info("Closing and Killing all the Office objects");
     
                    newDocument.Close();
                    Marshal.ReleaseComObject(newDocument);
                    originalDocument.Close();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    Marshal.ReleaseComObject(originalDocument);
                    application.NormalTemplate.Saved = true;
                    application.Quit();
                    Marshal.ReleaseComObject(application);
                    originalDocument = null;
                    Log.Info("All Office objects Closed");
     
                }
     
    
            }

    Monday, November 13, 2017 6:05 PM

Answers

  • Hi KomailShirumavilla,

    You could try to add a paragraph after the bookmark range and then select the range and insert the string.

    Here is the example.

     Microsoft.Office.Interop.Word.Range myRng = newDocument.Bookmarks.get_Item(ref name).Range;
                        //select the bookmark range
                        myRng.Select();
                        //insert a paragraph after the book mark
                        myRng.InsertParagraphAfter();
                        //move to next line to get the range of the added paragraph
                        application.Selection.Move(MSWord.WdUnits.wdLine, 1);
                        //get the start location of the added paragraph
                        myRng = application.Selection.Range;
                        myRng.ListFormat.ApplyBulletDefault();
                        foreach (string str in strRejResArr)
                        {
                            myRng.InsertBefore(str);
                        }

    Best Regards,

    Terry


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 14, 2017 2:33 AM

All replies

  • Hi KomailShirumavilla,

    You could try to add a paragraph after the bookmark range and then select the range and insert the string.

    Here is the example.

     Microsoft.Office.Interop.Word.Range myRng = newDocument.Bookmarks.get_Item(ref name).Range;
                        //select the bookmark range
                        myRng.Select();
                        //insert a paragraph after the book mark
                        myRng.InsertParagraphAfter();
                        //move to next line to get the range of the added paragraph
                        application.Selection.Move(MSWord.WdUnits.wdLine, 1);
                        //get the start location of the added paragraph
                        myRng = application.Selection.Range;
                        myRng.ListFormat.ApplyBulletDefault();
                        foreach (string str in strRejResArr)
                        {
                            myRng.InsertBefore(str);
                        }

    Best Regards,

    Terry


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 14, 2017 2:33 AM
  • Thank you so much Terry. It was really helpful and it worked out for me. :)
    Friday, November 17, 2017 10:50 PM