using UserForm variables within SaveAs filename

L

Lee

Hello

I am using Word2003, Visual Basic 6.4

I have a UserForm that successfully populates a number of bookmarks within a
document.

I would like to reuse two of these variables, "Client" and "InvYear" within
the document filename when saved.

The SaveAs FileName statement is called, after the UserForm, within my
Private Sub Document_New() section and looks like this (Order being an
automatically incremented invoice number.):
ActiveDocument.SaveAs FileName:="F:\My Documents\Invoice" & Client & InvYear
& Format(Order, "00#")

However, the files are being saved as F:\My Documents\Invoice008 and so on.
The Client and InvYear variables are not coming through.

I tried converting all the UserForm subroutines from Private to Public but
this didn't work. I have also tried creating new variables varClient and
varInvYear within the UserForm Private Sub CommandButton1_Click() in the
hope that these would come through even after the UserForm was closed. This
did not work.

I think I have two choices:

a) Find out how to create general variables that stay within the document
even after the UserForm is closed, or
b) Re-read the contents of the bookmarks once inserted in order to insert
them into the file name.

can anybody help me out here? I am a little stuck now.

Thanks

Lee
 
H

Harold

Hi Lee,
A couple ideas come to mind.
If you are declaring the variables in the UserForm they would be out of
scope in ThisDocument.
You can pass the values to Document Variables then call those variables in
the FileSave

This is from the VBA Help on Variables
Represents a variable stored as part of a document. Document variables are
used to preserve macro settings in between macro sessions. The Variable
object is a member of the Variables collection. The Variables collection
includes all the document variables in a document or template.

The help article does have some examples.

Another more elegant approach would be to use Get and Let properties within
a Class module.
 
S

Steve Lang

Hi Lee,

In your userform you can get the field and variable values back into the
calling routine if you don't unload it when you click your "OK" (or
equivalent) button. If you use a Me.Hide command instead, then in the
calling routine you can access the fields from the form and use them. Once
you have used them to your heart's content, in the calling sub, you unload
the form.

Private Sub Document_New()
Dim strClient as string
Dim strYear as String
With myForm
.Show
strClient = .txtClient.Text (or whatever the text field is
called)
strYear = .txtInvYear.Text (or whatever the text field is called)
End With
Unload MyForm
End Sub

In the OK button click event of the form, just put Me.Hide.

HTH and have a great day!


Steve
 
L

Lee

Steve Lang said:
Hi Lee,

In your userform you can get the field and variable values back into the
calling routine if you don't unload it when you click your "OK" (or
equivalent) button. If you use a Me.Hide command instead,...

Well. That worked a treat!

Thank you.

L
 

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