0

I've modified the code for UN-protecting Excel VBA Project to work with Microsoft Project from the link below.

Unprotect VBProject from VB code

But in MS Project there is always a Global.mpt(my code runs from here) and when the below line executes

projAp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute

it always opens the VBA Project Properties of the Global.mpt file.

How do I select my Project Plan's VBA Project Properties? Is there a Windows API function that would let me choose the second VBA Project Shown in the screen host. Launching the FindControl command after selecting the proejct should launch it corresponding VBA Project Properties(Manually tried this).

Screen Shot of Project VBA

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
RamY
  • 492
  • 1
  • 5
  • 20
  • I am not sure I understand but I think you need to iterate through VBProjects to get references to the right one since you are qualifying your `projAp` as VBIDE.Project possibly –  Mar 12 '14 at 12:34
  • @mehow I tried that now: `debug.Print projAp.ActiveProject.VBProject.VBE.VBProjects.Item(1).FileName` _C:\Users\IBM_ADMIN\AppData\Roaming\Microsoft\MS Project\14\1033\Global.mpt_ `debug.Print projAp.ActiveProject.VBProject.VBE.VBProjects.Item(2).FileName` _C:\Users\IBM_ADMIN\Desktop\Project Plan v2.46.mpt_ `projAp.VBE.VBProjects.Item(2).VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute` Even if I explicitly select the second VBAProject as in code snippet above the first VBA Project Properties only opens again. – RamY Mar 12 '14 at 13:19

2 Answers2

1

The trick is to switch over to the Project pane and "type" the first letter of the project name; I suggest you change it from the default VBAProject if possible.

SendKeys "%{F11}"
SendKeys "^r"
SendKeys "V"
DoEvents
VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
Rachel Hettinger
  • 7,927
  • 2
  • 21
  • 31
  • I have tried this and it works,but not always. And I don't want to use Send keys as they don't work in all scenarios. Is there any other way to switch to the Project Pane and select the next Project? – RamY Apr 04 '14 at 09:56
  • The VBE does not expose any methods to select/activate a project. An alternative would be to enlist your users for assistance. Perhaps if the SendKeys fails, that could be the backup plan. – Rachel Hettinger Apr 04 '14 at 11:17
0

projAp.VBE.CommandBars(1).

Your code is always working with the application object, so is always looking in the Global.mpt file. try:

ActiveProject.VBProject.VBE.CommandBars(1)

It should return only items for the project.

Rod Gill
  • 31
  • 2