0

Problem: I am attempting to replace a key-value pair within a text file. The Key is staying the same while the value is changing. I don't want to modify any other lines except the single line that starts with the keyname.

What I have currently:

Sub REPLACESESSIONID()

Dim CONTENT, SESSIONID, WORKINGDIR As String

SESSIONID = "urn:uuid:A750ADE2355E6EAA2315397874423"

SESSION = FreeFile()

' ESTABLISH WORKING DIRECTORY
WORKINGDIR = Environ("AppData") & "\PROJECT"

' READ SESSION.TXT AND STORE AS VARIABLE
Open WORKINGDIR & "\SESSION.txt" For Input As #SESSION   ' Open file
    CONTENT = Input(LOF(SESSION), SESSION)
Close #SESSION

' REPLACE SESSIONID* WITH NEW SESSIONID
CONTENT = Replace(CONTENT, "SESSIONID*", "SESSIONID;" & SESSIONID)

' WRITE CHANGES TO SESSION.TXT
Open WORKINGDIR & "\SESSION.txt" For Output As #SESSION
    Print #SESSION, CONTENT
Close #SESSION

End Sub

I am aware that Replace does not allow wildcards, but am unsure how proceed.

Contents of SESSION.txt:

SESSIONID;urn:uuid:A7AGBDE25436EAA231532239423
ROLENAME;Local_Unix_Admin
braX
  • 11,506
  • 5
  • 20
  • 33
27560
  • 93
  • 7
  • 1
    `Dim CONTENT, SESSIONID, WORKINGDIR As String` is equal to `Dim CONTENT as Variant, Dim SessionID as Variant, Dim WorkingDir As String.` To `Dim` them all as strings you have to write it out each time – Jchang43 Oct 19 '18 at 16:27
  • [this](https://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops#22542835) may be of use – cybernetic.nomad Oct 19 '18 at 16:49
  • Can you help me out with the syntax? Here is what I have: `CONTENT = regEx.Replace(CONTENT, strReplace)` It appears I am missing something – 27560 Oct 19 '18 at 17:58

0 Answers0