Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2014, 11:12 AM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default 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
I currently get "Run-time error '438': Object doesn't support this property or method" at the highlighted line.

If "Text" is not the property for the text contained in a table cell, what is?
Reply With Quote
  #2  
Old 07-31-2014, 01:03 PM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default

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
But the value for all of the document properties is coming from docTitleCell.
Reply With Quote
  #3  
Old 07-31-2014, 03:30 PM
macropod's Avatar
macropod macropod is offline Automate document properties Windows 7 32bit Automate document properties Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #4  
Old 08-01-2014, 06:38 AM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default

Title, subject, author, and manager properties are all still getting the value from Tables(1).Cell(2, 2).Range.
Reply With Quote
  #5  
Old 08-01-2014, 03:04 PM
macropod's Avatar
macropod macropod is offline Automate document properties Windows 7 32bit Automate document properties Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That is simply not possible. I suggest you check what is in your document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 08-04-2014, 05:48 AM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 08-04-2014, 05:54 AM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default

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
Images of the table and document properties are attached.
Attached Images
File Type: png DocInfoTable.png (20.3 KB, 24 views)
File Type: png DocProperties.png (30.4 KB, 22 views)
Reply With Quote
  #8  
Old 08-04-2014, 06:05 AM
macropod's Avatar
macropod macropod is offline Automate document properties Windows 7 32bit Automate document properties Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #9  
Old 08-04-2014, 06:10 AM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default

".docm" is not a valid file type for upload.
Reply With Quote
  #10  
Old 08-04-2014, 06:16 AM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default

Here it is as a .docx (code lost).
Attached Files
File Type: docx Generic Document Template 20140804.docx (49.5 KB, 14 views)
Reply With Quote
  #11  
Old 08-04-2014, 06:29 AM
macropod's Avatar
macropod macropod is offline Automate document properties Windows 7 32bit Automate document properties Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #12  
Old 08-04-2014, 06:31 AM
Ted C Ted C is offline Automate document properties Windows XP Automate document properties Office 2007
Novice
Automate document properties
 
Join Date: Oct 2011
Posts: 18
Ted C is on a distinguished road
Default

Ah! There's the problem!
Reply With Quote
Reply

Tags
properties



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automate document properties Help with Document Properties Prompt Macro KJJ Word VBA 14 11-10-2016 08:18 PM
Automate document properties 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
Automate document properties 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:53 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft