Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

DocProperties not available on startup

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Colin Chaplin - 07 Mar 2008 16:38 GMT
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
Helmut Weber - 08 Mar 2008 00:09 GMT
Hi Colin,

you may have to wait until the document is fully loaded:

http://tinyurl.com/35b7t5
http://tinyurl.com/3asjt3

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.