Has anyone figured out a way to have a macro read and change the setting for "Keep Desktop Objects on Layer" which is found on the Objects docker, "Gear"/settings menu? I would love to be able to check and change this using VBA.
If I understand correctly, "Keep Desktop Objects on Layer" is an application preference. As an example, I see in the Windows Registry a key named KeepDesktopObjectsOnLayer under Computer\HKEY_CURRENT_USER\SOFTWARE\Corel\CorelDRAW\23.0\Draw\Application Preferences\VGDoc Pref Settings.
As an aside: I really, really wish that they had made that a document property instead of an application preference.
I think that there is something in the API for checking and changing application preferences. See Application.GetApplicationPreferenceValue method and Application.SetApplicationPreferenceValue method.
I have messed aroud a little bit trying to use those methods for other settings, but never achieved success. If you learn more about using those methods, then please share, as I would love to learn more about them.
Eskimo, I'm almost there. You're discovery of the Registry value for this setting was the key. After seeing that I thought, Hmm, I wonder if there is a way to edit Registry settings using VBA. Turns out, there is!
I wrote a quick macro that will read the Registry value for "KeepDesktopObjectsOnLayer" and then toggle it on or off based on what it finds. And it actually works. If you paste the script below into a module and step through the code using F8, you can see it do its thing. And if you have the Registry Editor open to this key and press F5 to refresh the window after running the script, you can see that it has changed the value.
Unfortunately, CorelDraw isn't aware that the setting was changed externally, so it remains the same. Closing CorelDraw will also overwrite the Registry value with whatever it was left as in the program.
So I'm wondering if you can figure out away to refresh/reload the application settings (or at least that value) with the update from the Registry. That will be the solution.
Here is my script:
Sub ToggleKeepDtObjOnLayer()'Toggle the CorelDraw application setting: Keep Desktop Objects on LayerOn Error GoTo OopsDim WS As ObjectDim RegKey As String, Value As Variant, sType As String'REG_SZ A string'REG_DWORD A Number An integer'REG_BINARY A binary value An integer'REG_EXPAND_SZ An expandable string (e.g., "%windir%\\calc.exe") A stringSet WS = VBA.CreateObject("WScript.Shell")RegKey = "HKEY_CURRENT_USER\SOFTWARE\Corel\CorelDRAW\23.0\Draw\Application Preferences\VGDoc Pref Settings\KeepDesktopObjectsOnLayer"If WS.RegRead(RegKey) = 0 ThenValue = 1ElseValue = 0End IfsType = "REG_SZ"WS.RegWrite RegKey, Value, sType
Done:Set WS = NothingExit Sub
Oops:MsgBox Err.Number & ": " & Err.DescriptionResume DoneResume 'For debuggingEnd Sub
The registry entry is probably just a place where CorelDraw saves setting after the program exits. And changing this value will not affect the current state.
CorelDraw certainly saves the setting in the Registry when it closes, but it must also read it from there when it opens. My hope is that I can find a way to make it read and update this one setting while already running without having to do the equivalent of reloading an entire Workspace full of settings.
Try this:
Application.FrameWork.Automation.InvokeItem "53861e80-f191-4a0f-beaf-99c4f20aee44"
I ran this code twice and each time it toggled the "KeepDesktopObjectsOnLayer" state
Hi shark.
The line of code that you provided:Application.FrameWork.Automation.InvokeItem "53861e80-f191-4a0f-beaf-99c4f20aee44"is able to toggle the state of the setting: KeepDesktopObjectsOnLayer, but I have not been able to figure out a way for VBA to determine the current state of that setting. I've tried just about every method of "FrameWork.Automation", and numerous attempts using "GetApplicationPreferenceValue" but without any luck.
Is there a way using "Automation" to get the current value of this setting so that my code will 'know' if it needs to be toggled? Or how do I figure out what 'GroupName" this setting belongs to so that I can get its Preference Value? I haven't been able to find any detailed documentation on either of these methods. Any help would be greatly appreciated.
This feature and the objects docker were implemented poorly and have been a source of problems since it was put on line.
Hi Ric.
Sorry, I can't help on this issue. You can get a list of menu items, but unfortunately there is no switch value there.
Hi Ric.I finally solved your issue. Below is a great video from CorelCS and there is sample code to get/set the value you need (at 3:03). It works for VBA.
There it was this whole time. Thank you, shark. I had watched through that video several times but couldn't get it to work until now.
Here is my revised script, if anyone else is interested.
Sub KeepDTObjectsOnLayer(bKeep As Boolean)'bKeep (True/False): Keep Desktop Objects on Layer (On/Off)On Error GoTo OopsDim ObjMgr As ObjectDim bCurrent As Boolean
Set ObjMgr = FrameWork.Application.DataContext.GetDataSource("WObjectManagerDS") bCurrent = ObjMgr.GetProperty("KeepDesktopObjectsOnLayer") 'Retrieve the current setting for this property, if desired. ObjMgr.SetProperty "KeepDesktopObjectsOnLayer", bKeep
Done: Set ObjMgr = Nothing Exit Sub
Oops: MsgBox Err.Number & ": " & Err.Description Resume Done End Sub