ok, let me elaborate. i'm wanting to have a macro (in code, textbox, etc.) to replace it with a random line from a file...
so here's what i have, %wow.txt% replaces it with a random line from \macros\wow.txt
here's my code...
read line sub
Code:
Public Function ReadLine(fName As String, LineNumber As Long) As String
Dim oFSO As New FileSystemObject
Dim oFSTR As Scripting.TextStream
Dim Ret As Long
Dim lCtr As Long
If oFSO.FileExists(fName) Then
Set oFSTR = oFSO.OpenTextFile(fName)
Do While Not oFSTR.AtEndOfStream
lCtr = lCtr + 1
If lCtr = LineNumber Then
ReadLine = oFSTR.ReadLine
Exit Do
End If
oFSTR.SkipLine
Loop
oFSTR.Close
Set oFSTR = Nothing
End If
End Function
parse sub
Code:
Public Function Parse(Expression As String, DelimiterA As String, DelimiterB As String) As String
On Error GoTo done:
Dim a As Long, b As Long, txtbx As String
a = 1
While InStr(a, Expression, DelimiterA) > 0
a = InStr(a, Expression, DelimiterA) + Len(DelimiterA)
b = InStr(a, Expression, DelimiterB)
txtbx = Mid$(Expression, a, b - a)
Parse = txtbx
Wend
done:
End Function
random number sub
Code:
Function RndNumber(ByVal Lowest As Double, ByVal Highest As Double) As Double
Do: DoEvents
Randomize
RndNumber = CDbl((Rnd * Highest) + Lowest)
Loop Until RndNumber <= Highest
End Function
actual code to replace with random line
Code:
'\macros\%file.txt%
If InStr(Text, ".txt") Then
strFile = Parse(Text, "%", "%")
Text = Replace(Text, "%", "")
If FileExists(App.Path & "\macros\" & strFile) = True Then
Text = Replace(Text, strFile, ReadLine(App.Path & "\macros\" & strFile, RndNumber(1, FileCount(App.Path & "\macros\" & strFile) + 1)))
End If
End If
i know it's a lot, my sub works, but when i do 2 files like %wow.txt% and %ha.txt% its nulled. anyone have another way of doing this? thanks.