PDA

View Full Version : MS Word VBA code won't work



DrChrisB
01-14-2011, 06:12 PM
I am having a problem using VBA in MS Word. For some reason my simple code works when I open a file but does not work if the file is a hyperlink on our company intranet.

I have a form called Request.doc with a field that it todays date using the format yymmddhhmm. Every time the file opens the number changes. I want the number to lock when a user closes the file at the same time he/she renames the file.

My code is below. Again the code executes if I open Request.doc, but it does not work if Request.doc is a hyperlink on our company intranet.


Sub Document_Close()
If ActiveDocument.Name <> Request.doc" Then
ActiveDocument.Unprotect Password:="Siltronic"
ActiveDocument.Fields.Locked = True
ActiveDocument.Protect Password:="Siltronic", NoReset:=False, Type:= _
wdAllowOnlyRevisions
ActiveDocument.Fields.Unlink
End If
End Sub

Can someone please explain why and offer a solution?

mdmackillop
01-27-2011, 02:21 PM
Missing quote?
If ActiveDocument.Name <> Request.doc" Then

geekgirlau
01-27-2011, 08:44 PM
This sounds like it should be setup as a template, which would minimise the risk of a user saving over the master file.

macropod
01-28-2011, 02:41 AM
I would have thought:
Sub Document_Close()
If ActiveDocument.Name <> "Request.doc" Then ActiveDocument.Fields.Unlink
End Subwould be sufficient.

macropod
01-28-2011, 10:16 PM
Hmm, 'If ActiveDocument.Name <> "Request.doc" Then ActiveDocument.Fields.Unlink' seem to work before (and I couldn't understand why), but now it doesn't. This does:
Sub Document_Close()
Const Pwd As String = "Siltronic"
With ActiveDocument
If .Name <> "Request.doc" Then
.Unprotect Password:=Pwd
.Fields.Unlink
.Protect Password:=Pwd, Type:=wdAllowOnlyRevisions
End If
End With
End SubWith this approach, there is no need to lock any fields or to worry about resets when protection is re-applied.