DocProperties not available on startup

C

Colin Chaplin

Hello

I've cobbled together a PoC app that writes out a certain document property.

If I manually run the macro, this works. When it runs on startup, the
docproperty output is blank (the code does run)
It would appear the code is running before the document is fully available.
Here's a sample of the output text file

docprop1 is :
#1899-12-30 15:43:30#
docprop1 is : TESTDOCPROP
#1899-12-30 15:44:04#
docprop1 is :
#1899-12-30 16:32:48#

You can see when it was run by a document starting, or being manually run by
me (the line with docprop1 is : TESTDOCPROP)

If I'm write, this is a major problem for me. Hope I'm not!?

Any ideas, chaps?



Option Explicit

Dim oAppClass As New ThisApplication

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
Dim PropVal As String
Dim myfile As String
Dim fnum


PropVal = ReadProp("docprop1")

myfile = "c:\" & "whateveryouwant.txt"
fnum = FreeFile()
Open myfile For Append As fnum

Print #fnum, "Docprop 1 is :", PropVal
Write #fnum, Time
Close #fnum




End Sub
Function ReadProp(sPropName As String) As Variant

Dim bCustom As Boolean
Dim sValue As String

On Error GoTo ErrHandlerReadProp
'Try the built-in properties first
'An error will occur if the property doesn't exist
sValue = ActiveDocument.BuiltInDocumentProperties(sPropName).Value
ReadProp = sValue
Exit Function

ContinueCustom:
bCustom = True

Custom:
sValue = ActiveDocument.CustomDocumentProperties(sPropName).Value
ReadProp = sValue
Exit Function

ErrHandlerReadProp:
Err.Clear
'The boolean bCustom has the value False, if this is the first
'time that the errorhandler is runned
If Not bCustom Then
'Continue to see if the property is a custom documentproperty
Resume ContinueCustom
Else
'The property wasn't found, return an empty string
ReadProp = ""
Exit Function
End If

End Function

Sub CloseAll()
'Close all open files and shutdown Word

With Application
.ScreenUpdating = False

'Loop Through open documents
Do Until .Documents.Count = 0
'Close no save
.Documents(1).Close SaveChanges:=wdDoNotSaveChanges
Loop

'Quit Word no save
.Quit SaveChanges:=wdDoNotSaveChanges
End With
End Sub
 

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