Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

saving file into newly created folder

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andrewwinters - 24 Aug 2006 14:20 GMT
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
Helmut Weber - 24 Aug 2006 16:06 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Jay Freedman - 24 Aug 2006 16:15 GMT
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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> 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
[quoted text clipped - 43 lines]
>
>    End Sub
andrewwinters - 24 Aug 2006 23:50 GMT
Thanks so much.  Sweet success!  I really appreciate the speedy and helpful
replies.

> Make strSaveName contain the complete path and filename:
>
[quoted text clipped - 62 lines]
> >
> >    End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.