wdPropertyTimeLastSaved automation error

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

wdPropertyTimeLastSaved automation error

Post by Robie »

Hi

We are currently experiencing this error in US only but fine everywhere else. Can somebody please explain why I am getting this error and what is the fix please? I am at my wits end. Thanks

The code sLastSaved = ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved).Value returns an automation error.

Code: Select all

Function SaveThisDocument() As Boolean
'
'   SaveThisDocument Function
'   Return true if document has been saved since it was last opened
'
    Dim bFileSaved As Boolean
    Dim sLastSaved As String
    Dim strArray() As String
    
    bFileSaved = ActiveDocument.Saved
    SaveThisDocument = False
    sLastSaved = ActiveDocument.BuiltInDocumentProperties(wdPropertyTimeLastSaved).Value
    strArray = Split(sLastSaved, "/")
    If (Left$(strArray(2), 4) >= "2007") Then
        ' if document saved ALREADY (in current session) return true
        ' ODT value is set to current date+time when the doc is created or opened
        SaveThisDocument = (CDate(sLastSaved) > CDate(GetValueOfCDP(sDocOpenDT)))
    End If
    ActiveDocument.Saved = bFileSaved
End Function
automation error.png
BTW: While am here (without having to create another topic) how can lock my template/vba code so that no one can change it?
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: wdPropertyTimeLastSaved automation error

Post by HansV »

This error will occur when someone runs the code from a new document that hasn't been saved yet.

To lock VBA code:
- Activate the Visual Basic Editor.
- Select Tools | Project Properties...
- Activate the Protection tab.
- Tick the check box "Lock project for viewing".
- Choose a password and enter it in the Password and Confirm password boxes.
- Make sure that you remember the password - if you lose it, you won't be able to view or edit the VBA code any more.
- Click OK.
- Save the template.
Next time you open the template or a document based on it, you'll have to provide the password if you want to view or edit the code.

Remark: if you lock the code, users won't get the option to debug errors, so it's a good idea to have error handling in your code.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: wdPropertyTimeLastSaved automation error

Post by Robie »

HansV wrote:This error will occur when someone runs the code from a new document that hasn't been saved yet.

To lock VBA code:
- Activate the Visual Basic Editor.
- Select Tools | Project Properties...
- Activate the Protection tab.
- Tick the check box "Lock project for viewing".
- Choose a password and enter it in the Password and Confirm password boxes.
- Make sure that you remember the password - if you lose it, you won't be able to view or edit the VBA code any more.
- Click OK.
- Save the template.
Next time you open the template or a document based on it, you'll have to provide the password if you want to view or edit the code.

Remark: if you lock the code, users won't get the option to debug errors, so it's a good idea to have error handling in your code.
Hans, thanks for the locking of template bit (never locked before but people are chaning the template willy-nilly without our permission).

Regarding the automation error (in US only). This is happening on current document, i.e. already saved and edited several times previsouly.
If it does happen on a new document, how can I get around it? Presumeably, if I check for ActiveDocument.FullName to be "" and if it is, I take another decision. Otherwise, carry on as I am doing now.