skip to Main Content

I am trying to create a Word VSTO Add-In that adds a commandbar on VBE permanently.

My environment:

windows 10 (language: Traditional Chinese), office 365

Visual Studio Community 2022 (64-bit) – 17.5.2, .Net Framework 4.8

I have modified the macro settings in Word host applications as follows:

File -> Options -> Trust Center -> Trust Center Setttings -> Macro Settings ->checked “Trust Access to the VBA Project object model”.

I have added the following references:

Microsoft.Vbe.Interop

VB.NET Code:

Imports Microsoft.Office.Core

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Dim cb As CommandBar
        cb = Globals.ThisAddIn.Application.VBE.CommandBars.Add("testbar")
        cb.Visible = True
    End Sub

End Class

I have researched this issue and found that most solutions suggest checking ‘Trust Access to the VBA Project object model’ to allow the code to work.

However, despite enabling this checkbox, I still encounter the error message ‘Programmatic Access to Visual Basic Project is Not Trusted‘.

enter image description here

(By using VBA code, I am able to add a command bar on VBE, but it does not maintain permanently.)

I would appreciate any help in resolving this issue. Thank you.


update:

I have done some testing and found the following results:

Even if I haven’t added the Group Policy registry keys that Eugene Astafiev mentioned (highlighted in red box in the image below), as long as "Trust Access to the VBA Project object model" is checked in Trust Center, the Add-in can be built and run successfully, despite the error message "Programmatic Access to Visual Basic Project is Not Trusted" during the build process.

enter image description here

Checking Trust access to the VBA project object model and modifying Group Policy settings and Registry Key in Word, Excel, and PowerPoint doesn’t solve the error message problem.

2

Answers


  1. The command bar is added every time the addin is initialized. To do this you have to permanently add your addin to Word. This can e. g. done by installing and registering your AddIn in Word with an installer project.

    Login or Signup to reply.
  2. However, despite enabling this checkbox, I still encounter the error message ‘Programmatic Access to Visual Basic Project is Not Trusted’.

    You need to check other Trust center settings as well as group policy settings. For example, a corresponding group policy setting controls whether automation clients such as Microsoft Visual Studio Tools for Microsoft Office (VSTO) can access the Visual Basic for Applications project system in the specified applications. VSTO projects require access to the Visual Basic for Applications project system in Excel, PowerPoint, and Word, even though the projects do not use Visual Basic for Applications. Design-time support of controls in both Visual Basic and C# projects depends on the Visual Basic for Applications project system in Word and Excel.

    If you enable this policy setting, VSTO and other automation clients can access the Visual Basic for Applications project system in the specified applications. Users will not be able to change this behavior through the Trust access to the VBA project object model user interface option under the Macro Settings section of the Trust Center.

    If you disable this policy setting, VSTO does not have programmatic access to VBA projects. In addition, the Trust access to the VBA project object model check box is cleared and users cannot change it. Disabling this policy setting prevents VSTO projects from interacting properly with the VBA project system in the selected application.

    If you do not configure this policy setting, automation clients do not have programmatic access to VBA projects. Users can enable this by selecting the Trust access to the VBA project object model in the Macro Settings section of the Trust Center. However, doing so allows macros in any documents the user opens to access the core Visual Basic objects, methods, and properties, which represents a potential security hazard.

    Registry Hive HKEY_CURRENT_USER
    Registry Path softwarepoliciesmicrosoftoffice16.0wordsecurity
    Value Name accessvbom
    Value Type REG_DWORD
    Enabled Value 1
    Disabled Value 0
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search