-1

Could somebody help me with the following? I tried making it on my own, but all I could do is open a txt and replace a static word with static word.

VBA script:

Open and Read first line of ThisVbaPath.WordsToUse.txt

Open and Find USER_INPUT in ThisVbaPath.BaseDoc.docx (or txt)

Replace all occurrence of USER_INPUT with first line from WordsToUse.txt

Save BaseDoc.docx as BaseDoc&First_Line.docx

Close all

Go next line and do same, but don't ask for user input, use previous one

If error go next

When done show if there were any errors (unlikely I guess)

I would use it about weekly for 150-ish lines.

Thanks!

Dkoonice
  • 1
  • 2

2 Answers2

0

I think something like this should work?

Sub test()

  Dim text, textReplace, findMe As String

  findMe = InputBox("What Text To Find?")
  Open "C:\Excel Scripts\test.txt" For Input As #1
  Open "C:\Excel Scripts\test2.txt" For Input As #2
  Open "C:\Excel Scripts\output.txt" For Output As #3

  While Not EOF(1)
    Line Input #1, text
      While Not EOF(2)
        Line Input #2, textReplace
        Write #3, Replace(text, findMe, textReplace)
      Wend
  Wend

  Close #1
  Close #2
  Close #3


End Sub
NuWin
  • 276
  • 5
  • 15
-1
Sub TextFile_FindReplace()
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String

FilePath = Application.ActiveWorkbook.Path & "\NEWTEST.txt"
TextFile = FreeFile
Open FilePath For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)


FileContent = Replace(FileContent, "FOO", "BAR")


Print #TextFile, FileContent
Close TextFile
End Sub
Techie
  • 181
  • 4
  • 14