Free Support Forum - aspose.com

Adding a .dot template to a document on the server

I have a set of processes that create word documents residing on the server. Our users then click a link which delivers the appropriate document to their browser (always IE). I need to include some VBA code in the document, which I have in a Word document template. How can I programatically add the .dot reference to the Word document prior to sending to the browser?

Thanks

Hi Marvin,

Thanks for your inqury.

You can attach the template containing the VBA macro to your generated documents by loading the template before you build you content instead of starting with an empty document.

// Load the template document.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Document doc = new Document("Template.dot");

// Code to generate content

// Save the file to disk.

doc.Save("Document out.doc");

Thanks,

Thank you Adam. I have created an aspx page to accomplish this feat. However, the processes are already complete for this document's creation, and I'm merely retreiving the requested document to deliver via Page.Response. So I'm creating a "baseDocument" using the appropriate .dot file, and then appending the desired document. But, of course, I'm now getting an additional blank page everytime I retreive the document. Do you have a quick solution for this?

Thanks

Marvin

Hi Marvin,

Thanks for this additional information. Yes this sounds like a good way to achieve what you are looking for. I believe setting the PageSetup.SectionStart property to SectionStart.Continuous should solve the problem with the blank first page. You should only need to set that property for the first section of your generated document (the one that you are appending to the end of the template with the macro).

If you have any further issues could you please attach your template and generated document here for testing and I will take a look.

Thanks,

Adam, you have been a life saver. I appreciate all your help. I have implemented your suggestion, and it works like a champ. All of this works... with Word 2007. I almost feel bad to have to ask this but... It appears that with Word2003, though the template is sort of attached: if you to go "templates and add-ins" it is not present even in the global section, but if you hit Alt+F11, the VB window comes up, and it is there. I do have it password protected, and you don't even have to do anything with it. Not even click on it. It seems that just bringing up the VB window with Alt-F11 is all it needs to "activate" the VB code. Now, most of our users are on Word 2007, and I wouldn't mind having them hit Alt-F11 for this to work, is there any reason you can think of to cause this?

Thanks

Marvin

Hi Marvin,

Cheers for that. Feel free to ask if any time you have an inquiry. Are you able to attach your template (with macro) and the generated document here for testing? It will help get more insight into the issue on my side. All attachments that you upload to the forum can only be opened by you and Aspose staff members.

I believe you may want to try explicitly setting the full path to the template document using the Document.AttachedTemplate property and see if that helps.

If this has no effect then I also have an idea, you might want to try saving your template .dot document to a .doc document using MS Word. Loading this file instead of the .dot template file into Aspose.Words and then appending your generated document as you were before. This might just help.

Thanks,

Adam,

You are definitely a life saver. The Document.AttachedTemplate property did just what I needed it to do. Thank you so much.

How is the weather in Auckland? I have visited that great city many years ago, and still look back on that adventure with favor.

Thanks.

Marvin

Hi Marvin,

It's great that you have it fully working now.

Cheers for those kind words regarding Auckland. I must say since it's winter over here the weather is erratic at best right now. It changes every hour or so from sunny to raining and back again. The weather is much better during summer, which makes it a great time to visit then.

Thanks,