Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Insert random quote from txt file?

1 view
Skip to first unread message

Marc Ries

unread,
Jan 10, 2002, 3:23:34 PM1/10/02
to
Ok, I've googled the web, Microsoft, the word MVP site, etc. and have not
tracked down a macro to do the following:

I have a notepad file (but it could be changed to a word file) that contains
a large number of quotations, one per line in the text file (i.e., EOL
delimited). I would like to be able to add an auto-update field (e.g., like
a "CURRENT DATE" field) to a document so that when the document is opened, a
new and random line of text (i.e., quotation) is inserted into this field at
either the bottom of the page or a footer.

I DO NOT want to import the quotation from an Access database or Excel
spreadsheet and I don't want to have to do a "Mail Merge".

If anyone can offer a solution, pointers, tips, etc. to get this task
accomplished, I would greatly appreciate it.

Thank you in advance,

Marc Ries


Dave Lett

unread,
Jan 10, 2002, 4:25:29 PM1/10/02
to
Hi Marc,

If you change the text file to a Word document, then the process would be
easier (for me at least). Let's say that you have "TextFile.doc" that
contains your quotations; you also have the "Trial.doc" file. In
"Trial.doc", I suggest that you put a bookmark where you want the random
quotation to insert (your quotations might be longer than 256 characters).
Create an AutoOpen macro that generates a random number from 1 to number of
paragraphs in "TextFile.doc".
Open "TextFile.doc", with its invisble property set to true (W2K only).
Copy the paragraph range specified from the Random number generator.
Close "TextFile.doc.
Insert the copied paragraph in the bookmarked range.
Reinsert the bookmark name (so that it's there the next time you open the
document).

HTH

"Marc Ries" <ri...@wmni.net> wrote in message
news:OA58DThmBHA.2460@tkmsftngp04...

Astrid

unread,
Jan 10, 2002, 4:16:19 PM1/10/02
to
Hi Marc,

Now there's a nice challenge ;-)
First thing that coms to mind is to have a macro that runs whenever a
document based on the template with the macro is opened (AutoOpen) or made
(AutoNew) that reads a value from your inifile with quotations and stores
that in a documentvariable for the document.
In the document itself you could use a built-in Word field of the type
documentproperty that shows the value of the documentproperty.

Here's an example.
There's only one gotcha. Instead of documentproperties (for which the Word
field only shows the first 127 characters, you could use also
documentvariables which don't have this problem. Only problem with
documentvariables is, that at least in Word 97 there was a problem with them
if you used documentvariables fields inside a header or footer of the
document.
So you need to decide on which one to use, depending on location of the
field (the quotation itself in the document) and the version you're going to
use.

-----------------------------------------------------
INI-File looks like:
[Quotations]
TotalNumber = 5
1 = First quotation
2 = Second quotation
3 = Third quotation
4 = Fourth quotation
5 = Fifth quotation

<following code in your template, code assumes that the name of the ini file
is Quotation.ini and that the file is stored in the folder of the
usertemplates (Tools - Options - FileLocations - Usertemplates>

Sub AutoNew()
Const csIniFile As String = "Quotation.ini"
Const csSection As String = "Quotations"
Const csTotal As String = "TotalNumber"
Const csDocPropName As String = "Quotation"
Dim sIniLocation As String
Dim sQuote As String
Dim iRandomNo As Integer
Dim iTotal As Integer

On Error GoTo ErrHandler
sIniLocation = Options.DefaultFilePath(wdUserTemplatesPath)
If Right(sIniLocation, 1) <> Application.PathSeparator Then
sIniLocation = sIniLocation & Application.PathSeparator
End If

'Get number of quotations from the inifile
iTotal = Val(System.PrivateProfileString(sIniLocation & csIniFile,
csSection, csTotal))

If iTotal > 0 Then
'Get a random number between 1 and the number of quoations in the file
Randomize
iRandomNo = Int(iTotal * Rnd + 1)

'Get the quote
sQuote = System.PrivateProfileString(sIniLocation & csIniFile,
csSection, CStr(iRandomNo))
Debug.Print sQuote

'Write to documentproperty
ActiveDocument.CustomDocumentProperties.Add _
Name:=csDocPropName, LinkToContent:=False, Value:=sQuote, _
Type:=msoPropertyTypeString

'Update fields in document
ActiveDocument.Fields.Update

End If
Exit Sub

ErrHandler:
Select Case Err
Case Else
'Documentproperty was already in the document
'set the new value and resume next command
ActiveDocument.CustomDocumentProperties(csDocPropName).Value = sQuote
Resume Next
End Select
End Sub

In the template add a field that shows the result of the documentproperty:
{ DOCPROPERTY "Quotation" }
To insert the brackets use Ctrl + F9

-----------------------------------------------------
If you want the quotation to update also when a document based on this
template is opened, create another macro with the exact same code but name
it AutoOpen.

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to
the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Marc Ries" <ri...@wmni.net> schreef in bericht
news:OA58DThmBHA.2460@tkmsftngp04...

Marc Ries

unread,
Jan 10, 2002, 9:51:05 PM1/10/02
to

>
> 'Get number of quotations from the inifile
> iTotal = Val(System.PrivateProfileString(sIniLocation & csIniFile,
csSection, csTotal))
>

Thanks for the quick feedback. I'm working on it, but get a SYNTAX ERROR
pointing to the
above statement. I am using Word 2002.

Thanks again,

Marc

Marc Ries

unread,
Jan 10, 2002, 11:38:38 PM1/10/02
to
Oh, well, what ever it was it's working now!

Thanks a bunch... you have made my day...

Marc

0 new messages