PDA

View Full Version : loop through and rename wordfiles



litrainer
10-27-2010, 03:57 AM
I have 10,000 word files named file0 through file10000, Some are corrupt, some not, no way to tell.

I'm looking for a solution that will:
1. open a file
2. if unopenable it will skip to the next file
3. if opend, the file will be saved using docment property's, i'm hoping for last person who opened and last date modified
4.close the file and loop to the next

i can get the file names into an excel file and can do this with excel, but i'm cluless about word macros

Tinbendr
10-27-2010, 07:47 AM
Welcome to VBA Express!

If you've posted this same question on other forums, please provide a link to those messages.

In Knowledge base there is this article (http://www.vbaexpress.com/kb/getarticle.php?kb_id=76) about processing all files.

Then add this sub.

'this is where a macro would be that would actually do something
Sub DoWork(wdDoc As Document)
Dim A$, B$
Dim MyPath as String

MyPath = "C:\My Path\" ' Change to suit.
A$ = wdDoc.BuiltInDocumentProperties(wdPropertyLastAuthor)
B$ = wdDoc.BuiltInDocumentProperties(wdPropertyTimeLastSaved)
wdDoc.SaveAs MyPath & A$ & "-" & B$
End Sub


In the OpenAllFiles sub, you'll need to add error checking.

On Error Goto Skip
Set wdDoc = Documents.Open(FileName:=strPath & "\" & strName, _
ReadOnly:=False, Format:=wdOpenFormatAuto)

'Call the macro that performs work on the file pasing a reference to it
DoWork wdDoc

'we close saving changes
wdDoc.Close wdDoNotSaveChanges

Skip:
On Error Goto 0 'Turn Off Error Checking

macropod
10-27-2010, 02:12 PM
Re:
if opend, the file will be saved using docment property's, i'm hoping for last person who opened and last date modifiedWhy not simply rename the file (copy if necessary)? That way, you avoid messing around with any of the file's other attributes.

fumei
10-29-2010, 10:22 AM
Because they want to use the document properties? Because they need to do an Open as a check to see if it is corrupted, or not?

I am guessing.

macropod
10-29-2010, 04:21 PM
Hi Gerry,

Opening a file to test for corruption doesn't mean you have to re-save it. Simply closing then renaming would do the job. If there are document properties that are needed and can only be accessed by opening the file, those can be harvested while the file is open.

Unless there is a good reason for saving, using File|Save As (this process being implied by the "saved using docment property's"), it's far better to simply rename the file (or a copy of it). Otherwise, the save process alters some key properties, including the document's creation and last-saved dates.

fumei
10-29-2010, 04:27 PM
If it tests for corruption it is (I think) ignored. If it is OK, and opened, they want to use a document property.

Yes, I agree though. It could be opened, the doc property gathered (or harvested as you put it), closed (unsaved), and then renamed using the data mined during the open.

There should be a difference in speed going that route.