PDA

View Full Version : Solved: SaveAs with formfield value in current folder



Kim75
05-08-2010, 11:56 AM
Good evening the forum !

I have a document “reservation.doc”, in which there is a formfield filled by a text, and I have to save this document in the current folder with a file name using that text formfield

Here I have an opened file named “reservation.doc”, and I try to saveas it in the current folder using a date, for example “30.09.2010”

So I tried this macro, the file is saved in the current directory and with a text of a formfield, but with a bizarre combination :

“reservation.doc30.09.2010”

Is there someone for help ? :dau:

In ThisDocument :Private Sub CommandButton1_Click()
Call SaveForm
End SubIn Module1 :Sub SaveForm()
Dim fName As String
Dim CurrentFolder As String
With ActiveDocument
CurrentFolder = ActiveDocument.Path & Application.PathSeparator & ActiveDocument.Name
fName = .Bookmarks("DateReunion").Range.Text & ".doc"
.SaveAs CurrentFolder + fName
End With
End SubThanks, Kim.

Tinbendr
05-08-2010, 06:00 PM
An unsaved document does NOT have an extension with Activedocument.Name, however, a SAVED document with Activedocument.Name does has an extension.

In your example, you'll have to strip the .doc from the .Name.

(Since your using With.Activedocument, I'll drop the qualifier.)
CurrentFolder = .Path & _
Application.PathSeparator & _
Left(.Name, Len(.Name) - 4)

This also assumes that the extension will always be four characters long.

Kim75
05-08-2010, 07:05 PM
An unsaved document does NOT have an extension with Activedocument.Name, however, a SAVED document with Activedocument.Name does has an extension.

In your example, you'll have to strip the .doc from the .Name.

(Since your using With.Activedocument, I'll drop the qualifier.)
CurrentFolder = .Path & _
Application.PathSeparator & _
Left(.Name, Len(.Name) - 4)

This also assumes that the extension will always be four characters long.

Evening dear friend :hi:

thank you for your help :thumb , by dint of groping around, ma blindness finally paid :bug: , the following code works :

Sub SaveForm()
Dim fName As String
Dim CurrentFolder As String
With ActiveDocument
CurrentFolder = ActiveDocument.Path
fName = CurrentFolder & "\" & "reservation" & "." & .Bookmarks("DateReunion").Range.Text & ".doc"
.SaveAs fName
End With
End Sub

friendly, Kim

fumei
05-10-2010, 11:30 AM
I just want to point out that using file names with multiiple dots - "." - may not be a good idea.

CurrentFolder & "\" & "reservation" & "." & _
.Bookmarks("DateReunion").Range.Text & ".doc"

seems it could end up as:

"c:\Yadda\Whatever\reservation.30.09.2010.doc”

Why do you have dots in your dates? Would not underscore be better? You could do it like:
Sub SaveForm()
Dim fName As String
Dim FolderPath As String
With ActiveDocument
FolderPath = ActiveDocument.Path & "\"
fName = FolderPath & "reservation" & "_" & _
Replace( .Bookmarks("DateReunion").Range.Text, ".", "_") & ".doc"
.SaveAs fName
End With
End Sub thus ending up with

"c:\Yadda\Whatever\reservation_30_09_2010.doc”

mdmackillop
05-10-2010, 12:08 PM
Whilst you cannot do this with the simple date from your formfield, saving as
"c:\Yadda\Whatever\reservation_2010_09_30.doc”
allows file names to be more easily sorted into date order.

fumei
05-10-2010, 12:22 PM
"Whilst you cannot do this with the simple date from your formfield,"

Would that not depend on the format of the date?

I do agree that yyyy_mm_dd would (probably) be a better format.

BTW: why are you using
.Bookmarks("DateReunion").Range.Text
rather than:

.Bookmarks("DateReunion").Result