Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

How to run find and replace command in a text file from VBA?

Hi Experts,
I am interesting in a way to open from my Access app a text file and replace all commas, then save the document.
0
bfuchs
Asked:
bfuchs
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
Rey Obrero (Capricorn1)Commented:
you can create  a new text file "myFile2.txt" without the commas

dim s as string
open "c:\folderName\myFile.txt" for input as #1
open "c:\folderName\myFile2.txt" for output as #2
do until eof(1)
line input #1, s
s=replace(s,",",":")           'change ":" with the replacement you want

print #2, s
loop
close #1
close #2
0
 
Nick67Commented:
Hi Ben,
I don't think you worded your question quite right.
You have a fixed width column text file that has some data that has commas in it.
This prevents you from importing the file directly as a csv file.

Rey has provided you with a solution that will replace all commas with colons and save the file with a new name.
Which is what you asked for in this Q

If you still remain interested in opening a fixed width column text file and processing the results line-by-line into fields and records, let me know.

Nick67
0
 
Jeffrey CoachmanCommented:
...yes, please clarify:
"Replace" all commas with ...?
...or "Delete" all commas...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bfuchsAuthor Commented:
@Ray,
Your solution works, just wonder if there is really a need to create a new document or this can be done in same document?

@Nick67,
For that I am happy with the solution provided in other thread, just wonder if what you had posted there could work for this as well?

@Jeff,
For what I am concerned now, it wouldn't matter, either way will work.

Thanks,
Ben
0
 
Nick67Commented:
just wonder if what you had posted there could work for this as well
Yes, the principle is pretty much universal.
You open the file, and can then proceed to read it line by line, and do whatever you would like with each line in VBA
Rey's solution here does the exact same things, but uses a different, less verbose, syntax than what I posted in your earlier question.

is really a need to create a new document
Both Rey's idea and mine require saving the read data to a new location.
But there is really very little involved with killing the source file and replacing it with the new file so in the end only the edited results exist
0
 
Rey Obrero (Capricorn1)Commented:
<Your solution works, just wonder if there is really a need to create a new document or this can be done in same document?>

if you want to retain the same document..

after running my codes
kill/delete the original document with

kill  "c:\folderName\myFile.txt"

'then name the new file with the name of the old file

name  "c:\folderName\myFile2.txt" as  "c:\folderName\myFile.txt"
0
 
bfuchsAuthor Commented:
Thanks Experts!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now