There are numerous instances where one stores the word document format as a Microsoft Word template. When the user opens the document (using the template), some macro needs to be executed. This can be achieved by RunAutoMacro Method of Word VBA
Sub Run_Macro_In_WordDocument()
Dim oWD As Word.Document
Set oWD = Documents.Add("c:\dcomdemo\sample.dot")
oWD.RunAutoMacro wdAutoOpen
End Sub
Here a new document is open based on the Sample.dot template and once the document is open the AutoOpen macro is fired
RunAutoMacro Method can be used to execute an auto macro that's stored in the specified document. If the specified auto macro doesn't exist, nothing happens
On the other hand, if a normal macro (not auto open etc) needs to be executed, Run method can be used
Application.Run "Normal.FormatBorders"
Where would you place the code for this? In the .dot template or elsewhere?
ReplyDeleteK
K,
Deleteyou should paste this code in a separate vba project, provided
c:\dcomdemo\sample.dot is a valid word document with a macro in it. hope dis will help
The Selection keyword inside the macro is not invoked if called from a vb project, do I have to add anything sort of ActiveDocument or ActiveWindow before the keyword in the macro?
ReplyDeletePlz help
P.S The macro is getting called with other codes