Gudra
Gudra

Reputation: 70

Can I disable a Word button when opening a document from a console application?

I'm working with a console application on C#, that receives a document path and open it on Microsoft Word 2010

Word.Application oWordApp = new Word.Application();
        DisableSaveAsButton(oWordApp);
        oWordApp.Visible = true;

        try
        {
            Word.Document doc = oWordApp.Documents.Open(docFile);
            doc.Activate();
        } catch (System.Exception e)
        {
            Console.WriteLine("Error opening document\n"+ e.ToString() + "\n" +e.StackTrace);
        }

where docFile is the .doc file path.

I would like to open Word, and the Save As button to not be activate, greyed out and unusable. I found the DisableSaveAsButton method over there, it would be like this:

private void DisableSaveAsButton(Word.Application oWordApp)
    {
        Object MenuBar = 40;
        Object FileMenu = 1;
        Object SaveAsButton = 5;
        var saveAsBtn = oWordApp.CommandBars[MenuBar].Controls[FileMenu].accChild[SaveAsButton] as CommandBarButton;

        saveAsBtn.Enabled = false;
    }

But it wont work. From what I've read, most people find this solution by editing a Ribbon1.xml on their projects, but my program is a console application with Word functions, not a Word add-in, and it doesn't have any ribbon xml files. So I was wondering, it is posible to disable the Save As button from a console application (requirement from boss), instead of using an add-in with its own template?

Upvotes: 3

Views: 248

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25673

The code you found is old technology - pre-Word 2007. The CommandBar object has been superceded by Ribbon XML and the old commands for disabling functionality no longer work. This was a conscious design decision by Microsoft.

In the newer, Ribbon interface only code running in-process can also affect the user interface. So what you want to do is not possible, at least not without also loading a VBA or VSTO add-in.

If you don't want users to be able to save their work, then a "Reader" would appear to be a better approach than using the full-blown Word application, which is first and foremost an editor. Better, perhaps, to save the documents to PDF file format then open them in Acrobat Reader, for example.

Upvotes: 1

Related Questions