Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
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
 
Old December 18th, 2008, 10:35 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default 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
 
Old December 19th, 2008, 05:42 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

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
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old December 19th, 2008, 09:03 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
 
Old December 19th, 2008, 09:11 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

You are right

You need to Include Microsoft Word 11.0 (or whatever) Object Library

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old December 19th, 2008, 09:20 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
 
Old December 19th, 2008, 09:26 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
 
Old December 19th, 2008, 10:05 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

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
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)
 
Old December 19th, 2008, 10:13 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
 
Old December 19th, 2008, 10:20 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

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
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
CRYSTAL REPORT 9 Changing Connection Properties aquiintac VB Components 0 November 6th, 2007 05:17 AM
Extracting Custom Document Properties for Word Doc jayanth_nadig XSLT 2 October 30th, 2007 03:31 AM
changing properties at runtime reza_ahmadi Java Espanol 0 December 26th, 2006 07:10 AM
Enumerating the reports collection dmoffice Pro VB 6 2 August 29th, 2006 03:25 PM
Changing forms properties at run time Louisa VB.NET 2002/2003 Basics 5 January 15th, 2004 01:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.