Hello All,

I've been having a look around for an answer to my question and have tried
to piece together some VBA code for similar things but I'm a bit of an
amateur and can't get anything to work! So... I'm looking for some help!

What I want to do is basically create quotes and faxes from two separate
templates in Microsoft Word 2003 and then save them to a Quotes or a Faxes
folder. Can this be determined automatically depending on the template being
used? And can I set the filename automatically to be a fied from my document?

Is this possible? Any help would be much appreciated!

Many Thanks

RE: Saving to a specific file with a specific file name by JeanGuyMarcil

JeanGuyMarcil
Thu Jul 03 05:10:00 PDT 2008

"Chr1551" wrote:

> Hello All,
>
> I've been having a look around for an answer to my question and have tried
> to piece together some VBA code for similar things but I'm a bit of an
> amateur and can't get anything to work! So... I'm looking for some help!
>
> What I want to do is basically create quotes and faxes from two separate
> templates in Microsoft Word 2003 and then save them to a Quotes or a Faxes
> folder. Can this be determined automatically depending on the template being
> used? And can I set the filename automatically to be a fied from my document?
>
> Is this possible? Any help would be much appreciated!
>
> Many Thanks

Yes, you can easily do that, but the only problem in your post is that it is
entirely unclear where the file name will come from. In the sample below, the
file name is lifted from a formfield called "Name". If you are working with
actual fields, it will be different...

This code displays the File Save As dialog with pre-entered information. It
assumes that one of the template names has the string "Quotes" in it.


Dim strPath As String
Dim strName As String

Const strQuotes As String = "Quotes"
Const strFaxes As String = "Faxes"

'Get template name...
If InStr(1, ActiveDocument.AttachedTemplate.Name, strQuotes) > 0 Then
strPath = "C:\" & strQuotes & Application.PathSeparator
Else
strPath = "C:\" & strFaxes & Application.PathSeparator
End If

'Get file name from document
strName = ActiveDocument.FormFields("Name").Result

'Set the new path you wish to use
ChangeFileOpenDirectory (strPath)
With Dialogs(wdDialogFileSaveAs)
.Name = strPath & strName & ".doc"
'If the user pressed "Save"
If .Display = -1 Then
'Execute the dialog
.Execute
End If
End With


RE: Saving to a specific file with a specific file name by JeanGuyMarcil

JeanGuyMarcil
Thu Jul 03 05:14:01 PDT 2008

"Chr1551" wrote:

> Hello All,
>
> I've been having a look around for an answer to my question and have tried
> to piece together some VBA code for similar things but I'm a bit of an
> amateur and can't get anything to work! So... I'm looking for some help!
>
> What I want to do is basically create quotes and faxes from two separate
> templates in Microsoft Word 2003 and then save them to a Quotes or a Faxes
> folder. Can this be determined automatically depending on the template being
> used? And can I set the filename automatically to be a fied from my document?
>
> Is this possible? Any help would be much appreciated!
>
> Many Thanks

Ooops, I posted too fast... The code I postd in my previous post came from
two sources, and I did not adjust it correctly.
See the difference in the (wdDialogFileSaveAs).Name value assignment...


Dim strPath As String
Dim strName As String

Const strQuotes As String = "Quotes"
Const strFaxes As String = "Faxes"

'Get template name...
If InStr(1, ActiveDocument.AttachedTemplate.Name, strQuotes) > 0 Then
strPath = "C:\" & strQuotes & Application.PathSeparator
Else
strPath = "C:\" & strFaxes & Application.PathSeparator
End If

'Get file name from document
strName = ActiveDocument.FormFields("Name").Result

'Set the new path you wish to use
ChangeFileOpenDirectory (strPath)
With Dialogs(wdDialogFileSaveAs)
.Name = strName & ".doc"
'If the user pressed "Save"
If .Display = -1 Then
'Execute the dialog
.Execute
End If
End With


Re: Saving to a specific file with a specific file name by Graham

Graham
Thu Jul 03 05:19:03 PDT 2008

You can intercept the FileSave (and SaveAs) command using a macro *in the
particular document template* eg

Sub FileSave()
On Error Resume Next
ChangeFileOpenDirectory "D:\My Documents\Faxes\"
ActiveDocument.Save
End Sub

