Carly Bond

VB.Net – Connection to Microsoft Excel Issue

Over the last week or so, I have been learning VB.Net. Now I am very new to this, and although I have got 10 years of VBA for Applications behind me, any new language is difficult to pick up. For me, anyway!

The job I needed to do, is work through a list of files & folders and amend a small amount of VBA code from one thing to another.  Given a starting point from Mike Hudson, I started to look into how it all worked & fitted together.  My first stumbling block was being able to connect to Excel.  I kept getting the following message:

HRESULT = 0x800A17B4 (-2146822220) “Programmatic Access to Visual Basic Project is not trusted”

I searched on the Internet to find that there was a setting in Excel that I needed to select before I could continue.  One which you can solve by following these easy instructions:

Office 2003 & Office XP

  1. Open the Office 2003 or Office XP application in question.
  2. Go to the Tools menu.
  3. Click Macro.
  4. Click Security to open the Macro Security dialog box.
  5. Go to the Trusted Sources tab.
  6. Click to select the Trust access to Visual Basic Project check box to turn on access.
  7. Click OK to apply the setting.
  8. You may need to restart the application for the code to run properly if you automate from a Component Object Model (COM) add-in or template.

Office 2007

  1. Open the 2007 Microsoft Office system application in question.
  2. Click the Microsoft Office button.
  3. Click Application Options.
  4. Click the Trust Center tab
  5. Click Trust Center Settings.
  6. Click the Macro Settings tab.
  7. Click to select the Trust access to the VBA project object model check box.
  8. Click OK.
  9. Click OK.