PDA

View Full Version : rewrite code (or array) on the fly



OTWarrior
02-27-2008, 08:10 AM
I am trying to change the values of an array, which are written in the vba code of the word document.

I would like to be able to change these values based on what the user inputs (I am using an inputbox going into a variable).

for example:
Data = Array("item1", "item2", "item3", "item4", "Main List...")
Data(0) = InputBox(Message, "Add custom list")


say the user puts "First Item" into the inputbox when prompted, I want the actual code to change to:

Data = Array("First Item", "item2", "item3", "item4", "Main List...")
Data(0) = InputBox(Message, "Add custom list")


I know I could store the values in an external txt file, database file or within the document itself in a textfield, but I would like to try and keep the values hidden from the user.

Any ideas? As I am stumped and not even sure if such a thing is possible (although I did recently discover I can rewrite queries on the fly in Access :))

TonyJollans
02-27-2008, 10:03 AM
I'm not entirely sure what you want here. It is possible to change VBA code but, in recent versions of Office, it requires that the user allows access (and this is not set by default).

Another way might be to use document variables.

Norie
02-27-2008, 10:24 AM
OTWarrior

You can use code to alter/write code but it's normally not recommended.

Mainly for security reasons - like Tony has implied, I think anyway :), settings need changing.

How would storing the values in a text file not hide them from the user?

fumei
02-27-2008, 12:07 PM
I am not following. You can change the value of the array.
Dim Data()
Dim var
Data = Array("item1", "item2", "item3", "item4", "Main List...")
For var = 0 To UBound(Data)
MsgBox Data(var)
' displays "item1", "item2".....
Next

Data(0) = InputBox("Add custom list")
' type "yadda yadda" into inputbox

For var = 0 To UBound(Data)
MsgBox Data(var)
' displays "yadda yadda", "item2".....
Next
I am not following why you are trying to change the string IN the original array declaration, i.e change:


Data = Array("item1", "item2", "item3", "item4", "Main List...")


to

Data = Array("yadda yadda", "item2", "item3", "item4", "Main List...")

OTWarrior
02-27-2008, 02:33 PM
Fumei:
I know you can change the value of an array, but the next time you load the document, it won't be there (or even, when the proceedure is over). The whole point of this is to make the code I currently have, easy to use by someone who is not a programmer, which is why I want the user to be able to change (the parts of) the code (that i designate) in order to permanently change the array.

I want to make this document into a blank state with the code held within it, so anyone can take a new document (effectively) and start using the code I have made immediately, just by opening this file and creating a document howevere they like.

Hope that makes sense?

Tony & Norie:
How would I be able to change the code on the fly like you guys suggest I am able to do? (what is the technique). That sounds like what I am after. having the array in a text file wouldn't make the document self contained, as a user could go to the txt file and change any values they wished. I want it to be easy to change for the original author of the document, but hard for a standard user.

TonyJollans
02-28-2008, 10:04 AM
I could not recommend this technique for use outside your own environment but this fairly simple example should get you started:


Sub MyProc()
Dim ThisModName As String: ThisModName = "Module1"
Dim ThisProcName As String: ThisProcName = "MyProc"
Dim CM As CodeModule
Dim CodeWords() As String
IdLabel1: Data = Array("item1", "item2", "item3", "item4", "Main List...")
Data0 = InputBox(Message, "Add custom list")
Set CM = ThisDocument.VBProject.VBComponents(ThisModName).CodeModule
For ndx = CM.ProcBodyLine(ThisProcName, vbext_pk_Proc) _
To CM.ProcStartLine(ThisProcName, vbext_pk_Proc) _
+ CM.ProcCountLines(ThisProcName, vbext_pk_Proc)

If CM.Lines(ndx, 1) <> "" Then
If Split(CM.Lines(ndx, 1), ":", 2)(0) = "IdLabel1" Then
CodeWords = Split(CM.Lines(ndx, 1), """", 3)
CM.DeleteLines ndx, 1
CM.InsertLines ndx, CodeWords(0) & """" & Data0 _
& """" & CodeWords(2)
End If
End If

Next
End Sub



Some notes:
A procedure starts on the line after the previous one ends.
It includes all blank lines and comments before it.
The last procedure in a module also includes blank lines and comments after itself.
ProcBodyLine gives you the line the Sub (or whatever) statement is on.
You will need a reference to the VB extensibility library.

fumei
02-28-2008, 04:04 PM
I do not understand why you need to have the user change things "in order to permanently change the array."

It is the permanent I am wondering about. WHY??

It is not even permanent anyway...as they could change it again.