问题
I'm trying to create a macro that PopUp a toolBar containing button for a specific action but the code works fine and I tested every single line but when I run it a message from word PoPUP and prevent me to run this and I donno why
PS : I tried every signle solution in Google and forums
and I'm working with Word 2016 and word 2007
this is My code
Sub AddNewMB()
Dim myCommandBar As CommandBar, myCommandBarCtl As CommandBarControl
Dim myCommandBarSubCtl As CommandBarControl
On Error GoTo AddNewMB_Err
Set myCommandBar = CommandBars.Add(Name:="Airbus", Position:=msoBarTop,
menuBar:=True, Temporary:=False)
myCommandBar.Visible = True
myCommandBar.Protection = msoBarNoMove
Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
With myCommandBarCtl
.BeginGroup = True
.Caption = "UnGroup Shapes"
.Style = msoButtonCaption
.OnAction = "=UnGroupShapes_click()"
End With
Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
With myCommandBarCtl
.BeginGroup = True
.Caption = "Group Shapes"
.Style = msoButtonCaption
.OnAction = "=GroupShapes_click()"
End With
Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
With myCommandBarCtl
.BeginGroup = True
.Caption = "&Set Visibility Off"
.Style = msoButtonCaption
.OnAction = "=SampleMenuDisable()"
End With
Exit Sub
AddNewMB_Err:
Debug.Print Err.Number & vbCr & Err.Description
Exit Sub
End Sub
Function GroupShapes_click()
Dim MyMenu As CommandBar
Dim myCommandBarClickMe As CommandBarControl
Dim mydocument As Document
Set mydocument = ActiveDocument
On Error Resume Next
ActiveDocument.Unprotect 'enregistré manuellement
Dim S As Shape, t As Shape
On Error GoTo GroupShapes_click_Err
Set MyMenu = CommandBars("Airbus")
Set myCommandBarClickMe = MyMenu.Controls(2)
With CommandBars.ActionControl
End With
GroupShapes_click_Err:
Debug.Print Err.Number & vbCr & Err.Description
End Function
Function SampleMenuDisable()
Application.CommandBars("Sample Menu Bar").Visible = False
Application.CommandBars("Menu Bar").Visible = True
End Function
What I want is to prevent this to happen to every code I use and install this toolBar for everyword I use
this is my error
I need some help I'm stuck here thank's in advance
回答1:
Separate your macro from the issue that gives you trouble. You haven't established that the code you published is related to the error message you receive. Take a new document and add the following code to it.
Private Sub Test() MsgBox "This code is running." End Sub
Save the document as macro enabled. Try to run the code. If it runs, close the document, quit Word. Restart Word, load the document, try to run the code again. If the code runs in one or both of these scenarios the fault must be with the document that contains the code you posted above or with the code itself. Post the result of your above test here and we can look for the error there.
- Go to File -> Options -> Trust Center -> Trust Center Settings. Select 'Macro Settings' on the left. You will see a number of radio buttons. Select the one which says "Enable all macros (not recommended; potentially dangerous code can run)". From your description, this is the setting to blame for your problem. If the review of this setting doesn't solve your problem please post a screenshot of this setting dialog box.
来源:https://stackoverflow.com/questions/46286742/macro-is-disabled-by-word-confidentiality