You can also name the file from a field's content. Tell us more about the
Word version and the nature of the field you wish to use.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Chr1551 wrote:
> Hello All,
>
> I've been having a look around for an answer to my question and have
> tried to piece together some VBA code for similar things but I'm a
> bit of an amateur and can't get anything to work! So... I'm looking
> for some help!
>
> What I want to do is basically create quotes and faxes from two
> separate templates in Microsoft Word 2003 and then save them to a
> Quotes or a Faxes folder. Can this be determined automatically
> depending on the template being used? And can I set the filename
> automatically to be a fied from my document?
>
> Is this possible? Any help would be much appreciated!
>
> Many Thanks



Re: Saving to a specific file with a specific file name by Chr1551

Chr1551
Thu Jul 03 08:48:03 PDT 2008

Thank you both for your help. I'm using Word 2003 and have a table in my
document, I was hoping that I could use one of the cells in the table to name
the file eg. the Quote Reference cell.

"Graham Mayor" wrote:

> You can intercept the FileSave (and SaveAs) command using a macro *in the
> particular document template* eg
>
> Sub FileSave()
> On Error Resume Next
> ChangeFileOpenDirectory "D:\My Documents\Faxes\"
> ActiveDocument.Save
> End Sub
>
> You can also name the file from a field's content. Tell us more about the
> Word version and the nature of the field you wish to use.
>
> --
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor - Word MVP
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
>
> Chr1551 wrote:
> > Hello All,
> >
> > I've been having a look around for an answer to my question and have
> > tried to piece together some VBA code for similar things but I'm a
> > bit of an amateur and can't get anything to work! So... I'm looking
> > for some help!
> >
> > What I want to do is basically create quotes and faxes from two
> > separate templates in Microsoft Word 2003 and then save them to a
> > Quotes or a Faxes folder. Can this be determined automatically
> > depending on the template being used? And can I set the filename
> > automatically to be a fied from my document?
> >
> > Is this possible? Any help would be much appreciated!
> >
> > Many Thanks
>
>
>

Re: Saving to a specific file with a specific file name by JeanGuyMarcil

JeanGuyMarcil
Thu Jul 03 09:29:02 PDT 2008

"Chr1551" wrote:

> Thank you both for your help. I'm using Word 2003 and have a table in my
> document, I was hoping that I could use one of the cells in the table to name
> the file eg. the Quote Reference cell.
>

Try this variation of my earlier code:


Dim strPath As String
Dim strName As String

Const strQuotes As String = "Quotes"
Const strFaxes As String = "Faxes"

'Get template name...
If InStr(1, ActiveDocument.AttachedTemplate.Name, strQuotes) > 0 Then
strPath = "C:\" & strQuotes & Application.PathSeparator
Else
strPath = "C:\" & strFaxes & Application.PathSeparator
End If

'Get file name from document table _
(First table, third row, second column)
strName = ActiveDocument.Tables(1).Cell(3, 2).Range.Text
'Remove cell marker from string
strName = Left(strName, Len(strName) - 2)

'Set the new path you wish to use
ChangeFileOpenDirectory (strPath)
With Dialogs(wdDialogFileSaveAs)
.Name = strName & ".doc"
'If the user pressed "Save"
If .Display = -1 Then
'Execute the dialog
.Execute
End If
End With


But, this code will probably have to be modified depending on the exact
content of the target cell...

Re: Saving to a specific file with a specific file name by Chr1551

Chr1551
Thu Jul 03 09:52:01 PDT 2008

Thanks Jean-Guy, I have two separate templates, 1 for quotes and one for
faxes, I opened the one for quotes and opened the VBA editor and clicked
'this document' on the left hand side and pasted your code in there. Is this
correct? I changed the cell reference and then modified the top code as
follows:

strPath = "M:\Quotes\" & strFaxes & Application.PathSeparator

'Get file name from document table _
(First table, third row, second column)
strName = ActiveDocument.Tables(1).Cell(7, 2).Range.Text
'Remove cell marker from string
strName = Left(strName, Len(strName) - 2)

'Set the new path you wish to use
ChangeFileOpenDirectory (strPath)
With Dialogs(wdDialogFileSaveAs)
.Name = strName & ".doc"
'If the user pressed "Save"
If .Display = -1 Then
'Execute the dialog
.Execute
End If
End With

