Joss57
Joss57

Reputation: 467

Word Automation : could not open macro storage

My application (vb.net windows application deployed via ClickOnce) uses Word to open and fill .dot templates to create new Word documents. I reference Microsoft Word 14 Object Library and uses this code :

Dim oWord As Word.Application = Nothing
Dim oDoc As Word.Document = Nothing
Try
    oWord = New Word.Application
    Dim strFileName As String = ""
    Select Case strType
        Case "LettreReception"
            strFileName = Path.Combine(GetParam(1), "Template_LettreReception.dot")
            If File.Exists(strFileName) Then
                oDoc = oWord.Documents.Add(strFileName)

On the last line I receive "could not open macro storage" error on deployed machines (not on my development machine).

I develop with Windows 7 - Office 2010 - VS 2010 (.Net 3.5). My deployment machine is also a Windows 7 with Office 2010 installed.

I tried to remove normal.dotm (I found some links advicing it) without success. The .dot template used contains no macro.

Upvotes: 5

Views: 21661

Answers (6)

Sandeep Vaidya
Sandeep Vaidya

Reputation: 113

I had a similar problem. I closed all instances of Word and deleted the Normal.dotm file from this folder.

C:\Users<user>\AppData\Roaming\Microsoft\Templates

The file gets corrupted sometimes.

Upvotes: 0

Linus
Linus

Reputation: 107

enter image description here

Right click on the file that is opened -> Click the Unblock tick box -> Apply

Worked for me at least.

Upvotes: 3

Psychoceramics
Psychoceramics

Reputation: 45

Go to the Word document (if it's a template, be sure to open it, not create a new document with it) and disable Protected View: https://casecomplete.zendesk.com/hc/en-us/articles/200685047-Could-not-open-macro-storage

Upvotes: 0

user1379931
user1379931

Reputation:

"could not open macro storage" is telling you that VBA is looking for a particular structured storage file such as a .DOT or .DOC, and looking for the storage (a kind of stream within the file) in that file that contains the VBA code. If it can't open it, possible reasons include:

  • the container (the .doc/.dot) isn't there
  • the container cannot be opened with the caller's permissions
  • the container is there but the storage isn't there (e.g. on the target system there is a container with the expected name, but it contains no macros)
  • the container is there and the storage is there but cannot be opened with the caller's permissions

So one thing to do is to look through your project looking for anything it references (perhaps even other objects or DLLs that you specified via Tools->References) that is not also being delivered with your template.

Upvotes: 0

Oday Hazeem
Oday Hazeem

Reputation: 425

Check the properties of the word document and make sure the files are unblocked. Sometimes when you get the documents from a different computer or download them from the internet they will be blocked which will cause the the throwing of this exception "could not open macro storage"

Upvotes: 7

JPH
JPH

Reputation: 31

Because Word Interop is actually running behind the scenes as if it was running in an interactive session, certain permissions are required of the account used during execution.

Are you using Windows Authentication and impersonation in you web app? If so, the user being impersonated must have local log on rights to the server to run Word... In addition, you must actually log on to the server with that account at least once so that a profile exists on that machine for that user so the registry hive can be loaded. I've also found that you may need to actually run Word at least once as that user (to make sure any first-time initialization messages get taken care of before trying to run Word from code).

If not, then the service account that the application is running under (usually NETWORK SERVICE) requires the aforementioned permissions (which I will describe shortly) and you'll have to do something fancy like loading a registry hive dynamically at run-time. Personally, I prefer implementing an in-code temporary impersonation with a user account that has local log on permissions on the server in question.

Local log on permissions can be a bit tricky depending on your network and group policy configurations (if you want to be somewhat secure and not just use a Domain Admin account).

The reason everything works on your computer running in VS is because the context of the web application is YOUR user account - which, of course, has local log on permissions on your machine with a registry hive that can be loaded.

Now for the permissions:

  • First, you must run "dcomcnfg" on the server and make the following configuration change:

    • Right click on Component Services\computers\My Computer\DCOM Config\Microsoft Word 97 - 2003 Document and go to Properties
    • In the Properties screen, go to the Security tab and change the "Launch and Activation Permissions" to Customize.
    • Click the Edit button and add the local computer NETWORK SERVICE account (If not using impersonation... If using impersonation, add the appropriate user or group) to the list of users and check "Local Launch" and "Local Activation"
  • Make sure that the local computer NETWORK SERVICE account (If not using impersonation... If using impersonation, then the appropriate user or group) has appropriate read/modify permissions on the folder(s)and file(s) that you will be opening and/or saving to.

  • Create a "Desktop" directory under: C:\Windows\System32\config\systemprofile\ and give Full permissions to the local NETWORK SERVICE account (or the account that your ASP .NET application is running under) [NOTE: I believe this and the next step only apply if NOT using impersonation]

  • Give Modify/Read/Execute permissions on the C:\Windows\System32\config\systemprofile folder

Hope that helps some and wasn't too confusing...

Upvotes: 3

Related Questions