locked
using Ms office Api in C# - Find & Replace RRS feed

  • Question

  • User-1179126167 posted

    Hi All,

    I have a doc or docx file. That file may contains, images, texts and some font formatting and some keys like <FirstName> and <LastName>.

    if I call a function that function returns array of bytes read from the docx or doc file.

    I have array of bytes. Now, I need to write a function to find the key <FirstName> and <LastName> and replace with some values.

    This should be done by using MS Word API. Becuase, the file may contains images, text formatting. So, we need to do this without affecting the images and text formatting.

    How to do this?

    Thanks,

    R. Eswaran

    Thursday, July 30, 2015 1:50 AM

All replies

  • User1724605321 posted

    Hi Eswaran ,

    This should be done by using MS Word API. Becuase, the file may contains images, text formatting. So, we need to do this without affecting the images and text formatting.

    According to your description ,I would recommend you using the Open XML SDK to achieve your requirement .With the library, you can do the following to replace text from a Word document:

    // To search and replace content in a document part.
    public static void SearchAndReplace(string document)
    {
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }
    
            Regex regexText = new Regex("Hello world!");
            docText = regexText.Replace(docText, "Hi Everyone!");
    
            using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
        }
    }
    

    You could refer to link below for more details and demo:

    https://msdn.microsoft.com/en-us/library/office/bb508261.aspx

    Best Regards,

    Nan Yu

    Friday, July 31, 2015 1:21 AM
  • User-1157115462 posted

    From what you said I would say that you are actually looking for a "Mail Merge" where you use a Word document as a template containing placeholders that need to be populated with values from a database (or any other data source). If so, I would strongly suggest you to check Docentric Toolkit which already does all this and much more, before delving into creating your own solution from scratch. This solution will allow you to place many types of placeholders (string, number, datetime, image), plus other types of tagging elements, e.g. repeating blocks, charts, sub documents. You can insert these tags not just in the body of a document, but also in headers and footers of multiple sections.

    If your budget doesn't allow 3rd party components, then I suggest you to use Open XML SDK, but it in order to create a mail merge library, it will probaby cost you much more tan a 3rd party product.

    BTW, don't even think of using Office COM (Office/Word automation) - this approach doesn't work (especially) in ASP.NET applications.

    Tuesday, August 11, 2015 11:25 AM
  • User-1179126167 posted

    Hi i have done using the below code. But, I got an propblem when the find and replace function executes, a empty word doc opens it. But, I don't want any word doc open while find and replace happens.

    Here is my code..

    public void FindAndReplaceInWordDoc() {
    
                try
                {
    byte[] fileContentInBytes = response.Data;
                    var tmpFile = Path.GetTempFileName();
                    var tmpFileStream = File.OpenWrite(tmpFile);
                    tmpFileStream.Write(fileContentInBytes, 0, fileContentInBytes.Length);
                    tmpFileStream.Close();
    
                    MSWordApplication wordApp = new MSWordApplication { Visible = false };
                    MSWordDocument aDoc = wordApp.Documents.Open(tmpFile, ReadOnly: false, Visible: false);
                    aDoc.Activate();
    
                    FindAndReplace(wordApp, mergeFields);
    
                    wordApp.Documents.Save();
                    wordApp.Documents.Close();
                    aDoc = null;
                    wordApp = null;
    
                    var bytes = File.ReadAllBytes(tmpFile);                
    
                    File.Delete(tmpFile);
    
                    response.ReturnCode = 0;
                    response.Data = bytes;
                    response.ReturnMessage = "";
                    response.AdditionalMessage = "";
                }
                catch (Exception ex)
                {
                    response.ReturnCode = 1000;
                    response.Data = null;
                }
    }
    
    private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, MergeField[] mergeFields)
            {
                //options
                object matchCase = false;
                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 read_only = false;
                object visible = true;
                object replace = 2;
                object wrap = 1;
    
                for (int i = 0; i < mergeFields.Length; i++)
                {
                    object findText = (object)mergeFields[i].Field;
                    object replaceWithText = (object)mergeFields[i].Value;
    
                    //execute find and replace
                    doc.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);                
                }
            }
    

    Pls guid me is there anything worng ?

    Thanks

    R. Eswaran

    Wednesday, August 12, 2015 2:04 AM
  • User1724605321 posted

    Hi MSWordApplication ,

    Please try to debug in visual studio to find out which line cause the new word doc opened .And i find that you are using "MSWordApplication" class ,are you encapsulate the "Microsoft.Office.Interop.Word.ApplicationClass" or that is a third-party class ?

    Best Regards,

    Nan Yu

    Friday, August 14, 2015 5:57 AM
  • User-1179126167 posted

    Hi,

    "MSWordApplication" is not third party class.

    I have declared like below

    using MSWordApplication = Microsoft.Office.Interop.Word.Application;
    using MSWordDocument = Microsoft.Office.Interop.Word.Document;

    The document opens when the follwoing code executing

    wordApp.Documents.Save();
    wordApp.Documents.Close();

    It is running fine in my local machine.. But, when I deploy in testing environment that time only i got some error..

    I assume, if we don't have permission to create tmp file in the testing envirnoment then we unable to create a file.

    so, how can we create ms word doc using bytes of array without creating temp file.

    Now, I don't want to create tmp file.

    I need to create word object using array of bytes

    How to do that?

    Thanks

    R. eswaran

    Friday, August 14, 2015 10:35 AM
  • User-1179126167 posted

    Hi Nan Yu,

    "MSWordApplication" is not third party class.

    I have declared like below

    using MSWordApplication = Microsoft.Office.Interop.Word.Application;
    using MSWordDocument = Microsoft.Office.Interop.Word.Document;

    The document opens when the follwoing code executing

    wordApp.Documents.Save();
    wordApp.Documents.Close();

    It is running fine in my local machine.. But, when I deploy in testing environment that time only i got some error..

    I assume, if we don't have permission to create tmp file in the testing envirnoment then we unable to create a file.

    so, how can we create ms word doc using bytes of array without creating temp file.

    Now, I don't want to create tmp file.

    I need to create word object using array of bytes

    How to do that?

    Monday, August 17, 2015 12:59 AM