Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning current date to all word document.

Status
Not open for further replies.

Anthony904

IS-IT--Management
Jul 15, 2004
153
US
I am not sure If I am asking in the correct Forum but.. I have a problem..

I have tons of word 97 documents that need a current date on the doc. so that when the user prints, the current date is also printed on the doc.

Is there a way to assign a current date on all the documents w/o having to manually go into each doc and adding a date?

Thanks!
 
open all the of the docs programmically?
recursive folder/files search with FSO?
WMI SELECT statement on .doc file?
Now() statement when you open and put in the footer?
 
Hey Anthony.

Word isn't really my thing, but I adapted this from a similar Excel thing I had to do a while back:
Code:
Sub AddDateToFooters()
Dim fs
Set fs = Application.FileSearch
Dim MyPath
MyPath = InputBox("Please type the path of the folder that contains" & _
    " the Word Documents to which you want to add Dates", "Enter Path")

With fs
    .LookIn = MyPath
    .FileName = "*.doc"
    .Execute
    If .FoundFiles.Count = 0 Then GoTo NoneFound
        For i = 1 To .FoundFiles.Count
            Documents.Open .FoundFiles(i)
                
                ActiveWindow.ActivePane.View.SeekView = _
                    wdSeekCurrentPageFooter
                Selection.Fields.Add Range:=Selection.Range, _
                    Type:=wdFieldDate
                ActiveWindow.ActivePane.View.SeekView = _
                    wdSeekMainDocument
            
            ActiveDocument.Save
            ActiveDocument.Close
        Next i
End With
Set fs = Nothing

Exit Sub
NoneFound:
MsgBox "No Word Documents were found in this folder"
End Sub

[tt]_____
[blue]-John[/blue][/tt]

Help us help you. Please read FAQ181-2886 before posting.
 
mrmovie,

I'm not familar to what you just said.. could you explain futher? thanks

John,

I tried running your code from excel and I am getting..

Run-Time Error '5'
Invalid procedure call or argument

I created a folder on my C:drive .. called in Temp and placed a blank word document in there to test..

I placed your code in an excel sheet.. created a button and related your code to that.. I clicked my button and received an input box..

asking me to enter in the path of the folder or add dates...

So I punch in .. C:\Temp

this it gives me the error stated above.. ( excel 97 )

I also did the same with excel 2003.. and get a different error..

Object Required

Did I do something wrong..?

Thanks for your responses..
 
Try running it directly from Word.

