Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4239506
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:02:38+00:00 2026-05-21T03:02:38+00:00

I am working with sharepoint 2010, what I am trying to do is take

  • 0

I am working with sharepoint 2010, what I am trying to do is take a word document template, do a replace on a few key words (ex: replace ##ClientID## with the id of the client) and save it with a specific name in a library on sharepoint.

I have figured out how to do this on a local computer with word interop, however the word interop library is not designed to be run as a service. I then discovered Word Automation Services which appear to do what I need to do. However every example I find on the internet (including here on SO) is just “How to convert from a word document to xxx” using the Microsoft.Office.Word.Server.Conversions namespace. I have yet to find a example on how to use the Microsoft.Office.Word.Server.Service namespace to do a find and replace on a document. The MSDN is very lacking on how to use the classes and I do not know where to begin using it.

Is it not possible to use the services to do what I want to? If it can be done can someone point me in the right direction to do what I want to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:02:39+00:00Added an answer on May 21, 2026 at 3:02 am

    It appears that Word Automation Services is not what I want to use to do what I want to do, what I need is Open XML SDK.

    UPDATE:
    here is the code on how to do a replace on the document, in my the text I wanted to replace where in rich text boxes. so that is why I am looking inside SdtRun.

    public FileDetails GetOrGenerateChecklist(string PracticeName, string ContractID, string EducationDate, string MainContactInfo, string Address)
    {
        if (String.IsNullOrEmpty(PracticeName) || String.IsNullOrEmpty(ContractID))
            return null;
        SPWeb web = SPContext.Current.Web;
    
        SPDocumentLibrary list = (SPDocumentLibrary)web.Lists["Educator Checklists"];
        var templetAddr = String.Concat(web.Url, '/', list.DocumentTemplateUrl);
        SPQuery query = new SPQuery();
        query.Query = string.Concat(
                                "<Where><Eq>",
                                    "<FieldRef Name='FileLeafRef'/>",
                                    "<Value Type='File'>", PracticeName, " - ", ContractID, ".docx</Value>",
                                "</Eq></Where>");
        var items = list.GetItems(query);
    
        //if document exists return existing document.
        if (items.Count > 0)
            return new FileDetails() { Address = String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx"), LastModified = (DateTime)items[0]["Modified"]};
    
        //Begin transforming form template to document.
        MemoryStream documentStream;
    
        //copy the stream to memory
        using (Stream tplStream = web.GetFile(templetAddr).OpenBinaryStream())
        {
            documentStream = new MemoryStream((int)tplStream.Length);
            CopyStream(tplStream, documentStream);
            documentStream.Position = 0L;
        }
    
        using (WordprocessingDocument template = WordprocessingDocument.Open(documentStream, true))
        {
            template.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
            MainDocumentPart mainPart = template.MainDocumentPart;
            mainPart.DocumentSettingsPart.AddExternalRelationship(
                "http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",
                new Uri(templetAddr, UriKind.Absolute));
    
            ReplaceText(mainPart, "#PracticeName#", PracticeName);
            if(!String.IsNullOrEmpty(EducationDate))
                ReplaceText(mainPart, "#EducationDate#", EducationDate);
            if(!String.IsNullOrEmpty(MainContactInfo))
                ReplaceText(mainPart, "#MainContactInfo#", MainContactInfo);
            if(!String.IsNullOrEmpty(Address))
                ReplaceText(mainPart, "#Address#", Address);
        }
        documentStream.Position = 0L;
        try
        {
            list.RootFolder.Files.Add(String.Concat(PracticeName, " - ", ContractID, ".docx"), documentStream);
        }
        catch(SPException)
        {
            return null;
        }
    
        return new FileDetails() { Address = String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx"), LastModified = DateTime.Now };
    
    
    }
    
    private static void CopyStream(Stream source, Stream destination, int bufferSize = 0x1000)
    {
        int num;
        byte[] buffer = new byte[bufferSize];
        while ((num = source.Read(buffer, 0, buffer.Length)) != 0)
        {
            destination.Write(buffer, 0, num);
        }
    
    }
    
    private static void ReplaceText(MainDocumentPart docPart, string match, string value)
    {
        if (value == null)
            value = String.Empty;
        var sdtr = docPart.Document.Descendants<SdtRun>();
        foreach (var sdt in sdtr)
        {
            if (sdt.InnerText == match)
            {
    
                Text txt = new Text(value);
                //using the sdt.FirstChild.FirstChild.CloneNode(true) will copy the text formatting of the old text.
                var newtext = new SdtContentRun(new Run(sdt.FirstChild.FirstChild.CloneNode(true), txt));
                sdt.SdtContentRun.RemoveAllChildren();
                sdt.SdtContentRun.InsertAt(newtext, 0);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get SharePoint PropertyBag Settings 2010 working in my Central Administration site,
I'm currently working with Sharepoint 2010 and Sharepoint API on creating a document library
I'm working on some SharePoint web parts and I'm trying to make them as
I'm working with a client to plan the topology for a public facing SharePoint
I'm working on a webpart for a sharepoint 2010 installation. This has multiple sites,
I am working on a SharePoint 2010 integration that has an SPItemEventReceiver for handling
I am working with SharePoint resource file for SharePoint 2010 page. Example: <Field ID={A1D46FA9-0AE7-421A-A714-9561631632E6}
I need your suggestions for my problem area. I am working with SharePoint 2010.
I am currently developing a service using the SharePoint 2010 Client Object Model to
I am working with SharePoint 2010 and ECMAScript. I have added a customaction to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.