PDA

View Full Version : Writing a string to a custom document property on save event, and updated fields



bstephens
06-11-2011, 02:26 PM
I am looking to create a custom document property (the custom property called "DocID") on a save event, which then is updated in a { DOCPROPERTY "DocID" } field code in my footer as follows:

Suppose the user is working on a document called "X:\2011\70125.01\abc800.doc"
with the added complication, that sometimes users access the document through a search server and the document has a UNC name as "\\ro-server1\access\70125.01\abc800.doc"
Upon the user saving the document, the following complex string is written to a custom document property called "DocID"
2011\70125.01\abc800
Upon the save event, a field code which is already in the document called { DOCPROPERTY "DocID" } would automatically update and show the string "2011\70125.01\abc800"Where "2011\70125.01\abc800" is a string processed from ActiveDocument.FullName, and is processed the same regardless of whether the file has either a three letter extension (.doc) or a four letter extension (.docx), and regardless of whether the document is accessed from a mapped drive path or the UNC path (the UNC path is pretty static, so backing the string out, by counting from the fourth "\" symbol would probably be fine).

As far as updating the fields, I am already using the following repurposed save command from the ribbon to update fields, so I believe that part is handled, but I'm not clear how to write the custom document properties.

Anyone have ideas about writing the string to the custom document property upon save?


'Callback for FileSave onAction
Sub rxFileSave_repurpose(control As IRibbonControl, ByRef cancelDefault)
Call FileSave
Call UpdateFieldsALL
End Sub

'REPURPOSED SAVE COMMAND - Automatically updates the field upon a file SAVE event
Sub FileSave()
On Error Resume Next
ActiveDocument.Save
End Sub

Sub UpdateFieldsALL()

Dim sec As section
Application.ScreenUpdating = False

If Documents.Count = 0 Then Exit Sub

oldSaved = ActiveDocument.Saved

ActiveDocument.Fields.Update
For Each sec In ActiveDocument.Sections
sec.Headers(wdHeaderFooterPrimary).Range.Fields.Update
sec.Headers(wdHeaderFooterFirstPage).Range.Fields.Update
sec.Footers(wdHeaderFooterPrimary).Range.Fields.Update
sec.Footers(wdHeaderFooterFirstPage).Range.Fields.Update
Next
Application.ScreenUpdating = True
ActiveDocument.Saved = oldSaved
End Sub

bstephens
06-11-2011, 10:14 PM
"\\ro-server1\access\70125.01\abc800.doc" should read "\\ro-server1\access\2011\70125.01\abc800.doc" ("\\ro-server1\access\" is mapped as X: )

Frosty
06-12-2011, 10:20 AM
This is a multi-pronged question. Your other thread solved extracting the DocId from the filename, right? Just trying to clarify that this thread is (only) about writing a DocID string to a customdocument property. So your follow-up post is a little confusing. Writing to a custom docprop is easy... you just have to make sure it exists before you write to it (I've encapsulated the functionality, so you can call it with:

UpdateCustomDocProperty ActiveDocument, "DocID", "xyzzy"


Public Sub UpdateCustomDocProperty(oDoc As Document, sName As String, sValue As String)
On Error Resume Next
oDoc.CustomDocumentProperties(sName) = sValue
If Err.Number <> 0 Then
oDoc.CustomDocumentProperties.Add Name:=sName, _
LinkToContent:=False, _
Value:=sValue, _
Type:=msoPropertyTypeString
End If
On Error GoTo 0
End Sub


As for the save event and updating the document fields... those are sort of separate questions, and various people may have various strategies (all of which can work).

Personally, I prefer having footer information update when the document opens and you do a SaveAs, rather than on every save (which includes every time the user hits CTRL+S). You really only need to update when the info changes... that's one way of minimizing your programming footprint.

Other than that, fields are supposed to automatically update. So I would try to see why they aren't updating and when you actually need them to. Those fields should update when you re-open the document and when you print anyway, so what's the footer for?

But the way you have it structure, every time the user hits CTRL+S to save the last sentence he/she has typed, you're extracting file name info, dropping into a custom doc property, cycling through all the sections header objects to update all the fields... and none of that info has changed.

EDIT: code typo