 |
| Word VBA Discuss using VBA to program Word. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 18th, 2008, 10:35 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
Enumerating and changing Word properties
I want to run through a series of folders and copy the Date Created for each document to it's Title using either VBA or VBScript. Is this possible? For example:
From:
Doc1.doc
Title: ""
DateCreated: 1/1/2007
Doc2.doc
Title: "Sample"
DateCreated: 1/2/2007
To:
Doc1.doc
Title: "1/1/2007"
DateCreated: 1/1/2007
Doc2.doc
Title: "Sample, 1/2/2007"
DateCreated: 1/2/2007
Thanks.
__________________
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|

December 19th, 2008, 05:42 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
You can try using BuiltInDocumentProperties
Code:
Sub StoreProperty()
Dim oWD As Word.Document
Set oWD = Word.Documents.Open("c:\sample.doc")
oWD.BuiltInDocumentProperties("Title") = oWD.BuiltInDocumentProperties("Creation date")
oWD.Save
oWD.Close (False)
End Sub
Cheers
Shasur
|
|

December 19th, 2008, 09:03 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
Thanks, that looks promising. I am doing this from inside Access, and I get
Microsoft Visual Basic
Compile Error:
User-defined type not defined.
Is there a reference I am missing?
__________________
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|

December 19th, 2008, 09:11 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
You are right
You need to Include Microsoft Word 11.0 (or whatever) Object Library
Cheers
Shasur
|
|

December 19th, 2008, 09:20 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
The object library worked to get me past the Word.Document error, but now I am stuck on:
Set oWD = Word.Documents.Open("C:\Sample.doc")
I get:
Microsoft Visual Basic
Run-time error '429':
ActiveX component can't create object
oWD = Nothing
There is a word doc at C:\Sample.doc that is closed when this runs.
__________________
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|

December 19th, 2008, 09:26 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
I changed this line:
Dim oWD As Word.Document
to this:
Dim oWD As New Word.Document
I still get the same error, but now the tool tip says:
oWD = <Object variable or With block variable not set>
__________________
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|

December 19th, 2008, 10:05 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
Oh! You are trying from Access. I should have changed the code to create a Word instance. Can you try if the following works
Code:
Sub StoreProperty_V2()
Dim oWA As Word.Application
Dim oWD As Word.Document
Set oWA = New Word.Application
Set oWD = Word.Documents.Open("c:\sample.doc")
oWD.BuiltInDocumentProperties("Title") = oWD.BuiltInDocumentProperties("Creation date")
oWD.Save
oWD.Close (False)
oWA.Quit
If Not oWD Is Nothing Then Set oWD = Nothing
If Not oWA Is Nothing Then Set oWA = Nothing
End Sub
Cheers
Shasur
|
|

December 19th, 2008, 10:13 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
Sorry for the confusion. This runs without errors, but it doesn't write to the Title, so the Title remains unchanged. I guess I will create a csv of file attributes and then do the updates manually, unless you have further suggestions.
__________________
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|

December 19th, 2008, 10:20 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
Can you hardcode the title and check if it works
Code:
Sub StoreProperty_V2()
Dim oWA As Word.Application
Dim oWD As Word.Document
Set oWA = New Word.Application
Set oWD = oWA .Documents.Open("c:\sample.doc")
oWD.BuiltInDocumentProperties("Title") = "Mytitle" ' oWD.BuiltInDocumentProperties("Creation date")
oWD.Save
oWD.Close (False)
oWA.Quit
If Not oWD Is Nothing Then Set oWD = Nothing
If Not oWA Is Nothing Then Set oWA = Nothing
End Sub
cheers
Shasur
|
|
 |