No luck though... it just keeps opening my documents to save as usual and
the document name is still the default!



"Jean-Guy Marcil" wrote:

> "Chr1551" wrote:
>
> > Thank you both for your help. I'm using Word 2003 and have a table in my
> > document, I was hoping that I could use one of the cells in the table to name
> > the file eg. the Quote Reference cell.
> >
>
> Try this variation of my earlier code:
>
>
> Dim strPath As String
> Dim strName As String
>
> Const strQuotes As String = "Quotes"
> Const strFaxes As String = "Faxes"
>
> 'Get template name...
> If InStr(1, ActiveDocument.AttachedTemplate.Name, strQuotes) > 0 Then
> strPath = "C:\" & strQuotes & Application.PathSeparator
> Else
> strPath = "C:\" & strFaxes & Application.PathSeparator
> End If
>
> 'Get file name from document table _
> (First table, third row, second column)
> strName = ActiveDocument.Tables(1).Cell(3, 2).Range.Text
> 'Remove cell marker from string
> strName = Left(strName, Len(strName) - 2)
>
> 'Set the new path you wish to use
> ChangeFileOpenDirectory (strPath)
> With Dialogs(wdDialogFileSaveAs)
> .Name = strName & ".doc"
> 'If the user pressed "Save"
> If .Display = -1 Then
> 'Execute the dialog
> .Execute
> End If
> End With
>
>
> But, this code will probably have to be modified depending on the exact
> content of the target cell...

Re: Saving to a specific file with a specific file name by Chr1551

Chr1551
Thu Jul 03 10:16:01 PDT 2008

Well... I got things working with the M:\Quotes directory opening for saving
but when I added the code for naming the file it no longer works, while the
naming code works fine (if slowly)!! Strange! This is what I have:

Sub FileSaveAs()
Dim strName As String
On Error Resume Next
ChangeFileOpenDirectory "M:\Quotes\"
'Get file name from document table _
(First table, third row, second column)
strName = ActiveDocument.Tables(1).Cell(7, 2).Range.Text
'Remove cell marker from string
strName = Left(strName, Len(strName) - 2)

'Set the new path you wish to use
ChangeFileOpenDirectory (strPath)
With Dialogs(wdDialogFileSaveAs)
.Name = strName & ".doc"
'If the user pressed "Save"
If .Display = -1 Then
'Execute the dialog
.Execute
End If
End With
End Sub




"Chr1551" wrote:

> Thanks Jean-Guy, I have two separate templates, 1 for quotes and one for
> faxes, I opened the one for quotes and opened the VBA editor and clicked
> 'this document' on the left hand side and pasted your code in there. Is this
> correct? I changed the cell reference and then modified the top code as
> follows:
>
> strPath = "M:\Quotes\" & strFaxes & Application.PathSeparator
>
> 'Get file name from document table _
> (First table, third row, second column)
> strName = ActiveDocument.Tables(1).Cell(7, 2).Range.Text
> 'Remove cell marker from string
> strName = Left(strName, Len(strName) - 2)
>
> 'Set the new path you wish to use
> ChangeFileOpenDirectory (strPath)
> With Dialogs(wdDialogFileSaveAs)
> .Name = strName & ".doc"
> 'If the user pressed "Save"
> If .Display = -1 Then
> 'Execute the dialog
> .Execute
> End If
> End With
>
> No luck though... it just keeps opening my documents to save as usual and
> the document name is still the default!
>
>
>
> "Jean-Guy Marcil" wrote:
>
> > "Chr1551" wrote:
> >
> > > Thank you both for your help. I'm using Word 2003 and have a table in my
> > > document, I was hoping that I could use one of the cells in the table to name
> > > the file eg. the Quote Reference cell.
> > >
> >
> > Try this variation of my earlier code:
> >
> >
> > Dim strPath As String
> > Dim strName As String
> >
> > Const strQuotes As String = "Quotes"
> > Const strFaxes As String = "Faxes"
> >
> > 'Get template name...
> > If InStr(1, ActiveDocument.AttachedTemplate.Name, strQuotes) > 0 Then
> > strPath = "C:\" & strQuotes & Application.PathSeparator
> > Else
> > strPath = "C:\" & strFaxes & Application.PathSeparator
> > End If
> >
> > 'Get file name from document table _
> > (First table, third row, second column)
> > strName = ActiveDocument.Tables(1).Cell(3, 2).Range.Text
> > 'Remove cell marker from string
> > strName = Left(strName, Len(strName) - 2)
> >
> > 'Set the new path you wish to use
> > ChangeFileOpenDirectory (strPath)
> > With Dialogs(wdDialogFileSaveAs)
> > .Name = strName & ".doc"
> > 'If the user pressed "Save"
> > If .Display = -1 Then
> > 'Execute the dialog
> > .Execute
> > End If
> > End With
> >
> >
> > But, this code will probably have to be modified depending on the exact
> > content of the target cell...

