saving file into newly created folder

A

andrewwinters

I have code for using a click button in an Access form to take data from the
form, open a series of Word templates and insert data from the form into the
template bookmarks. I also can create a new folder, using data from the
Access form. But I'm getting stuck trying to then save the newly created
Word documents into the new folder. Instead it saves into MyDocuments. This
is a basic version of what the Code looks like:

Private Sub CreateDocuments_Click()

Dim Wrd As New Word.Application
Set Wrd = CreateObject("Word.Application")
Wrd.Documents.Add "C:\Documents and Settings\asw\My
Documents\appearance.dot"
Wrd.Visible = True

With Wrd.ActiveDocument.Bookmarks
.Item("County").Range.Text = County
.Item("State").Range.Text = State
End With

Dim newfol As String
newfol = County
MkDir (newfol)

Dim strSaveName As String
strSaveName = State
Wrd.ActiveDocument.SaveAs strSaveName

I can't now save the document into the newly created folder, which in this
example is named after the Access "County" bookmark. When I try
ChangeFileOpenDirectory _
"C:\Documents and Settings\asw\My Documents\County\"

I get an error not recognizing the new folder (even though when I check
later, the folder was, in fact, created).

Same thing when I try

ChangeFileOpenDirectory _ "C:\Documents and Settings\asw\My
Documents\newfol\"

Any suggestions? Thanks!













Wrd.ActiveDocument.Close



End Sub
 
H

Helmut Weber

Hi,

you cannot put a variable in a literal like you tried.

sTmp = "C:\Documents and Settings\asw\My Documents\"
stmp = stmp & County & "\"
stmp = chr(34) & sTmp & chr(34)

ChangeFileOpenDirectory sTmp

All untested.


HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jay Freedman

Make strSaveName contain the complete path and filename:

strSaveName = "C:\Documents and Settings\asw\My Documents\" _
& County & "\" & State
Wrd.ActiveDocument.SaveAs strSaveName

Another point, which you would have discovered if your previous attempt
worked: If the folder already exists, the MkDir function causes an error. To
avoid this, use On Error statements. The simplest version goes like this:

newfol = County
On Error Resume Next
MkDir (newfol)
On Error GoTo 0

This still isn't very robust -- you need to handle errors such as County or
State containing characters that aren't valid in folder or file names, or if
they're empty. This will do for a start, though.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

andrewwinters

Thanks so much. Sweet success! I really appreciate the speedy and helpful
replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top