Office Word Programming >> Reading from all files in a folder

by christophercbrewster via OfficeKB.com » Tue, 10 Feb 2009 07:01:54 GMT

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



Office Word Programming >> Reading from all files in a folder

by christophercbrewster via OfficeKB.com » Tue, 10 Feb 2009 07:36:26 GMT


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?)



--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via http://www.officekb.com




Office Word Programming >> Reading from all files in a folder

by Doug Robbins - Word MVP » Tue, 10 Feb 2009 12:28:14 GMT

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







Reading from all files in a folder

by christophercbrewster via OfficeKB.com » Tue, 10 Feb 2009 23:23:14 GMT

I'm treating the files as simple text. They can be displayed in Notepad.



--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via http://www.officekb.com



Reading from all files in a folder

by Doug Robbins - Word MVP » Wed, 11 Feb 2009 10:54:52 GMT

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








Similar Threads

1. VBScript that read multiple CVS file in multiple folder & writes o

2. Folders/files read only/can't create new folder in read only f

Thanks for that information, Michael - what about the files within the 
folders that are marked read-only and won't let me change them? And some of 
the folders refuse to let me create new folders in them - including my 
Documents folder.

Gina

"Michael" wrote:

> I don't know what your problem is with bird names, but you should not bark 
> up the wrong tree with respect to the 'read only' attribute of folders.
> 
> Folders are not read only (or not read only) note that the square is filled 
> in, not empty or checked. The attribute read-only does not apply to a 
> folder.
> When you look at any folder it will have the read-only box filled in. The 
> contents (that is files) of a folder may of course be read only.
> 
> If you have a folder with the box checked (not just filled in) do get back 
> and I will have to eat crow (would really like to know its path and name).
> 
> Michael
> vista home premium
> 
> "gshillitani" < XXXX@XXXXX.COM > wrote in message 
> news: XXXX@XXXXX.COM ...
> >I forgot to say, I am using Vista.
> >
> > "gshillitani" wrote:
> >
> >> I am logged in as administrator, and there are some folders and files 
> >> that
> >> are marked read only all of a sudden. When I try to create new folders in
> >> some of these directories, nothing happens. Others are marked read only 
> >> and
> >> when I create new folders they are all bird names (pigeon, hawk, turkey, 
> >> hen)
> >> instead of being named "New Folder" as usual. I've searched for any virus
> >> information that lists those problems and have not been able to find
> >> anything. When I try to change the folders from read only, it acts like 
> >> it
> >> has done it but when I click OK and then look at the properties again, it 
> >> is
> >> still marked read only.  I hope this post makes sense. Its driving me 
> >> crazy! 
> 
> 

3. Folders/files read only/can't create new folder in read only folde

4. Public Folders - some files/folders need admin privileges

First of all an alarm: Can you believe Administrator account on my Vista 
machine had no password 8={. I can't remember being asked for one.

Now to my question: I moved some files from my folders and I wanted to take 
write privilege of these but in error denied read and execute. Now, I can 
access them with Admin privileges only. I have reset privileges back to what 
they were but no luck.

Any thoughts!! I am puzzled. I can't move them or cut and paste them even 
under Admin account.


5. Problem deleting vista RC windows folder and program files folder

6. File problems after moving folder from desktop to folder inside the C drive

Hi,
For some reason, when I moved a folder from the desktop to a folder in the C
drive I lost access to the folder. This is in Windows XP Professional.

I subsequently disabled simple file
sharing and gave full control to my login user which allowed me access to
the folder and all of its contents again. The problem is that the folder is
full of Word files of various sizes. If I try to open a file I get an error
message saying the path is invalid.

When I do a search for one of the files, I can see it only in the proper
folder and no where else on the drive and the search dialog box lists its
size as 64KB or some similar size. I am sure the path is the same as the
files I am having trouble accessing. If I double-click on the file in the
search dialog box I get the same error message indicting the path is
invalid.

Can anyone tell me what has happened and how to access the information they
should contain?

Thanks



7. Please help to add file name path and double line spacing to mutliple folders & sub folders - Word Document management

8. Saving to a web folder:- Won't save back to folder file opened fro

Does anyone have any idea why in word 2000 the 'save as' that comes up when 
saving to a web folder (from a file opened through a link) will feed back to 
the correct folder within the web folder.

Yet any version of Word afterwards will try and save to my documents instead 
and not bother to find the folder and file within the webfolder?