#1
|
|||
|
|||
Automate document properties
I would like to automatically set certain document properties in a Word document when the user saves the file. I can send text to the properties, but I am having trouble retrieving the values I want to use from the document content.
The second page of the document is a table of document information, such as title, subject, and author. I want to use this table as the source for the document properties. Here is my code. Code:
Sub SetProperties() ' ' Set key document properties ' Set docInfoTable = ThisDocument.Tables(1) Set docTitleCell = docInfoTable.Cell(Row:=2, Column:=2) Set docSubjectCell = docInfoTable.Cell(Row:=4, Column:=2) Set docAuthorCell = docInfoTable.Cell(Row:=9, Column:=2) Set docManagerCell = docInfoTable.Cell(Row:=10, Column:=2) ThisDocument.BuiltInDocumentProperties("title") = docTitleCell.Text ThisDocument.BuiltInDocumentProperties("subject") = docSubjectCell.Text ThisDocument.BuiltInDocumentProperties("author") = docAuthorCell.Text ThisDocument.BuiltInDocumentProperties("manager") = docManagerCell.Text ThisDocument.BuiltInDocumentProperties("category") = "Generic Template" ' ' Save the document ' ActiveDocument.Save ' ' This macro replaces FileSave on the CTRL+s keyboard shortcut ' End Sub If "Text" is not the property for the text contained in a table cell, what is? |
#2
|
|||
|
|||
Update the code. This does not crash.
Code:
Sub SetProperties() ' ' Set key document properties ' Set docInfoTable = ThisDocument.Tables(1) Set docTitleCell = docInfoTable.Cell(Row:=2, Column:=2) Set docSubjectCell = docInfoTable.Cell(Row:=4, Column:=2) Set docAuthorCell = docInfoTable.Cell(Row:=9, Column:=2) Set docManagerCell = docInfoTable.Cell(Row:=10, Column:=2) ThisDocument.BuiltInDocumentProperties("title") = docTitleCell.Range.Text ThisDocument.BuiltInDocumentProperties("subject") = docSubjectCell.Range.Text ThisDocument.BuiltInDocumentProperties("author") = docAuthorCell.Range.Text ThisDocument.BuiltInDocumentProperties("manager") = docManagerCell.Range.Text ThisDocument.BuiltInDocumentProperties("category") = "Generic Template" ' ' Save the document ' ActiveDocument.Save ' ' This macro replaces FileSave on the CTRL+s keyboard shortcut ' End Sub |
#3
|
||||
|
||||
Try:
Code:
Sub SetProperties() ' ' Set key document properties ' Dim Rng As Range With ActiveDocument .BuiltInDocumentProperties("category") = "Generic Template" Set Rng = .Tables(1).Cell(2, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("title") = Rng.Text Set Rng = .Tables(1).Cell(4, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("subject") = Rng.Text Set Rng = .Tables(1).Cell(9, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("author") = Rng.Text Set Rng = .Tables(1).Cell(10, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("manager") = Rng.Text .Save End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Title, subject, author, and manager properties are all still getting the value from Tables(1).Cell(2, 2).Range.
|
#5
|
||||
|
||||
That is simply not possible. I suggest you check what is in your document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
I am looking at my document and at the code and that's exactly what is happening. The different table cells do not contain the same text.
|
#7
|
|||
|
|||
This is the code.
Code:
Sub SetProperties() ' ' Set key document properties ' Dim Rng As Range With ActiveDocument ' ' Category property .BuiltInDocumentProperties("category") = "Generic Template" ' ' Title property Set Rng = .Tables(1).Cell(2, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("title") = Rng.Text ' ' Subject property Set Rng = .Tables(1).Cell(4, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("subject") = Rng.Text ' ' Author property Set Rng = .Tables(1).Cell(9, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("author") = Rng.Text ' ' Manager property Set Rng = .Tables(1).Cell(10, 2).Range Rng.End = Rng.End - 1 .BuiltInDocumentProperties("manager") = Rng.Text ' ' Save document - macro will be mapped to CTRL+S .Save End With End Sub |
#8
|
||||
|
||||
Perhaps if you could attach a copy of the actual document to a post, it would be easier to work out what is going on.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
".docm" is not a valid file type for upload.
|
#10
|
|||
|
|||
Here it is as a .docx (code lost).
|
#11
|
||||
|
||||
Without wanting to place too fine a point on it, the data you're trying to access are in the second table...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
Ah! There's the problem!
|
Tags |
properties |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help with Document Properties Prompt Macro | KJJ | Word VBA | 14 | 11-10-2016 08:18 PM |
Updating Document Properties without using advanced properties dialogue | thedr9wningman | Word VBA | 3 | 01-20-2014 05:56 PM |
Key shortcut to access document properties | eroock | Word | 0 | 12-11-2012 10:54 AM |
Add custom document properties into document | NicBodkin | Word | 8 | 05-05-2011 09:09 AM |
document properties issues | charris1980 | Word | 0 | 04-29-2009 12:49 PM |