Specify directory to save to

C

chris_huh

I have a template which automatically generates the dates for the
following week (sunday to saturday) for any new document based on the
template. It also sets the filename to be those dates.
The only problem is that whenever i go to save the document it tries
to save it in some odd temporary folder. How can i specify what
directory to save the document to. The only things i can find about it
seem to be how to make a file automatically save to a directory, which
i don't think is necessary; i just want to be able to change the
folder that is shown when you go File>Save.

Thanks
 
D

Doug Robbins - Word MVP

Not sure how you are setting the filename, but include the path in it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

chris_huh

Not sure how you are setting the filename, but include the path in it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP







- Show quoted text -

The code that i have is:

Function WeekStart() As Date
Dim wday As Byte
wday = Weekday(Date)
Select Case wday
Case 1
WeekStart = Date
Case 2
WeekStart = Date - 1
Case 3
WeekStart = Date - 2
Case 4
WeekStart = Date - 3
Case 5
WeekStart = Date - 4
Case 6
WeekStart = Date - 5
Case 7
WeekStart = Date - 6
End Select
End Function

Sub AutoNew()
Selection.GoTo What:=wdGoToBookmark, Name:="DateRange"
Selection.InsertBefore Format(WeekStart() + 7, "dddd, d MMMM")
Selection.InsertAfter " to "
Selection.InsertAfter Format(WeekStart() + 13, "dddd, d MMMM yyyy")

Sunday = Format(WeekStart() + 7, "d")
Saturday = Format(WeekStart() + 13, "d MMM yy")

FileTitle = Sunday + " to " + Saturday

With Dialogs(wdDialogFileSummaryInfo)
.Title = FileTitle
.Execute
End With
End Sub

The last few lines deal with the filename. If i include the directory
structure with the filename (ie FileTitle = "C:\" + Sunday + " to " +
Sunday) it doesnt work and it just tries to have that as the filename.
 
J

Jean-Guy Marcil

chris_huh said:
The code that i have is:

Function WeekStart() As Date
Dim wday As Byte
wday = Weekday(Date)
Select Case wday
Case 1
WeekStart = Date
Case 2
WeekStart = Date - 1
Case 3
WeekStart = Date - 2
Case 4
WeekStart = Date - 3
Case 5
WeekStart = Date - 4
Case 6
WeekStart = Date - 5
Case 7
WeekStart = Date - 6
End Select
End Function

Sub AutoNew()
Selection.GoTo What:=wdGoToBookmark, Name:="DateRange"
Selection.InsertBefore Format(WeekStart() + 7, "dddd, d MMMM")
Selection.InsertAfter " to "
Selection.InsertAfter Format(WeekStart() + 13, "dddd, d MMMM yyyy")

Sunday = Format(WeekStart() + 7, "d")
Saturday = Format(WeekStart() + 13, "d MMM yy")

FileTitle = Sunday + " to " + Saturday

With Dialogs(wdDialogFileSummaryInfo)
.Title = FileTitle
.Execute
End With
End Sub

The last few lines deal with the filename. If i include the directory
structure with the filename (ie FileTitle = "C:\" + Sunday + " to " +
Sunday) it doesnt work and it just tries to have that as the filename.

Why don't you replace

With Dialogs(wdDialogFileSummaryInfo)
.Title = FileTitle
.Execute
End With

with

ActiveDocument.SaveAs "C:\myPath\" & FileTitle

???

By the way, the prefered way of concatenating elements in VBA is using the
ampersand "&" and not the plus sign "+".

Run this code to see the difference:

Dim str1 As String
Dim lng1 As Long

str1 = "10"
lng1 = 5

MsgBox str1 + lng1
MsgBox str1 & lng1


Jay Freedman has a good description of what is going on here:

http://groups.google.com/group/micr...ba3a8f5d1b1f?hl=en&lnk=st&q=#f750ba3a8f5d1b1f
 
C

chris_huh

Why don't you replace

   With Dialogs(wdDialogFileSummaryInfo)
      .Title = FileTitle
      .Execute
   End With

with

ActiveDocument.SaveAs "C:\myPath\" & FileTitle

???

By the way, the prefered way of concatenating elements in VBA is using the
ampersand "&" and not the plus sign "+".

Run this code to see the difference:

Dim str1 As String
Dim lng1 As Long

str1 = "10"
lng1 = 5

MsgBox str1 + lng1
MsgBox str1 & lng1

Jay Freedman has a good description of what is going on here:

http://groups.google.com/group/microsoft.public.word.vba.beginners/br...-Hide quoted text -

- Show quoted text -

Ah that works better, thanks. I have got it to go into the correct
directory now, but it automatically saves it as soon as the document
is opened. I guess this is because it is in the AutoNew bit. I don't
know a great deal about macros and vb (hence the + not &). Where would
i put this so that you still have to actively save the file, and
hopefully have the option the change the title/directory (ie through
the save file browse window).

Thanks
 
J

Jean-Guy Marcil

chris_huh said:
Ah that works better, thanks. I have got it to go into the correct
directory now, but it automatically saves it as soon as the document
is opened. I guess this is because it is in the AutoNew bit. I don't
know a great deal about macros and vb (hence the + not &). Where would
i put this so that you still have to actively save the file, and
hopefully have the option the change the title/directory (ie through
the save file browse window).

If you want to give the user the option of over-riding your suggestions, use
the dialogue but in order to point to a specific folder, you need a bit more
code.
Here is what you vcould do:

'Decalre this variable with your other variables
Dim sOrgPath As String
Dim sPath As String

'When you get to the saving part, use something like:
'Remember the original path
sOrgPath = Options.DefaultFilePath(wdDocumentsPath)
'Set the new path you wish to use
ChangeFileOpenDirectory ("C:\")
With Dialogs(wdDialogFileSaveAs)
.Name = FileTitle
'If the user pressed "Save"
If .Display = -1 Then
'Execute the dialog
.Execute
End If
'Restore original path
ChangeFileOpenDirectory sOrgPath

End With
 

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