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 9283687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:41:39+00:00 2026-06-18T18:41:39+00:00

I have a word document as a template and I want to replace a

  • 0

I have a word document as a template and I want to replace a placeholder; #Address with a number of lines of text.
It works except that the Environment.NewLine gets ignored. So how do I put in a line break for each new line?
I should point out that in the code, addressLines is a string made up of each line in the collection seperated by a comma and the standard new line constant.

public static void PrintAddress(string companyName, List<string> lines, string outputFolder)
{
    // TODO put in linebreak for each line of the address
    lines.Insert(0, companyName);
    var lineEnd = "," + Environment.NewLine;
    var addressLines = string.Join(lineEnd, lines.ToArray());
    var application = new Application();
    var path = Path.Combine(HttpContext.Current.Server.MapPath(AppSettings.UploadFolder), SessionObjectsSCD.UploadedFile.FileName);
    var document = application.Documents.Open(path);
    WordDocumentClass.FindAndReplace(document, "#ADDRESS", addressLines);
    object filename = string.Format("{0}/{1}.docx", outputFolder, companyName);
    document.SaveAs(ref filename);

    // TODO print document

    application.Quit();
}

public static void FindAndReplace(Document document, string placeHolder, string newText)
{
    object missingObject = null;
    object item = WdGoToItem.wdGoToPage;
    object whichItem = WdGoToDirection.wdGoToFirst;
    object replaceAll = WdReplace.wdReplaceAll;
    object forward = true;
    object matchAllWord = true;
    object matchCase = false;
    object originalText = placeHolder;
    object replaceText = newText;

    document.GoTo(ref item, ref whichItem, ref missingObject, ref missingObject);
    foreach (Range rng in document.StoryRanges)
    {
        rng.Find.Execute(
            ref originalText,
            ref matchCase,
            ref matchAllWord,
            ref missingObject,
            ref missingObject,
            ref missingObject,
            ref forward,
            ref missingObject,
            ref missingObject,
            ref replaceText,
            ref replaceAll,
            ref missingObject,
            ref missingObject,
            ref missingObject,
            ref missingObject);
    }
}
  • 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-06-18T18:41:41+00:00Added an answer on June 18, 2026 at 6:41 pm

    I found that this is the answer;
    var lineEnd = “,\x0B”;

    Next time I will try Open XML Toolkit as mentioned in the other answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a word document template that I need to do a find and
I have a template in word (.docx) format and want to replace some placeholders
I have a word document that was saved as xml. now I need to
I have an ordered list in my Word document, and I want to add
I have a GridView and I want to export it in Word document using
Every now and then I receive a Word Document that I have to display
I have a macro that creates a new document based on a template stored
We have an Oracle BPM 10g activity that: Reads a form-fill protected Word document
I have a Word document(some template format) where it containing some placeholders for the
I have a Word 2010 template with some bookmarks and styles that I must

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.