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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:01:30+00:00 2026-06-12T04:01:30+00:00

I have a gridview, I need to export the gridview to MS word and

  • 0

I have a gridview, I need to export the gridview to MS word and this works fine, but I need to put custom header and footer on each page of Ms word. Is it possible to run a macro from asp.net on the exported gridview so that I can put the custom header and footer on the exported gridview and also adjust some margins on the printed out page.

any help will be appreciated.

Thanks.

  • 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-12T04:01:31+00:00Added an answer on June 12, 2026 at 4:01 am

    This tutorial will show you how to call a macro using .NET. This is a summary of the steps relevant for your scenario (You may have some of this setup already if you have managed to export your GridView already):

    Add a reference to the Microsoft Word Library:

    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate Microsoft Word 10.0 Object Library or Microsoft Word 11.0 Object Library
    3. Click Select

    Then add these references to your code behind:

    using System.Reflection;
    using Word = Microsoft.Office.Interop.Word;
    using Microsoft.Office.Core;
    

    Add this helper method (which handles running the macro):

    private void RunMacro(object oApp, object[] oRunArgs)
    {
        oApp.GetType().InvokeMember("Run",
        System.Reflection.BindingFlags.Default |
        System.Reflection.BindingFlags.InvokeMethod,
        null, oApp, oRunArgs);
    }
    

    Then to call your macro use the following (You will need to modify this to suit your particular needs):

    private void CallCustomHeaderFooterMacro(string filename, string pageTitle, string author)
    {
        Word.ApplicationClass oWord = new Word.ApplicationClass();
        oWord.Visible = false;
        Word.Documents oDocs = oWord.Documents;
        object oFile = filename;
    
        // Used for optional arguments
        object oMissing = System.Reflection.Missing.Value;
    
        // CHOOSE THE APPROPRIATE CALL, DEPENDING ON THE LIBRARY YOU REFERENCE
        // Microsoft Word 10.0 Object Library
        Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
        // Microsoft Word 11.0 Object Library
        // Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
        // Run the 'CustomHeaderFooter' Macro.
        /* Change "CustomHeaderFooter" to your macro name.
         * You can send parameters to your macro such as: 
         * a Page title, author, date or other data you need to create the customised 
         * header and footer. You add and remove these as required. I have used pageTitle
         * and author as an example
        */
        RunMacro(oWord, new Object[]{"CustomHeaderFooter", pageTitle, author});
    
        // Save (as required)
    
        // Quit Word and clean up.
        oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject (oDoc);
        oDoc = null;
        System.Runtime.InteropServices.Marshal.ReleaseComObject (oDocs);
        oDocs = null;
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject (oWord);
        oWord = null;
    }
    

    This will work if you have setup a macro, such as this, in your document.

    Public Sub CustomHeaderFooter(sPageTitle As String, sAuthor As String)
        ' Your code to create the header and footer as required.
        ' The easiest way to get the macro code is to open the base document and
        ' and record the actions that you need such as adjusting the print margins
        ' and adding the custom headers and footers, then modify the resultant code
        ' to accept your parameters.
    End Sub
    

    In you ASP.NET you would call (Adjust to your file path and appropriate parameters):

    CallCustomHeaderFooterMacro(Server.MapPath("~/mydocument.docx"), "Sample Title", "John Smith");
    

    I hope this is clear. Obviously I can’t provide the macro code to produce the actual header and footer, but as above, the easiest path is to record the actions and manually adjust the generated code.


    Edit: Just as a side note. Microsoft doesn’t recommend the use of Office automation from server-side applications, i.e. calling from ASP.NET. This article explains here and offers alternative mechanisms for manipulating Office documents, which may or may not be useful to you. But I thought it may help, depending on the scalability your project requires.

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

Sidebar

Related Questions

I have a gridview control in ASP.Net 2.0 and i need to export this
I have a gridview with data. I need to put one button to Export/import
Need some help to solve this. I have a gridview and inside the gridview
i have a gridview with search option by a database table.All working fine but
i need to have a gridview of linearlayouts. Each linearLayout must have a imageview
i have GridView with each Grid as a simple TextView. i need a scrollbar
I have a gridview with data on a content page.i need to get the
Need help pretty soon on this..... I have a Gridview in which the columns
I need to have a GridView listing all invoices of an account. Each account
I have gridview with 15 and above columns, I need to freeze the header

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.