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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:50:37+00:00 2026-05-17T01:50:37+00:00

i’m trying to make a program that inserts data into specific places in existing

  • 0

i’m trying to make a program that inserts data into specific places in existing word document and saves a copy of it.
and i have no clue how to do it , and i cant find any good resource on office 2010 automating.
can anyone point me in the right direction and/or give me some examples.

thanks in advance.

found a solution will add an answer later on

  • 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-17T01:50:38+00:00Added an answer on May 17, 2026 at 1:50 am

    Here is how i did it , it may not be the best way , but its working !

    add references to the office interop

    using Microsoft.Office.Interop.Word;
    using Word = Microsoft.Office.Interop.Word;
    using Excel = Microsoft.Office.Interop.Excel;
    
    
              //defines new excel and workd apps
                var ap = new Word.Application();
                var excelApp = new Excel.Application();
                // defines new excel worksheet & workbooks
                Excel.Workbook exBook;
                Excel.Worksheet xlWorkSheet;
                // should the excell/word apps be visible ? 
                excelApp.Visible = false;
                ap.Visible = false;
    
                //defining the index numbers of our content controls that are in the word template
                // index numbers start from 1 and are numbered by order of creation
                object Price, Name, address;
                Price = 1;
                Name = 2;
                address = 3;
    
    
                // here we open the excell file
                exBook = excelApp.Workbooks.Open(@"C:\test.xls");
                // and we open the first worksheet
                xlWorkSheet = exBook.Worksheets.get_Item(1);
                Excel.Range range ;
                //here we select the first worksheet and make it active
                Excel._Worksheet workSheet = (Excel.Worksheet) excelApp.ActiveSheet;
                //we open the word document
                var doc = ap.Documents.Open(@"C:\test.dotx", ReadOnly: false, Visible: false);
                // and we assign the content controls 
                var dPrice = doc.ContentControls.get_Item(ref Price);
                var dName = doc.ContentControls.get_Item(ref Name);
                var dAddress = doc.ContentControls.get_Item(ref address);
                doc.Activate();
                range = xlWorkSheet.UsedRange;
                // here we define the columns that we are going to select
                object t, P , E , K, N,M,J;
    
                P = "P";
                E = "E";
                K = "K";
                J  = "J";
                N = "N";
                M = "M";
    
                // and here we loop trought the rows of the excell worksheet
                // IMPORTANT! excell rows count starts from 1 and not from 0 !
             for (int i =1; i< Convert.ToInt16(Settings1.Default.copies) ;i++)
    
                {
    
                    t = i;
                    // here we get the value if cell t(1..2..3..etc), P
                    var dummy = (range.Cells[t, P] as Excel.Range).Value2;
                    // here we insert the content of the cell to the content control
    
                    dPrice.Range.Text = ": " + Convert.ToString(dummy) + " лв";
                    dName.Range.Text = ": " + (string)(range.Cells[t, E] as Excel.Range).Value2;
    
                    // same thing here
                    var city = (string) (range.Cells[t, J] as Excel.Range).Value2;
                    var address1 = (string) (range.Cells[t, K] as Excel.Range).Value2;
                    var city2 = (string) (range.Cells[t, M] as Excel.Range).Value2;
                    var address2 = (string) (range.Cells[t,N] as Excel.Range).Value2;
                    if (!string.IsNullOrEmpty(city2) && city2 != " " && !string.IsNullOrEmpty(address2) && address2 != " ")
                    {
                        dAddress.Range.Text = ": " +city.Normalize() + " " + address1.Normalize() + " , " + city2.Normalize() + " " + address2.Normalize() ;
    
                    }
                    else
                    {
                        dAddress.Range.Text = ": " + city.Normalize() + " " + address1.Normalize();
                    }
    
    
                    try
                    {
                        //here we try to save the word document as a pdf file
                        object name = @"C:\t\test"+i+".pdf";
                        object FileFormat = WdSaveFormat.wdFormatPDF;
                        doc.SaveAs(ref name, ref FileFormat);
    
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Exception Caught: " + ex.Message +" source "+ ex.Source.ToString());
    
                    }
    
                }
                // here quit word without saving the changes to the template  
                ap.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
                excelApp.Quit();
                // and we release the objects 
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
    

    i hope this was helpful to someone 🙂

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.