#1
|
|||
|
|||
Help with Document Properties Prompt Macro
I have butchered an existing macro (as I don't have the knowledge to write one from scratch) to attempt to prompt Word (2007/2010) users in my workplace to add document properties at the point of closing their document (I have included the macro below). It works fine other than I don't know how to achieve two other specific aims:
1. How can I prompt for the property called 'Status'? I want users to be prompted so that it will save the document with Draft/Final status etc. 2. The macro at the moment has specific text pre-included in the form (e.g. add author name here), but how do i set the default text to be that already existing for that property, so the users don't have to start from scratch when adding this information each time they close the document? Any help much apprecaited. Macro: Sub AutoClose() Dim Title As String Dim Author As String Dim Subject As String Dim oStory As Range Dim oShp As Shape Title = InputBox("Enter the Document Title:", "Title", "Title Here") Author = InputBox("Enter the Document Author:", "Author", "Author Here") Subject = InputBox("Enter the Client and Site Name:", "Subject", "Client - Site Name Here") ActiveDocument.BuiltInDocumentProperties("Title"). Value = Title ActiveDocument.BuiltInDocumentProperties("Subject" ).Value = Subject ActiveDocument.BuiltInDocumentProperties("Author") .Value = Author For Each oStory In ActiveDocument.StoryRanges Do On Error Resume Next oStory.Fields.Update Select Case oStory.StoryType Case 6, 7, 8, 9, 10, 11 If oStory.ShapeRange.Count > 0 Then For Each oShp In oStory.ShapeRange If oShp.TextFrame.HasText Then oShp.TextFrame.TextRange.Fields.Update End If Next oShp Set oShp = Nothing End If Case Else 'Do Nothing End Select On Error GoTo 0 'Get next linked story (if any) Set oStory = oStory.NextStoryRange Loop Until oStory Is Nothing Next oStory Set oStory = Nothing End Sub |
#2
|
||||
|
||||
Hi KJJ,
Much of your code isn't related to the task you've described, so I've omitted it from the code below. Also, instead of using an 'Document_Close' macro, I've used a 'AutoClose' macro, which you place in the template's 'ThisDocument' module. The code below provides the prompts you're after, and only updates the properties if anything's changed. That way, the document doesn't give unnecessary 'save' prompts (though if you do include your other code for updating fields, you're going to get 'save' prompts anyway). Code:
Private Sub Document_Close() Dim StrOldTitle As String, StrNewTitle As String Dim StrOldAuthor As String, StrNewAuthor As String Dim StrOldSubject As String, StrNewSubject As String Dim bSaved As Boolean With ActiveDocument bSaved = .Saved StrOldTitle = .BuiltInDocumentProperties("Title").Value StrOldAuthor = .BuiltInDocumentProperties("Author").Value StrOldSubject = .BuiltInDocumentProperties("Subject").Value StrNewTitle = InputBox("Enter the Document Title:", "Title", StrOldTitle) StrNewAuthor = InputBox("Enter the Document Author:", "Author", StrOldAuthor) StrNewSubject = InputBox("Enter the Client and Site Name:", "Subject", StrOldSubject) If StrOldTitle <> StrNewTitle Then _ .BuiltInDocumentProperties("Title").Value = StrNewTitle If StrOldAuthor <> StrOldAuthor Then _ .BuiltInDocumentProperties("Author").Value = StrNewAuthor If StrOldSubject <> StrOldSubject Then _ .BuiltInDocumentProperties("Subject").Value = StrNewSubject If bSaved = True And .Saved = True Then Exit Sub End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
That is extremely helpful. Many thanks for that. Much appreciated.
Do you know the answer to my first query: How can I prompt for the property called 'Status'? Thanks again. Karl |
#4
|
||||
|
||||
Hi Karl,
For 'Status', you'd simply add another set of variables and the corresponding code to get/set its values. For example: Code:
Dim StrOldStatus As String, StrNewStatus As String ... StrOldStatus = .BuiltInDocumentProperties("Status").Value ... StrNewStatus = InputBox("Enter the Document Status:", "Status", StrOldStatus) ... If StrOldStatus<> StrNewStatus Then _ .BuiltInDocumentProperties("Status").Value = StrNewStatus
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thanks for the quick reply. I tried that and unfortunately it doesn't seem to work. I get an error on this line:
StrOldStatus = .BuiltInDocumentProperties("Status").Value It maybe suggests that 'status' is not a 'Built In Document Property' or that 'status' is not the correct description? Thnaks Karl |
#6
|
||||
|
||||
Hi Karl,
OK, on-screen it's called 'Status', but in code it's called 'Content Status'. So you'll need to change the two references from: .BuiltInDocumentProperties("Status").Value to: .BuiltInDocumentProperties("Content Status").Value
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thanks Paul,
That sorted out the status issue nicely. I think there were a couple of issues with the last few lines of your code, so i have amended this as below. Does this look right to you? Seems to work ok. Many thanks again. Karl Code:
Private Sub Document_Close() Dim StrOldTitle As String, StrNewTitle As String Dim StrOldAuthor As String, StrNewAuthor As String Dim StrOldSubject As String, StrNewSubject As String Dim StrOldStatus As String, StrNewStatus As String Dim bSaved As Boolean With ActiveDocument bSaved = .Saved StrOldTitle = .BuiltInDocumentProperties("Title").Value StrOldAuthor = .BuiltInDocumentProperties("Author").Value StrOldSubject = .BuiltInDocumentProperties("Subject").Value StrOldStatus = .BuiltInDocumentProperties("Content Status").Value StrNewTitle = InputBox("Enter the Document Title:", "Title", StrOldTitle) StrNewAuthor = InputBox("Enter the Document Author:", "Author", StrOldAuthor) StrNewSubject = InputBox("Enter the Client and Site Name:", "Subject", StrOldSubject) If StrOldTitle <> StrNewTitle Then _ .BuiltInDocumentProperties("Title").Value = StrNewTitle If StrOldAuthor <> StrNewAuthor Then _ .BuiltInDocumentProperties("Author").Value = StrNewAuthor If StrOldStatus <> StrNewStatus Then _ .BuiltInDocumentProperties("Content Status").Value = StrNewStatus If StrOldSubject <> StrNewSubject Then _ .BuiltInDocumentProperties("Subject").Value = StrNewSubject If bSaved = True And .Saved = True Then Exit Sub End With End Sub |
#8
|
||||
|
||||
Hi Karl,
Oops! Here's the corrected (and complete) code: Code:
Private Sub Document_Close() Dim StrOldTitle As String, StrNewTitle As String Dim StrOldAuthor As String, StrNewAuthor As String Dim StrOldSubject As String, StrNewSubject As String Dim StrOldStatus As String, StrNewStatus As String Dim bSaved As Boolean With ActiveDocument bSaved = .Saved StrOldTitle = .BuiltInDocumentProperties("Title").Value StrOldAuthor = .BuiltInDocumentProperties("Author").Value StrOldSubject = .BuiltInDocumentProperties("Subject").Value StrOldStatus = .BuiltInDocumentProperties("Content Status").Value StrNewTitle = InputBox("Enter the Document Title:", "Title", StrOldTitle) StrNewAuthor = InputBox("Enter the Document Author:", "Author", StrOldAuthor) StrNewSubject = InputBox("Enter the Client and Site Name:", "Subject", StrOldSubject) StrNewStatus = InputBox("Enter the Document Status:", "Status", StrOldStatus) If StrOldTitle <> StrNewTitle Then _ .BuiltInDocumentProperties("Title").Value = StrNewTitle If StrOldAuthor <> StrNewAuthor Then _ .BuiltInDocumentProperties("Author").Value = StrNewAuthor If StrOldSubject <> StrNewSubject Then _ .BuiltInDocumentProperties("Subject").Value = StrNewSubject If StrOldStatus <> StrNewStatus Then _ .BuiltInDocumentProperties("Content Status").Value = StrNewStatus If bSaved = True And .Saved = True Then Exit Sub End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Almost there! ...
Quote:
StrNewStatus = InputBox("Enter the Document Status:", "Content Status", StrOldStatus) Many thanks Karl |
#10
|
||||
|
||||
Hi Karl,
That change is inconsequential so far as the macro's correct execution is concerned. What gets displayed in the InputBox is independent of the doc property's name.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Ok, thanks.
Karl |
#12
|
|||
|
|||
Hi, How would you do this with the fields, rather than document properties?
|
#13
|
||||
|
||||
Do what, precisely?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
Exactly the same thing. Have a prompt to enter fields. In the advanced properties of Word are custom fields (e.g. checked by, client, date completed etc) that I would like to modify in the document with prompt dialog boxes. cheers.
|
#15
|
||||
|
||||
Well, you could use FILLIN fields but, other than displaying whatever you input at the location where the fields are in the document, I'm not sure what that achieves that you could achieve more easily by just typing the data into the same locations without the fields - they certainly won't add anything to the document metadata
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Disable email security prompt | mcinnes01 | Outlook | 1 | 03-17-2011 07:00 AM |
Selecting the macro document | lars | Word VBA | 0 | 08-19-2010 06:06 AM |
Macro to put content into keywords properties? | erik2000 | Word VBA | 3 | 03-05-2010 10:14 PM |
Macro to export document sections to individual txt files? | MJMR999 | Excel Programming | 0 | 02-18-2010 12:49 PM |
document properties issues | charris1980 | Word | 0 | 04-29-2009 12:49 PM |