How to disable "Save As" menu option from a C# console app?
Posted: 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
Also I have a problem with some objects and uno.Any. The converted code, not working, I have right now would be:
I'm not sure if this is the way to go with this problem, to be quite honest. I'm totally lost.
Code: Select all
XComponentContext xLocalContext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
Code: Select all
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(
"com.sun.star.bridge.UnoUrlResolver", xLocalContext);
XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver)urlResolver;
Object initialObject = xUnoUrlResolver.resolve(
"uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager");
XPropertySet xPropertySet = (XPropertySet)initialObject;
Object context = xPropertySet.getPropertyValue("DefaultContext");
xRemoteContext = (XComponentContext)context;
xRemoteServiceManager = xRemoteContext.getServiceManager();
Object transformer = xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.util.URLTransformer", xRemoteContext);
xTransformer = (XURLTransformer)transformer;
Object configProvider = xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.configuration.ConfigurationProvider", xRemoteContext);
XMultiServiceFactory xConfigProvider = (XMultiServiceFactory)configProvider;
Object xAccess = xConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", lParams);
XSingleServiceFactory xSetElementFactory =
(XSingleServiceFactory)xAccess;
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;
xFlush.commitChanges();
}
}