News:

Happy New Year!

Main Menu

Arbitrary layer selection for show/hide.

Started by wapperdude, December 19, 2024, 12:20:10 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wapperdude

There are 2 macros.  One to create a UserForm.  Presently, it is presumed to be UserForm1.  The 2nd macro brings up the UserForm for interactive usage.

Conceptually, this wasn't that difficult to imagine.  Implementing was another thing altogether.  Yes.  I did use AI a lot.  There's syntax I would never have known, including the need to incorporate the MS VBA Extensibility 5.3 library.

The idea was to build a UserForm and place checkboxes for however many layers are on the ActivePage.  Yes, if the ActivePage is changed, a new UserForm will be needed.  BAD news.  I could not determine how to reference the new UserForm programmatically.  It is hardcoded.  I consider this a VERY bad restriction.  Moving along.  There is code that is added to the UserForm to make it functional.  That was also a challenge. 

Anyway, the form is non-modal, so you may change selections and submit until the cows come home.  When they do, time to get off Visio and do some milking.  :o  ;D  ;D  ::)

The attached file already has UserForm1 constructed.  It should be deleted before creating a new one, as the code is preset to grab UserForm1.
Visio 2019 Pro

Thomas Winkel

Important to say that this requires to allow access to the VBA object model in the trust center.
Better do disallow afterwards.

Yacine

Yacine

Yacine

Quote from: Thomas Winkel on December 19, 2024, 08:41:29 AMImportant to say that this requires to allow access to the VBA object model in the trust center.
Better do disallow afterwards.
Right, but Wapperdude could also have provided a high enough amount of controls and switched off their visibility. Much safer.
Yacine

Thomas Winkel

#4
I think there is nothing wrong with this setting, if you are a developer.
But in addition to security concerns, normal users cannot be expected to make settings in the Trust Center for usability reasons.

However, as a helper during development, I find this an interesting approach.
In the past I used this to export / import my VBA code for version control.

wapperdude

#5
@Thomas, Yacine:  did you actually try it out?

@Thomas, Yacine:  The learning experience has been an adventure and an eye opener. Based upon your comments, I did some AI research.  Here's what I found with wrt to security settings.  First, yes, the Trust Center setting was enabled wrt VBA Projects.  I had forgotten. 

Is that setting necessary.  Here's the answer:
Without the "Trust access to the VBA project object model" setting enabled, you can:
a) Use existing UserForms.
b) Manually create, alter, remove UserForms.
c) Use UserForms to input and output values.

However, you cannot:
d) Use code (programmatically) to add, alter, or remove UserForms.

Yes, the enabled setting exposes additional security risks.  I just don't know how severe.

Conclusion, without the setting, it is not possible to dynamically achieve this task of having UserForm tailored to each, individual page requirements.
Visio 2019 Pro

wapperdude

@Thomas: 
QuoteBetter do disallow afterwards.
. Something only the User can do...both enable and disable.
Visio 2019 Pro

Thomas Winkel

#7
Quote from: wapperdude on December 19, 2024, 04:53:37 PM@Thomas, Yacine:  did you actually try it out?
Yes, I did 👍

Quote from: wapperdude on December 19, 2024, 04:53:37 PMYes, the enabled setting exposes additional security risks.  I just don't know how severe.
An attacker could load malicious code in text form and execute it.

But, you could also combine this with AI to generate and execute code.
Would be a nice Christmas project to develop the following function:

Option Explicit

Public Sub LetAiHandleIt(job As String)
    ' Your magic code here
End Sub

Private Sub TestLetAiHandleIt()
    LetAiHandleIt "Draw a Christmas tree."
End Sub

Start here:
ChatGPT in VBA
If anyone wants to do this I will provide an API key 🎅

Thomas Winkel

Here a demo how to create and run a macro dynamically:
Sub AddAndRunMacro()
    Dim code As String
    Dim vbProj As VBIDE.VBProject
    Dim vbComp As VBIDE.VBComponent
    Dim CodeModule As VBIDE.CodeModule
   
    code = _
        "Sub DynamicallyAddedMacro()" & vbCrLf & _
        "    MsgBox ""This code was added dynamically!""" & vbCrLf & _
        "End Sub"
       
    Set vbProj = ActiveDocument.VBProject
    Set vbComp = vbProj.VBComponents.Add(vbext_ct_StdModule)
    Set CodeModule = vbComp.CodeModule
    CodeModule.AddFromString code
    ActiveDocument.ExecuteLine vbComp.Name & ".DynamicallyAddedMacro"
End Sub

wapperdude

This update is for all of you Module fans.  It takes the previous development, relocates some subroutines from ThisDocument module to a new, ordinary module.  Code in the new module is treated as private because of global declaration of the module code. 

Because of the learning curve, this was, indeed, more than cut/paste.  It was also a solution the M$ Copilot failed to provide.

Visio 2019 Pro

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 336 (show)
Files included: 32 - 1207KB. (show)
Memory used: 1181KB.
Tokens: post-login.
Cache hits: 13: 0.00158s for 26,548 bytes (show)
Cache misses: 3: (show)
Queries used: 19.

[Show Queries]