How to disable "Save As" menu option from a C# console app?

Java, C++, C#, Delphi, ??? - Using the UNO bridges

How to disable "Save As" menu option from a C# console app?

Postby Gudra » Thu Apr 05, 2018 11:41 am

So, I want to disable a single menu option from Office when I launch a new document. Most of what I've read redirect me to this, which is a monstruous Java example I'm not sure I understand fully (I'm new with the API). I tried to convert most of the code there, but I don't know how to convert this line

Code: Select all   Expand viewCollapse view
XComponentContext xLocalContext =

Also I have a problem with some objects and uno.Any. The converted code, not working, I have right now would be:

Code: Select all   Expand viewCollapse view
        private void disableCommands()
            string[] aCommandURLTestSet = { "Open", "About", "SelectAll", "Quit", "SaveAs" };

            // Set the root path for our configuration access
            PropertyValue[] lParams = new PropertyValue[1];
            lParams[0] = new PropertyValue();
            lParams[0].Name = "nodepath";
            lParams[0].Value = new uno.Any("/org.openoffice.Office.Commands/Execute/Disabled");
            XComponentContext xRemoteContext = null;
            XMultiComponentFactory xRemoteServiceManager = null;
            XURLTransformer xTransformer = null;
            //      // Create configuration update access to have write access to the configuration
            // connect
            XComponentContext xLocalContext = createInitialComponentContext(null); //this wont work obviously
            XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
            Object urlResolver = xLocalServiceManager.createInstanceWithContext(
                "", xLocalContext);
            XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver)urlResolver;
            Object initialObject = xUnoUrlResolver.resolve(
            XPropertySet xPropertySet = (XPropertySet)initialObject;
            Object context = xPropertySet.getPropertyValue("DefaultContext");
            xRemoteContext = (XComponentContext)context;
            xRemoteServiceManager = xRemoteContext.getServiceManager();
            Object transformer = xRemoteServiceManager.createInstanceWithContext(
                "", xRemoteContext);
            xTransformer = (XURLTransformer)transformer;

            Object configProvider = xRemoteServiceManager.createInstanceWithContext(
                "", xRemoteContext);
            XMultiServiceFactory xConfigProvider = (XMultiServiceFactory)configProvider;
            Object xAccess = xConfigProvider.createInstanceWithArguments("", lParams);

            XSingleServiceFactory xSetElementFactory =

            XNameContainer xNameContainer = (XNameContainer)xAccess;

            if (xSetElementFactory != null && xNameContainer != null)
                Object[] aArgs = { };

                for (int i = 0; i < aCommandURLTestSet.Length; i++)
                    // Create the nodes with the XSingleServiceFactory of the configuration
                    Object xNewElement = xSetElementFactory.createInstanceWithArguments(new uno.Any(aArgs)); //can't use aArgs as an argument for uno.Any()
                    if (xNewElement != null)
                        // We have a new node. To set the properties of the node we need
                        // the XPropertySet interface.
                        XPropertySet xPropertySet1 = (XPropertySet)xNewElement;

                        if (xPropertySet1 != null)
                            // Create a unique node name.
                            String aCmdNodeName = "Command-";
                            aCmdNodeName += i;

                            // Insert the node into the Disabled set
                            xPropertySet.setPropertyValue("Command", new uno.Any(aCommandURLTestSet[i]));
                            xNameContainer.insertByName(aCmdNodeName, new uno.Any(xNewElement)); //can't use aArgs as an argument for uno.Any()

                // Commit our changes
                XChangesBatch xFlush = (XChangesBatch)xAccess;

I'm not sure if this is the way to go with this problem, to be quite honest. I'm totally lost.
Posts: 3
Joined: Thu Apr 05, 2018 10:56 am

Re: How to disable "Save As" menu option from a C# console a

Postby Zizi64 » Mon May 21, 2018 12:51 pm

I want to disable a single menu option from Office when I launch a new document.

Do you want to hide it only from the menu an from the toolbar and from the hotkeys, or do you want really disable it?

My first tip:
Just create a template file (manually), with unvisible "Save as..." menu item, and without "Save as..." toolbar icon by usage the Customize feature. Then use that template in your program (load it instead of a new, empty document).

And some questions:
Why you want to do it?
What about the normal Save function and the simple "copy + rename" feature of the operating system? Everybody can achieve a virtual "Save as..." by these functions... And everybody can switch ON the invisible functions...
Tibor Kovacs, Hungary; LO4.4.7, LO5.4.6 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.0.2 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Posts: 6595
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Return to External Programs

Who is online

Users browsing this forum: No registered users and 0 guests