Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have this situation:

I made an add-in for Word 2010 in visual studio 2010 (C#), which dynamicly adds new buttons to a new ribbon tab (data from sql database). Now when i click on a button, a new document with a macro needs to be opend. I have the needed macro in a document (.docm), and the path to it is in the database.

My problem now is, how do i create a new document with the macro from another document?

thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 12:03pm    
Word macro is itself quite a bad idea, and now you want to mix it with Office programming in .NET... Don't explain me why, I won't understand... :-)
--SA
Slacker007 5-Apr-11 12:23pm    
Macros in Word have there place and they can be quite useful and efficient when used properly.
Sergey Alexandrovich Kryukov 5-Apr-11 13:07pm    
Sorry, I disagree... overuse of Word... there are much better ways of working with document. If something is somewhat useful, it is not enough justification for using it.
--SA
Slacker007 5-Apr-11 13:20pm    
I'm interested to hear what you have to say about other ways to work with Word. I'm also interested to hear how long you have developed applications that use and manipulate Microsoft Word.
Sergey Alexandrovich Kryukov 5-Apr-11 13:35pm    
"I don't have to eat all egg to tell it's wrong". Not a good answer, I understand. None. Many times I managed to avoid something from the very beginning, something that others gave up much later and wasted time. Word in not structured well to be used for something except writing formatted texts.

Alternatives? Sure. Just one: XML documents with various representations via XSLT.

Sorry, I'm not in the mood for flames here. You do what you want, me too, OK?
--SA

1 solution

I don't think you can "import" a macro from another document into a separate document on the fly.

I could be off based here but don't you want to create a Word Template with the macro in this template and then when you create a new Word Doc (through your C# code) based on this new template.
 
Share this answer
 
Comments
mpower88 5-Apr-11 6:15am    
Yes that is what i want, do you know how to do that? Do i need to save my document with macro as a .dotm? (word macro-enabled template)
Slacker007 5-Apr-11 6:35am    
You need to create a base document with everything that you want in it including macros and save it as a template (I believe it is .dotm). Then call this document template from you C# code.
mpower88 5-Apr-11 6:44am    
I've done that, now i have just 1 problem.
I call te template like this: "Globals.ThisAddIn.Application.Documents.Open(location);"
where location is the path to the template. but i realy open the template, but i need to open a new document based on the template... thx for your help btw!
mpower88 5-Apr-11 6:51am    
I fixed it, this is my solution:
//create new document, location = path to template
Document doc = Globals.ThisAddIn.Application.Documents.Add(location);
//auto run macro
doc.RunAutoMacro(WdAutoMacros.wdAutoOpen);

Thank you for your help Slacker007 !
Slacker007 5-Apr-11 7:29am    
You will open the template - this is supposed to happen. When the user clicks save in the template it will save it as a regular document and not a template. You are only opening an instance of the template not the template itself.

BTW - if you feel like it, then please vote on my solution appropriately. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900