Reading from all files in a folder

  • Thread starter christophercbrewster via OfficeKB.com
  • Start date
C

christophercbrewster via OfficeKB.com

I need to loop through the files in a folder and reading the content of each
into a variable. It needs to be on the TXT/ASCII level rather than approached
through Word, because there are some problem characters that aren't displayed
correctly in Word. (I'm trying to get rid of them.) I think this would use
the FileSystem object but I'm stumped about how to code it. I think it would
start:

dirPath=ActiveDocument.path
Set fs = CreateObject("Scripting.FileSystemObject")
for each txtFile in fs. [something]

Help appreciated.

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200902/1
 
C

christophercbrewster via OfficeKB.com

I'd like to add to my question. Within the loop, I would do

sVariable=txtFile.Readall
sVariable=replace(sVariable, 'oldstring', 'newstring')

and then do something with the new value, maybe write it to the file. (If I
do that, is there a way to first delete the old text from the file?)
I need to loop through the files in a folder and reading the content of each
into a variable. It needs to be on the TXT/ASCII level rather than approached
through Word, because there are some problem characters that aren't displayed
correctly in Word. (I'm trying to get rid of them.) I think this would use
the FileSystem object but I'm stumped about how to code it. I think it would
start:

dirPath=ActiveDocument.path
Set fs = CreateObject("Scripting.FileSystemObject")
for each txtFile in fs. [something]

Help appreciated.
 
D

Doug Robbins - Word MVP

What type files are they?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Word should be able to handle any ASCII character, so the following should
probably do what you want:

Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
' Get the folder containing the files
With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With
'Close any documents that may be open
If Documents.Count > 0 Then
Documents.Close Savechanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(PathToUse & "*.txt")
While myFile <> ""
'Open each file and make the replacement
Set myDoc = Documents.Open(PathToUse & myFile)
'Modify the text in the file
myDoc.Range.Text = Replace(myDoc.Range.Text, "oldstring", "newstring")
'Close the file, saving the changes.
myDoc.Close Savechanges:=wdSaveChanges
myFile = Dir$()
Wend

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

christophercbrewster via OfficeKB.com said:
I'm treating the files as simple text. They can be displayed in Notepad.
What type files are they?
I need to loop through the files in a folder and reading the content of
each
[quoted text clipped - 12 lines]
Help appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top