I'm trying to run a macro when opening a word document but when I try it I get this error:
Exception Details: System.Runtime.InteropServices.COMException: Unable to run the specified macro
Any one got any ideas on this? I've googled around and can't find anything on it.
This is my code:
Sub openDoc(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim WordApp As New Word.ApplicationClass()
Dim missing As Object = System.Reflection.Missing.Value
Dim fileName As Object = "C:\\template.dot"
Dim newTemplate As Object = False
Dim docType As Object = 0
Dim isVisible As Object = True
Dim aDoc As Word.Document = WordApp.Documents.Add(fileName, newTemplate, docType, isVisible)
aDoc.Application.Run("TemplateProject.Modules.TemplateForm")
WordApp.Visible = True
aDoc.Activate()
End Sub
I don't think it is an access problem as I have opened the file without trying to run the macro without any problems and as I am running the pages locally rather than remotely.
It's just when I try to run the macro that I get that error.
Ok, I can change the content of the document with aDoc.Content.Text (and any other command within aDoc.Content) and can save the file with aDoc.SaveAs(saveFile) in asp.net.
I have made sure that I can see the file in the directory that I saved it to in the saveFile object variable.
Well, considering that running Word on a web server isn't supported and is not recommended in about the strongest terms possible ... I wouldn't really expect everything to work.
I'd guess that it has to do with macro security. But ... you shouldn't be doing this in the first place. Running a strictly desktop app on a web server can have bad bad bad consequences. Like ... what happens when Word pops up a modal dialogue in the server
user context? You can't clear it.
I am building this system for an intranet. It wont be publicly available. It will run word on the users machine rather than the server hosting the site.
Nalum
Member
1 Points
23 Posts
Running a Word Macro with asp.net
May 26, 2008 01:15 PM|LINK
Hey all,
I'm trying to run a macro when opening a word document but when I try it I get this error:
Exception Details: System.Runtime.InteropServices.COMException: Unable to run the specified macro
Any one got any ideas on this? I've googled around and can't find anything on it.
This is my code:
Sub openDoc(ByVal sender As Object, ByVal e As CommandEventArgs) Dim WordApp As New Word.ApplicationClass() Dim missing As Object = System.Reflection.Missing.Value Dim fileName As Object = "C:\\template.dot" Dim newTemplate As Object = False Dim docType As Object = 0 Dim isVisible As Object = True Dim aDoc As Word.Document = WordApp.Documents.Add(fileName, newTemplate, docType, isVisible) aDoc.Application.Run("TemplateProject.Modules.TemplateForm") WordApp.Visible = True aDoc.Activate() End Subdavid wendel...
All-Star
15865 Points
2243 Posts
Re: Running a Word Macro with asp.net
May 26, 2008 02:04 PM|LINK
It's possible that the account the activity is running under does not have access to the c:\ directory.
Try moving the template into the website directory and see if that makes a difference.
Just a guess!
Nalum
Member
1 Points
23 Posts
Re: Running a Word Macro with asp.net
May 26, 2008 02:16 PM|LINK
Thanks for your reply.
I don't think it is an access problem as I have opened the file without trying to run the macro without any problems and as I am running the pages locally rather than remotely.
It's just when I try to run the macro that I get that error.
david wendel...
All-Star
15865 Points
2243 Posts
Re: Running a Word Macro with asp.net
May 26, 2008 02:30 PM|LINK
There are two files, the word document and the template document.
I would assume you are opening the word document, not the template doc. Where is the word document located? in C:\ also?
Maybe it's a permission's issue. Can you issue a word command that will change the document, and does that work?
When I really don't know why something is acting the way it is, I try to test as many things as I quickly can. Sometimes I get lucky.
Nalum
Member
1 Points
23 Posts
Re: Running a Word Macro with asp.net
May 26, 2008 03:14 PM|LINK
I'm opening the template file, it will be replacing the normal.dot template file for word so I wont be pointing to it when the script is live.
What do you mean by issue a word command to change the document?
david wendel...
All-Star
15865 Points
2243 Posts
Re: Running a Word Macro with asp.net
May 27, 2008 05:48 AM|LINK
In the ms word classes you are using, there has to be a way to change the contents of a document.
If you are successfully opening the document, the next step is to prove that you are allowed to change it and save it.
Nalum
Member
1 Points
23 Posts
Re: Running a Word Macro with asp.net
May 27, 2008 09:15 AM|LINK
Ok, I can change the content of the document with aDoc.Content.Text (and any other command within aDoc.Content) and can save the file with aDoc.SaveAs(saveFile) in asp.net.
I have made sure that I can see the file in the directory that I saved it to in the saveFile object variable.
Nalum
Member
1 Points
23 Posts
Re: Running a Word Macro with asp.net
Jun 06, 2008 03:19 PM|LINK
Any one have any ideas on why this isn't working?
JSawyer
Member
622 Points
94 Posts
Re: Running a Word Macro with asp.net
Jun 06, 2008 03:22 PM|LINK
Well, considering that running Word on a web server isn't supported and is not recommended in about the strongest terms possible ... I wouldn't really expect everything to work.
I'd guess that it has to do with macro security. But ... you shouldn't be doing this in the first place. Running a strictly desktop app on a web server can have bad bad bad consequences. Like ... what happens when Word pops up a modal dialogue in the server user context? You can't clear it.
Don't forget ... Mark as Answer if this helps!
DevBiker - my blog
Nalum
Member
1 Points
23 Posts
Re: Running a Word Macro with asp.net
Jun 06, 2008 04:09 PM|LINK
Hi thanks for your reply.
I am building this system for an intranet. It wont be publicly available. It will run word on the users machine rather than the server hosting the site.