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.
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:
Microsoft Word 10.0 Object LibraryorMicrosoft Word 11.0 Object LibrarySelectThen add these references to your code behind:
Add this helper method (which handles running the macro):
Then to call your macro use the following (You will need to modify this to suit your particular needs):
This will work if you have setup a macro, such as this, in your document.
In you ASP.NET you would call (Adjust to your file path and appropriate parameters):
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.