Constants as arrays?

C

Charlotte E

Hello,


Is there a way to make Constants as Arrays?

Something like:

Public Const Doc(1) As String = "Rules.DOC"
Public Const Doc(2) As String = "Personal.DOC"
Public Const Doc(3) As String = "Reporting.DOC"

etc...


Currently I'm using:

Public Const Doc_01 As String = "Rules.DOC"
Public Const Doc_02 As String = "Personal.DOC"
Public Const Doc_03 As String = "Reporting.DOC"

....but that doesn't allow me to run through the documents in a loop, like a
For-To-Next.


Any suggentions?


TIA,
 
M

Mike H

Charlotte,

You could do something like this

Sub sonic()
Dim WordApp As Object, WordDoc As Object
Set WordApp = CreateObject("Word.Application")
mypath = "C:\"
MyArr = Array("Rules.doc", "Personal.doc", "Reporting.doc")
For x = LBound(MyArr) To UBound(MyArr)
WordApp.documents.Open mypath & MyArr(x)
WordApp.Visible = True
'do things
Next
End Sub


Mike
 
P

Peter T

Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim v
For Each v In Split(cDOCS, "\")
MsgBox v, , "For Each"
Next

' or
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i ="
Next

End Sub

Regards,
Peter T
 
K

keiji kounoike

I might misunderstand the meaning of Charlotte's "To make Constants as
Arrays". But I can change elements of the constant array(arr). I wonder
this could be called as a constant array.


Const cDOCS As String = "Rules.doc\Personal.doc\Reporting.doc"

Sub test()
Dim i As Long, arr
arr = Split(cDOCS, "\")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i = " & i
Next

For i = 0 To UBound(arr)
arr(i) = Replace(UCase(arr(i)), "R", "A")
Next

For i = 0 To UBound(arr)
MsgBox arr(i), , "After Modifing For i = " & i
Next

End Sub

Keiji
 
P

Peter T

Constants can only refer to a value, not an array or object. The example I
posted makes a variable array from the constant string using the Split
function with the delimiter "\". After making the variable array from the
fixed constant you can of course change the array as you wish.

Regards,
Peter T
 
K

keiji kounoike

In my thought, if you say a variable is constant, you cannot change it's
value. In some other languages like C, you can make a constant array. In
VBA, it seems to be impossible to make a constant array. then, I wonder
why the poster needs the constants in this case. I think a code like
this would be enough.

Dim arr as Variant

Sub test()
arr = Array("Rules.DOC", "Personal.DOC", "Reporting.DOC")
For i = 0 To UBound(arr)
MsgBox arr(i), , "For i = " & i
Next
End Sub

Keiji
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top