-->

How can I put spaces in my VBA project name?

2019-07-28 03:41发布

问题:

I have an Excel add-in I wrote in VBA, let's call it MyAddin.xlam.
I have set the project name as MyAddin.

When I open a second project and choose Tools->References..., MyAddIn appears just fine. Everything works. That's great.

But.. for purely aesthetic reasons, I would like the listing to appear as My Addin in the list of References. Pretty much everything available in the list of potential References has spaces in the name.
But if I try to rename my project to have a space in the name, VBA doesn't allow me to do that.

So, silly as the question may sound, is there any way I can get spaces into the displayed name of my VBA add-in in the "References" list?

I'm perfectly willing to accept the answer "This isn't possible in VBA; you can only put spaces in the reference name if you build the add-in with another language", but I'm wondering if I am missing something simple.

Thanks!

EDIT, Addendum: Essentially, my question is: Is there a way to get the "displayed name" to be different from the project name, which doesn't care about filenaming/object naming restrictions?

回答1:

Yup you are right. VBA will not let you insert spaces in the Project Name in the Properties window.

If you try to, it will give you an error (see snapshot)

Well No Luck with VB.net as well. I added the space and it converted it to an underscore in Excel. let me test it more and get back to you...

SNAPSHOT

FOLLOWUP

Phew Finally! Yes it is possible in VB.Net. Let me know if you want more details?



回答2:

You can vary the display name as it appears in the Add-ins window by editing the Title Built-in Document Property, but that doesn't help you with the name in the References window

The VBE won't let you use illegal characters in a project name of module name, but that doesn't mean it can't be done.

For example, the Analysis Toolpak - VBA add-in ATPVBAEN.XLAM has a project name of atpvbaen.xls which includes the supposedly illegal character .

Likewise, the same project has a module called VBA Functions and Subs which includes the illegal (space) character.

But the public procedures in Analysis Toolpak are still callable by using square brackets.

Sub test()

  'Run the auto_open macro in Analysis Toolpak
  [atpvbaen.xls].[VBA Functions and Subs].auto_open

End Sub

So Microsoft knows how to use illegal characters in project and module names, which is presumably done in the binary or with a special build of VBE.

EDIT

I just edited the binary of a VBA project and was able to put spaces in the VBA project name!

Interestingly, you can't export and then reimport the modules from Analysis Toolpak, as VBE still enforces the naming rules on import. Good luck getting that to work with Source Control.

EDIT: 3 FEB 2017 From the VBA file format spec: [MS-OVBA]

[module name] SHOULD be an identifier... MAY be any string of characters... MUST be less than or equal to 31 characters long.

[project name] SHOULD be an identifier... MAY be any string of characters... MUST be less than or equal to 128 characters long.

So it seems the VBE is imposing the SHOULD rule, but VBA is actually more permissive, if you are able to edit the project binary.