Re: Saving to a specific file with a specific file name by JeanGuyMarcil

JeanGuyMarcil
Thu Jul 03 10:20:01 PDT 2008

"Chr1551" wrote:

> Thanks Jean-Guy, I have two separate templates, 1 for quotes and one for
> faxes, I opened the one for quotes and opened the VBA editor and clicked
> 'this document' on the left hand side and pasted your code in there. Is this
> correct? I changed the cell reference and then modified the top code as
> follows:

I just provided the code, not the full routine. Make sure you have
"sandwiched" the code you are using between

Sub mySubName()

'Your Code

End Sub


Also, how are you executing the code (using the code from the document)?

Re: Saving to a specific file with a specific file name by Chr1551

Chr1551
Thu Jul 03 10:30:17 PDT 2008

I got it guys, thanks so much for pointing me in the right direction - much
appreciated!

"Chr1551" wrote:

> Thanks Jean-Guy, I have two separate templates, 1 for quotes and one for
> faxes, I opened the one for quotes and opened the VBA editor and clicked
> 'this document' on the left hand side and pasted your code in there. Is this
> correct? I changed the cell reference and then modified the top code as
> follows:
>
> strPath = "M:\Quotes\" & strFaxes & Application.PathSeparator
>
> 'Get file name from document table _
> (First table, third row, second column)
> strName = ActiveDocument.Tables(1).Cell(7, 2).Range.Text
> 'Remove cell marker from string
> strName = Left(strName, Len(strName) - 2)
>
> 'Set the new path you wish to use
> ChangeFileOpenDirectory (strPath)
> With Dialogs(wdDialogFileSaveAs)
> .Name = strName & ".doc"
> 'If the user pressed "Save"
> If .Display = -1 Then
> 'Execute the dialog
> .Execute
> End If
> End With
>
> No luck though... it just keeps opening my documents to save as usual and
> the document name is still the default!
>
>
>
> "Jean-Guy Marcil" wrote:
>
> > "Chr1551" wrote:
> >
> > > Thank you both for your help. I'm using Word 2003 and have a table in my
> > > document, I was hoping that I could use one of the cells in the table to name
> > > the file eg. the Quote Reference cell.
> > >
> >
> > Try this variation of my earlier code:
> >
> >
> > Dim strPath As String
> > Dim strName As String
> >
> > Const strQuotes As String = "Quotes"
> > Const strFaxes As String = "Faxes"
> >
> > 'Get template name...
> > If InStr(1, ActiveDocument.AttachedTemplate.Name, strQuotes) > 0 Then
> > strPath = "C:\" & strQuotes & Application.PathSeparator
> > Else
> > strPath = "C:\" & strFaxes & Application.PathSeparator
> > End If
> >
> > 'Get file name from document table _
> > (First table, third row, second column)
> > strName = ActiveDocument.Tables(1).Cell(3, 2).Range.Text
> > 'Remove cell marker from string
> > strName = Left(strName, Len(strName) - 2)
> >
> > 'Set the new path you wish to use
> > ChangeFileOpenDirectory (strPath)
> > With Dialogs(wdDialogFileSaveAs)
> > .Name = strName & ".doc"
> > 'If the user pressed "Save"
> > If .Display = -1 Then
> > 'Execute the dialog
> > .Execute
> > End If
> > End With
> >
> >
> > But, this code will probably have to be modified depending on the exact
> > content of the target cell...