Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 18th, 2008, 10:35 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #2 (permalink)  
Old December 19th, 2008, 05:42 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
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)
Reply With Quote
  #3 (permalink)  
Old December 19th, 2008, 09:03 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #4 (permalink)  
Old December 19th, 2008, 09:11 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
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)
Reply With Quote
  #5 (permalink)  
Old December 19th, 2008, 09:20 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #6 (permalink)  
Old December 19th, 2008, 09:26 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #7 (permalink)  
Old December 19th, 2008, 10:05 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
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)
Reply With Quote
  #8 (permalink)  
Old December 19th, 2008, 10:13 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #9 (permalink)  
Old December 19th, 2008, 10:20 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
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)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 04: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 04:25 PM
Changing forms properties at run time Louisa VB.NET 2002/2003 Basics 5 January 15th, 2004 01:07 PM



All times are GMT -4. The time now is 01:55 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.