[ul][li]Open Word[/li]
[li]Press [Alt]+[F11] (To open the VBEditor)[/li]
[li]Right Click on Normal (or, if you don't have Normal listed, then right click on Project(Document1))[/li]
[li]Select Insert[/li]
[li]Select Module[/li]
[li]Paste my code into the module (the window on the right)[/li][/ul]

Try to run it again. You are doing the right thing with the inputbox by putting in C:\Temp. That should work.

[tt]_____
[blue]-John[/blue][/tt]

Help us help you. Please read FAQ181-2886 before posting.
 
John,

I am getting the same error as stated above..

It is pointing to this line..

.FileName = "*.doc"

Thanks!
 
John,

Ok, I take it back .. I ran this in Word 03 and it seems to be working now..

Thanks!
 
It worked fine for me in Word 2000.

In any case, I'm glad you got it sorted out!
[cheers]

[tt]_____
[blue]-John[/blue][/tt]

Help us help you. Please read FAQ181-2886 before posting.
 
John,

I tried running this your code with the documents I have with macros in them.. I'm getting an error..

run-time error '4605'
This command is not avaiable.

Could you advise?

Thanks!
 
What kind of macros are in those files? Are they set to auto-run when the document is open?

[tt]_____
[blue]-John[/blue][/tt]

Help us help you. Please read FAQ181-2886 before posting.
 
Yes.. I have an auto-run on open that pulls up a UserForm..
 
In Excel, I would place
Application.enableEvents = False
before opening a workbook, but Word doesn't seem to recognize the command.

I'll poke around to see if I can figure out how to supress the macros in Word. In the mean time, does anyone else following this thread have any ideas?

[tt]_____
[blue]-John[/blue][/tt]

Help us help you. Please read FAQ181-2886 before posting.
 
John,

I also cannot write to protected documents. Is there a way to unprotect .. write the date... then reprotect..?

There are some protected and some unprotected documents..

Thanks for your help!
 
Have a look at the Unprotect and Protect methods of the Document object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I just had a thought that might get around the Document_Open macros:

Go to Tools > Macro > Security > High

While this is set to high, no macros should even try to launch. You'll want to switch this back after making the changes.

As for the protected documents, what PHV said.

[tt]_____
[blue]-John[/blue][/tt]

Help us help you. Please read FAQ181-2886 before posting.
 
Hi Anthony904,

John's code goves you the 'DATE' field, which is what you asked for, with the default formatting. If that formatting doesn't suit your needs, you can change it by adding the appropriate picture switches via code.

Another field that might be useful in this context is Word's 'PRINTDATE' field. The syntax is the same as for the 'DATE' field, but has the possible advantage for you of showing when the document was last printed, and updates each time the document is printed. The DATE field simply shows the current date.

Cheers
 
Thanks for the suggestion Macropod, but I think the current date will work for now. I will however keep your suggestion in mind.

I've update John's code and added a turn off protection and turn on Protection.. This seems to work but this now protects all documents. Even documents that weren't protected to begin with.. I like to leave the document as they were.. (protected/unprotected)

One other thing.. Everytime I run the code it adds the date field fine but does not close the document after it writes the date.

I orginally commented line (below) out.. So I could test the date.

Code:
ActiveDocument.Close

The error that I am getting is..

Run-time error '4198'
Command Failed

and it points to the line above.

I am posting the updated code.. maybe I have misplaced something..

Code:
Sub AddDateToFooters()
Dim fs
Set fs = Application.FileSearch
Dim MyPath
MyPath = InputBox("Please type the path of the folder that contains" & _
    " the Word Documents to which you want to add Dates", "Enter Path")

With fs
    .LookIn = MyPath
    .FileName = "*.doc"
    .Execute
    If .FoundFiles.Count = 0 Then GoTo NoneFound
        For i = 1 To .FoundFiles.Count
            Documents.Open .FoundFiles(i)
            
        If ActiveDocument.ProtectionType <> -1 Then
            ActiveDocument.Unprotect
        End If
                ActiveWindow.ActivePane.View.SeekView = _
                    wdSeekCurrentPageFooter
                Selection.Fields.Add Range:=Selection.Range, _
                    Type:=wdFieldDate
                ActiveWindow.ActivePane.View.SeekView = _
                    wdSeekMainDocument
    
    If ActiveDocument.ProtectionType <> 1 Then
        ActiveDocument.Protect _
        NoReset:=False, Type:=wdAllowOnlyFormFields
    End If
    
            ActiveDocument.Save
            ActiveDocument.Close
           
        Next i
        
End With
Set fs = Nothing

Exit Sub
NoneFound:
MsgBox "No Word Documents were found in this folder"
End Sub

Thanks for your help!
 
Is there any way to do the same thing in header of a .log file and diplaying page numbers in footer of the .log file through word macro.
 
Hi Anthony904,

To only unprtect/reprotect documents that were already protected, you need to test & store the protection state when opening, so that you can reset the protection state afterwards only if the document had already been protected. So the code:
Code:
If ActiveDocument.ProtectionType <> -1 Then
     ActiveDocument.Unprotect
End If
would become something like:
Code:
PState = ActiveDocument.ProtectionType 
If PState <> -1 Then
     ActiveDocument.Unprotect
End If
and the code:
Code:
If ActiveDocument.ProtectionType <> 1 Then
     ActiveDocument.Protect _
     NoReset:=False, Type:=wdAllowOnlyFormFields
End If
would become:
Code:
If PState <> -1 Then
     ActiveDocument.Protect _
     NoReset:=False, Type:=wdAllowOnlyFormFields
End If

Cheers
PS: For your folder selection, try adding this function:
Code:
Function GetFolder(Optional Title As String, Optional RootFolder As Variant) As String
On Error Resume Next
GetFolder = CreateObject("Shell.Application").BrowseForFolder(0, Title, 0, RootFolder).Items.Item.Path
End Function
and calling it from your existing sub with:
Code:
GetFolder(Title:="Please select the folder containing the Word" & vbCrLf & _
    "Documents to which you want to add Dates", RootFolder:=&H11)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top