PDA

View Full Version : Solved: Store Strings in Macro



danvanf
06-19-2008, 02:59 PM
I am building an app that combines a 5000 word text list in various ways outputting millions of combinations. The text list is currently stored in a .txt file, where works just fine.

However, isn't there always a however.... I'd like to wrap this text file somehow in my project so when I lock it, the end user can't see my word list. (it would allow them to basically cheat)

Thanks !
Dan

Oorang
06-19-2008, 03:50 PM
In short, there isn't an "official way" to do that. VBA projects don't support resource files. (Unless someone knows something I don't). The only way I can think of to meet those parameters is non-trivial. You could encode the words in a bitmap and store the bitmap in the word file. But if you have never worked with data at a byte level you might find that one a little rough. If it were me I would just scramble the text file up a bit so it's not human-readable and then have your program unscramble it on the fly.

danvanf
06-19-2008, 08:09 PM
Darn, Shucks... All those hardend words.... I've done byte level stuff but it's been 10 years or so, noting like that, and my mind is twisted enough with this project already. I appreciate the expert advice. I suppose I should take solice in the fact that you didn't say rtfm. <smile> I've chosen an unelegent solution of creating 2 modules, one to hold 3000 words, and the other for the rest, seems there's a size limitation on modules somewhere around 3 and 4K. So now the text file is formatted like this
ObjectData(1) = "bla-word"
ObjectData(2) = "Sam-test"
up to ObjectData(5030).
And I paste it to a Module. I played with Containers, but I actually understand the array<smile> thanks again!
Dan

Oorang
06-19-2008, 09:02 PM
That's best case really. I would have suggested it, but I wasn't sure if they would all fit :) (Had the same problem myself a while back and they didn't all fit;))

Tinbendr
06-20-2008, 10:52 AM
just scramble the text file up a bit so it's not human-readable and then have your program unscramble it on the fly.I've done this in the past. Shift the ASC value of each letter by the length of the word worked for me.


So now the text file is formatted like this
ObjectData(1) = "bla-word"
ObjectData(2) = "Sam-test"
up to ObjectData(5030). I have something simlier (but smaller) and setup a element variable.

aCnt = 1
ObjectData(aCnt) = "bla-word": aCnt = aCnt + 1
ObjectData(aCnt) = "Sam-test": aCnt = aCnt + 1

This way, if I needed to insert another line in the middle, I wouldn't have to painfully renumber everything.

Oorang
06-20-2008, 11:10 AM
Yah, it's easy to ad-hoc something up as "better than nothing". The only thing I would say is "Don't believe it's encryption." :) It just not human readable without some effort.

The other concern, is that if you store the information in the module as string literals even if you password protect the document and the vbProject... The literals will not be encrypted. Meaning you can open up the document in notepad and read the list fairly easily. I don't know how advanced your cheaters are, but that seems like a low entry barrier to me :) (Which, btw, is one of the many reasons why you should never ever "hard code" a password.

So even if you store them in the module, you might consider obfuscating them a little:)

MOS MASTER
06-22-2008, 02:30 PM
Hi Dan, :hi:

Another alternative which is more hidden then the above alternatives is to store the values as metadata in the variables collection of the document.

Like:

ActiveDocument.Variables.Add Name:="Value1", Value:="1"
MsgBox ActiveDocument.Variables("Value1")


HTH

Oorang
06-22-2008, 05:24 PM
Hi Joost,
The only thing on that, is doc variables don't get encrypted either. They are still stored in the document (in plain text) for all to see. If you define a variable named "Foo" with a value of "Bar" then password protect and encrypt the document (and the vba project) it will still look like this in Notepad:

 Foo ?  Bar
So even if you do go that route, you will still want to obfuscate the value for storage to defeat the "notepad attack" :)


Edit:
Although as someone who spends their time in Access, Excel & Outlook, I was totally unaware that of the Document.Variables collection. I'm really glad to know about it. That would totally useful for storing persistent values without using the registry. And the fact that they stay in the document is even better then the reg (IMO) because the data can go with the document instead of being lost between computers. :thumb

MOS MASTER
06-23-2008, 09:47 AM
Hi Aaron, :yes


The only thing on that, is doc variables don't get encrypted either. They are still stored in the document (in plain text) for all to see. If you define a variable named "Foo" with a value of "Bar" then password protect and encrypt the document (and the vba project) it will still look like this in Notepad:

Sure, agreed in full there's no such thing as protection in Office so variables aren't an exception. (allthough they are a bit more hidden, when you don't know what your looking for)



Although as someone who spends their time in Access, Excel & Outlook, I was totally unaware that of the Document.Variables collection. I'm really glad to know about it.

Indeed, this is one of those unknown jems in MS Word that little know about. It's the best mechanisme for saving data as Metadata in the document that is transported with the document wherever it goes.

I'm glad I could share it with you! :thumb

fumei
06-23-2008, 10:04 AM
"It's the best mechanisme for saving data as Metadata in the document "

Is it not really the ONLY mechanism for storing persistent data in a Word doc? That is the purpose of the Variables collection. From Help:

"Document variables are used to preserve macro settings in between macro sessions."

Although, really, "settings" is too narrow a word. And not specific enough. Document Variables store strings.

Is there another way to hold persistent data? I mean, actually in a document.

MOS MASTER
06-23-2008, 10:10 AM
Hi Gerry, :hi:

No, it's not realy the only mechanisme to do this.
You also have something like Customdocument properties you can set on a document. (they can hold data as well)

But I like variables better because you can't see them. (easily) You can see custom document properties.

fumei
06-23-2008, 10:18 AM
Ummmmm.....






doh.

Yes CustomDocumentProperties will persist as well. And yes, of course you are right, they can definitely be considered as containers for persistent data.

I am like you, I prefer Variables, and for the same reason.

MOS MASTER
06-23-2008, 02:36 PM
Glad to here where on the same page Gerry! :yes (I wonder how many Word VBA coders already use these variables)

fumei
06-23-2008, 04:59 PM
Actually, I think a fair amount of them.