admin管理员组

文章数量:1336728

I have several functions and subs (appoximately 7) that use the following two lines.

Dim oFolder As Folder
Set oFolder = Application.Session.Folders("Rings").Folders("Contacts").Folders("Customers")

I'd like to create a Private Constant for the module to take the place of those two lines. I've tried Private Const oFolder as Folder = Application.Session.Folders("Rings").Folders("Contacts").Folders("Customers"), but that gives me a compile error: Expected: type name.

Is this even possible?

I have several functions and subs (appoximately 7) that use the following two lines.

Dim oFolder As Folder
Set oFolder = Application.Session.Folders("Rings").Folders("Contacts").Folders("Customers")

I'd like to create a Private Constant for the module to take the place of those two lines. I've tried Private Const oFolder as Folder = Application.Session.Folders("Rings").Folders("Contacts").Folders("Customers"), but that gives me a compile error: Expected: type name.

Is this even possible?

Share Improve this question asked Nov 19, 2024 at 22:40 Nathan GuillNathan Guill 758 bronze badges 2
  • 2 The best alternative would be to create a function called oFolder and have it return the value you want. – braX Commented Nov 19, 2024 at 22:45
  • 2 Constants are assigned at compile-time, so any assignment which is not evaluable except at run-time will get flagged. See previously: stackoverflow/questions/29614384/… – Tim Williams Commented Nov 19, 2024 at 22:56
Add a comment  | 

3 Answers 3

Reset to default 2

Creating the below function worked great. Didn't think about this route when I posted the question.

Private Function CustomerFolder() As Folder
  Set CustomerFolder = Application.Session.Folders("Rings").Folders("Contacts").Folders("Customers")
End Function

Per the Documentation, Constants can be declared as: Byte, Boolean Integer, Long, Currency, Single, Double, Decimal¹, Date, String, or Variant`

Note, first, that Folder is not in that list — so, no, it is not possible.

Note, second, that those are all Data Types that you assign values to, and not ones that you Set to an Object Reference. So, no, it's doubly not possible — because Folder is an object type, not a value type.


¹ Decimal Constants are not supported in all versions of Visual Basic, so I would advise sticking with Single or Double instead

No, it is not possible - you have a sequence of methods calls, not a constant variable. Unless you create a function that takes a string, parses it, and then opens the folders recursively. You can then hardcode the function argument - a string like "\\Rings\Contacts\Customers"

本文标签: vbaPrivate Const oFolder as FolderStack Overflow