vba - Private Const oFolder as Folder - Stack Overflow

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

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"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742394205a4435614.html

相关推荐

  • vba - Private Const oFolder as Folder - Stack Overflow

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

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信