Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Loop txt files and make change and save

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Excel 009 - 18 Oct 2006 14:41 GMT
Dear all,

I am trying to modify the same specific content in 100+ txt files (for
example, change xxx to yyy).  I need help to write a Word VBA code to
do that.  Here is the pesudo code:

loop all files in a folder

open Nth txt file
     find xxx
     replace with yyy
     save the txt file with a new name (add suffix "-mod")
     close the file
end loop

Can anyone help?  Thank you in advance.

- 009
http://en.wikipedia.org/wiki/Cyborg_009
Graham Mayor - 18 Oct 2006 15:31 GMT
See the code at http://www.gmayor.com/batch_replace.htm

Add the line
Dim intPos As Integer
at the start of
Public Sub BatchReplaceAnywhere()

Replace in that sub

'Close the file, saving the changes.
myDoc.Close Savechanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub

with

intPos = InStrRev(myFile, ".")
myFile = Left(myFile, intPos - 1)
myFile = myFile & "_MOD.txt"
MyDoc.SaveAs FileName:=myFile, _
FileFormat:=wdFormatDOSText
MyDoc.Close SaveChanges:=wdDoNotSaveChanges
   myFile = Dir$()
Wend
End Sub

This should leave your original files unchanged but save new text versions
with the filename
filename_MOD.txt in the same folder. Test on sample data.
Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Dear all,
>
[quoted text clipped - 15 lines]
> - 009
> http://en.wikipedia.org/wiki/Cyborg_009
Excel 009 - 18 Oct 2006 20:17 GMT
Thank you Graham.

You code works after I did the following changes:

Comment out:

   'Close any documents that may be open
'    If Documents.Count > 0 Then
'    Documents.Close SaveChanges:=wdPromptToSaveChanges
'    End If

Because it closes my Word document that contains the macro.

Change
   myFile = Dir$(PathToUse & "*.doc")
to
   myFile = Dir$(PathToUse & "*.txt")

because my files are txt files.

I code ran, but where are the output files?  Where are they located?

- 009
Russ - 19 Oct 2006 04:49 GMT
Excel 009,

> Thank you Graham.
>
[quoted text clipped - 17 lines]
>
> I code ran, but where are the output files?  Where are they located?
Did you check your original files to see if they where changed after
'running code'?

> - 009

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 19 Oct 2006 04:57 GMT
Excel 009,
> Excel 009,
>
[quoted text clipped - 21 lines]
> Did you check your original files to see if they *where typo* changed after
> 'running code'?
I meant, did original files change, like you wanted them to change?

>> - 009

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Graham Mayor - 19 Oct 2006 07:23 GMT
Put the macro in normal.dot - not the document you are working with and
restore the commented lines.

Your other changes seem OK - but for some reason I am not yet clear about,
the modified documents are saved in the default document folder (tools >
options > file locations > documents).

You'll also need to ensure that tools > options > general > confirm
conversions at open is unchecked or you will be prompted for a text filter
for each document in the folder.

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Thank you Graham.
>
[quoted text clipped - 19 lines]
>
> - 009
Excel 009 - 19 Oct 2006 14:13 GMT
Hi Graham and Russ,

Yes, the output files got populated beautifully.  Thanks to Graham's
code.
It was my mistake that despite I listed the changes that I did, but the
changes were not saved and the code was ran unchanged.

I have another question.  How can I have the have the whole word
changed and not on partial work.  Ex: in

x =  10
text = "ABC"
z = x + 30,

if I want to just change the "x" variable to "y" without making change
on "text", how can I do so?

- 009
Graham Mayor - 19 Oct 2006 15:47 GMT
You will have to include sufficient of the string to uniquely identify it,
eg search for
z = x + 30
replace with
z = y + 30

If necessary change the line
.MatchWildcards = False
to
.MatchWildcards = True
and use a suitable wildcard pattern
http://www.gmayor.com/replace_using_wildcards.htm

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hi Graham and Russ,
>
[quoted text clipped - 14 lines]
>
> - 009
Excel 009 - 19 Oct 2006 16:02 GMT
Ok, Thanks.  I think including the space after will help.

-009
Tony Jollans - 19 Oct 2006 17:58 GMT
The easiest wildcard pattern for your example would be <x> which means
start-of-word lower-case-letter-x end-of-word

--
Enjoy,
Tony

> You will have to include sufficient of the string to uniquely identify it,
> eg search for
[quoted text clipped - 35 lines]
> >
> > - 009

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.