VBA, VB, task?

E

EdC

Hi,

I need to run through a large number of Word files, changing the Titl
field in the Summary tab of File Properties to make it the same as th
filename.

I've read about the ActiveX DLL, dsofile.dll, on the Word MVP site
which gives you access to the file's properties without actuall
opening each file.

Can you do what I want to do just by using VBA and the dsofile, or d
you need to master other things?

Many Thanks, E
 
P

Peter Hewett

Hi Edc

Insert the following code in a VBA standard module in a new template. Then
just run the Worker macro. It will then find all Word documents in the
specified folder and all sub-folders. It sets the Document Property
"Title" of each doument to its filename. You need to modify the path info
to whatever it is you want to use. Also you need to create a project
reference for the new template to the "Microsoft Scripting Runtime"
library. You do this from within the VBA IDE. Just click on the project
name and then using the menu: Tools>References, now scroll down the list
until you find the above entry. When you find it make sure you check it's
checkbox and then click on OK and save your template. The code is now ready
to go.


' This is the base folder the document property extraction will start at
Const mcBasePath As String = "F:\My Templates\"

' Only files of this type are processed (must be lower case)
Const mcFileTypeMask As String = ".doc"

Private Sub Worker()
Dim fsoTemp As Scripting.FileSystemObject
Dim filStartingFolder As Scripting.Folder

' This speeds things up
Application.ScreenUpdating = False

' You must do this to stop AutoOpen macros
' from firing when the documents are opened
WordBasic.DisableAutoMacros True

' Iterate from base folder processing each Word document
Set fsoTemp = New Scripting.FileSystemObject
Set filStartingFolder = fsoTemp.GetFolder(mcBasePath)
ProcessFilesInFolder filStartingFolder

' Tidy up...
Application.ScreenUpdating = True
WordBasic.DisableAutoMacros False
End Sub ' Worker

Private Sub ProcessFilesInFolder(ByVal folCurrentFolder _
As Scripting.Folder)
Dim folSubFolder As Scripting.Folder
Dim filToProcess As Scripting.File

' Process files in the current folder
For Each filToProcess In folCurrentFolder.Files

' Only process files of the appropriate file type
If LCase$(Right$(filToProcess.Name, _
Len(mcFileTypeMask))) = mcFileTypeMask Then
ProcessFile filToProcess
End If
Next filToProcess

' Now process files in any subfolders of the current folder
For Each folSubFolder In folCurrentFolder.SubFolders
ProcessFilesInFolder folSubFolder
Next
End Sub ' ProcessFilesInFolder

Private Sub ProcessFile(ByVal filWordDoc As Scripting.File)
Dim docCurrent As Word.Document
Dim offDocProperty As Office.DocumentProperty

On Error GoTo ProcessFileError

Debug.Print filWordDoc.Path

' Log the name of the document we're modifying to the current document
ActiveDocument.Content.InsertAfter filWordDoc.Path & vbCr
Set docCurrent = Documents.Open(filWordDoc.Path)

' Set the documents Title to the file name (but not the path)
docCurrent.BuiltInDocumentProperties(wdPropertyTitle).Value = _
docCurrent.Name

' We're done with the document
docCurrent.Close wdDoNotSaveChanges

ProcessFileExit:
Exit Sub

ProcessFileError:
' Report unable to process file error here
Debug.Print "Unable to process document, error: " & Err.Number & ", " &
Err.Description
Resume ProcessFileExit
End Sub ' ProcessFile


HTH + Cheers - Peter
 
R

Randy Birch

Hi Peter ...

Two things ...

a) what additional line is required to set the author? Is it as simple as

docCurrent.BuiltInDocumentProperties(wdPropertyAuthor).Value = "birchr"

b) does performing a change such as you indicated affect the date modified
as reflected the document properties or in Explorer?

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
 
P

Peter Hewett

Hi Randy

1. Exactly right
2. Unfortunately, yes because you're actually opening and modifying the
document. The FSO will not let you modify the date modified either :(

Cheers - Peter
 
E

EdC

Peter,

Very many thanks for your help with this. I'll give it a go. Hop
others can also benefit.

Cheers, E
 
R

Randy Birch

1) What a guess ! :)
2) I figured that would be the case. :-(

Thanks.

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


: Hi Randy
:
: 1. Exactly right
: 2. Unfortunately, yes because you're actually opening and modifying the
: document. The FSO will not let you modify the date modified either :(
:
: Cheers - Peter
:
:
 

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