how to manipulate SaveAs so that it saves as the name we want

A

Associates

Hi,

Is there a way of taking control of what filename we want to save the file
as in the word 03?

For example, in a letter template, we have a Subject field as a DOCVARIABLE.
When user go to save as and the Save As dialog box pops up, we like it to
take the value of the Subject from the Subject field as the file name (rather
than whatever comes up in the first line or paragraph, which word document
usually does by default).

I have the following code as follows

Private Sub Document_Close()
ActiveDocument.SaveAs
FileName:=ActiveDocument.Variables("subject").Value,
fileformat:=wdFormatDocument
End Sub

There is no such object like "SaveAs" for Document. Hence, i used
Document_Close instead. However, the problem with this is it automatically
save the file with the Subject as the filename and close immediately without
letting user to browse for where to park the file.

Your help would be greatly appreciated.

Thank you in advance
 
G

Greg Maxey

Something like this:

Sub ScratchMacro()
Dim pStr As String
pStr = ActiveDocument.BuiltInDocumentProperties("Subject")
With Dialogs(wdDialogFileSaveAs)
.Name = pStr
.Show
End With
End Sub
 
A

Associates

Thank you Greg for your reply.

It works but not the way i wanted it. Where should i put the code? I put the
code under AutoClose macro. It gets called everytime the document is closed
even when there is no changes made to it.

I was thinking about doing something to the Save As under the File menu on
the toolbar on the top. How do i access that Save As so that it would
automatically put in the subject to the File Name and all users have to do is
to browse for location and click OK to save and close it.

Sorry if i don't explain it well enough

Thank you in advance
 
G

Gordon Bentley-Mix

We must be gluttons for punishment, eh Greg? <g>

In the ThisDocument module insert the following:

Private Sub Document_Close()
Dim myFileName As String
myFileName = ActiveDocument.Variables("subject").Value
If ActiveDocument.Name <> myFileName Then
With Dialogs(wdDialogFileSaveAs)
.Name = myFileName
.Show
End With
End If
End Sub

This will display the Save As dialog if and only if the file name is not the
same as the value in the "subject" variable.

If you want to take a bit of control over the File | Save As... process, you
can also put the following into any code module in your project:

Sub FileSaveAs()
Dim myFileName As String
myFileName = ActiveDocument.Variables("subject").Value
If ActiveDocument.Name <> myFileName Then
With Dialogs(wdDialogFileSaveAs)
.Name = myFileName
.Show
End With
End If
End Sub

Of course neither of these processes will stop the user from changing the
file name that's displayed in the Save As dialog, but at least it's a start.

You should also add some error handling - especially around checking for the
existence of the document variable before trying to access it.

And if you really wanted to be a good programmer, you would do something
about the use of ActiveDocument; it's lazy and risky.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
A

Associates

Thank you Gordon,

It works now.



Gordon Bentley-Mix said:
We must be gluttons for punishment, eh Greg? <g>

In the ThisDocument module insert the following:

Private Sub Document_Close()
Dim myFileName As String
myFileName = ActiveDocument.Variables("subject").Value
If ActiveDocument.Name <> myFileName Then
With Dialogs(wdDialogFileSaveAs)
.Name = myFileName
.Show
End With
End If
End Sub

This will display the Save As dialog if and only if the file name is not the
same as the value in the "subject" variable.

If you want to take a bit of control over the File | Save As... process, you
can also put the following into any code module in your project:

Sub FileSaveAs()
Dim myFileName As String
myFileName = ActiveDocument.Variables("subject").Value
If ActiveDocument.Name <> myFileName Then
With Dialogs(wdDialogFileSaveAs)
.Name = myFileName
.Show
End With
End If
End Sub

Of course neither of these processes will stop the user from changing the
file name that's displayed in the Save As dialog, but at least it's a start.

You should also add some error handling - especially around checking for the
existence of the document variable before trying to access it.

And if you really wanted to be a good programmer, you would do something
about the use of ActiveDocument; it's lazy and risky.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

You're most welcome.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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