PDA

View Full Version : [SOLVED:] Help with error message and bookmarks



Jfp87
09-18-2014, 03:44 PM
Guys, another few questions. First, here's the code (word 2010):

file = Dir("F:\Joe\Test Folder\Test Destination\")
Do While Len(file) > 0
With Documents(file)
.Bookmarks("Discip").Range.Text = strDiscip
.Bookmarks("ProjectNumber").Range.Text = lngProjNum
.Bookmarks("Rev").Range.Text = "A1"
.ContentControls(1).Range.Text = Me.cboClient.Value
.ContentControls(2).Range.Text = cboAsset.Value
.ContentControls(3).Range.Text = txtWpkDocNo.Value
.ContentControls(4).Range.Text = Me.cboDiscip.Value
.ContentControls(5).Range.Text = txtWpkTitle.Value
End With
file = Dir
Loop

When I run the code I am getting an error at line 3 saying "bad file name". I am unsure if this error is a problem with the code or one of my files.

Second question relates to the first: is it possible to have identical bookmarks in multiple documents, and update all of them? That's what I'm trying to do.

Again, the helps appreciated.

Joe

fumei
09-18-2014, 04:01 PM
DIR is a function that you use to action ONE file at a time from a group of files. Your Dir does not define agroup of files. You need something like /Destination/*.doc, thus defining all .doc files in the Destination folder. Using just the slash does not define files.

Usually you then do something like

Do While file <> ""

Do the following actions if file is NOT empty. There is no file name in your code thus "bad" filename.

Second, yes sure. Loop through each document and action the bookmark. BTW, your does not appear to actually Open any documents (besides the filename issue).

Jfp87
09-18-2014, 04:08 PM
Thanks, that makes sense.

I didn't think I had to open the documents to update the bookmarks?

Jfp87
09-18-2014, 04:43 PM
I'm still getting the same error message...i'm pretty sure line 3 is wrong. I don't think that is the correct way to achieve what I want.

gmaxey
09-18-2014, 05:14 PM
Sub ScratchMacro()
Dim strFile As String
Dim oDoc As Word.Document
strFile = Dir("D:\My Documents\Word\*.doc")
Do While strFile <> ""
Set oDoc = Documents.Open(strFile)
'Do whatever with the file, e.g.,
Debug.Print oDoc.FullName
oDoc.Close wdSaveChanges
Set oDoc = Nothing
strFile = Dir
Loop
End Sub

Jfp87
09-18-2014, 06:45 PM
Thanks Greg, I think that's what I was looking for.

Joe

fumei
09-19-2014, 08:22 PM
Like I said...you need to open a file to actually do anything. There is a good example in VBA Help. That is often a reasonable place to look.