Saving TextBox value as a filename in a given location

H

Haroon

hi

i managed to write a code to save a template file name using whatever the
user typed in textbox, but i want to find out how to save that file in
another folder, e.g. on a network drive.

e.g if user types Test in TextBox1, and press OK button, the active file is
saved as 'Test' template, but i want to save it as word document .doc file
not as template and also save it in another folder.

anyone help pls

thanks.
 
A

Astrid

Hi Haroon,

The code to save a document would be :
ActiveDocument.SaveAs filename:="C:\Test",FileFormat:=wdFormatDocument

Where C:\Test is the name and location you want to file to be saved.
From you description I couldn't be sure if the textbox is a formfield inside
a protected document or a textbox on an userform.

If it's a formfield, try code like this:

--------------------------------------------------------------------------------------
Sub SaveMyDocument()
Dim sFileName As String
Dim sPath As String
Dim sCompleteName As String

sPath = "c:\"
'(Replace with the path you want to use)
sFileName = ActiveDocument.FormFields("Text1").Result
'(Replace with the name of the formfield you want to use)
'Combine both
sCompleteName = sPath & sFileName
ActiveDocument.SaveAs Filename:=sFileName, fileformat:=wdFormatDocument
End Sub
--------------------------------------------------------------------------------------

You mention the file gets saved as a template instead of a document, this
might suggest that you open the template itself instead of creating a new
document based on the template. How is this new document created?

If you don't use formfields but other sort of textboxes please post back
with a description in a bit more detail.

Kind regards,
Astrid
 
H

Haroon

hi Astrid,

thanks for quick reply.

im creating a template which prompts user with form when it opens up, the
document has bookmarks where info from textboxes on the form gets poopulated
on the document when user hits submit, i want to auto save this as a document
not as template using file name from one of textbox/bookmark in a network
drive.

hope you understand it now.

cheers
 
A

Astrid

Hi Haroon,

I asked the question about the creation of the documents cause there's a
difference in opening a template (File - Open) or creating a new document
based on a template (File - New select template - if template is saved in
either workgroup- or usertemplates folder - or doubleclick on file in Windows
Explorer)
If you open the file you see the name of the template in the titlebar, if
you create a new document based on a template, you see Document 1 (or any
other number depending on how many opened, unnamed documents are open)
If you open the template and use SaveAs to resave, the default format for
saving is template, if you create a new document based on the template, the
default format is document.

So you're using an userform?
In this case, instead of
sFileName = ActiveDocument.FormFields("Text1").Result
use
sFileName = NameOfTextbox.Text
if you use the code inside the userform itself, on the OK button perhaps.

Does this help or do you need other examples. If so, maybe you can post back
with the code you have so far?

Astrid
 
H

Haroon

sorry, im creating the template and form from scratch, and saving it as
template, so wen user opens the template the form pops up and user fills in
the details, when submit button is pressed the data from textboxes gets
placed in bookmarks on the document, after that I want this document to be
auto saved with the name from bookmark as a filename on the network drive.

does that help?
 
A

Astrid

Hi Haroon,

If you use the code from my previous posting in the OK button code, the
document gets saved with the name the user entered in the textbox.
Is this not what you need?

Astrid
 
H

Haroon

hi Astrid,

well the code works, but it is not saving in the location where i defined
it, e.g. E:\tests

it saves in the template folder.

this is the code with OK button:
------------
Private Sub CommandButton1_Click()
Dim sFileName As String
Dim sPath As String
Dim sCompleteName As String

sPath = "e:\"
'(Replace with the path you want to use)
sFileName = TextBox1.Text
'(Replace with the name of the formfield you want to use)
'Combine both
sCompleteName = sPath & sFileName
ActiveDocument.SaveAs FileName:=sFileName, fileformat:=wdFormatDocument
End Sub
 
G

Graham Mayor

Your SaveAs uses the wrong variable as a filename! It should be
sCompleteName
or you could use

Private Sub CommandButton1_Click()
Dim sFileName As String
Dim sPath As String
sPath = "e:\tests\"
sFileName = TextBox1.Text
'(Replace with the name of the formfield you want to use)
ActiveDocument.SaveAs FileName:=sPath & sFileName,
FileFormat:=wdFormatDocument
End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Haroon

hi

i get error 'run-time error '4198' when it runs through the 'saving the
document' code, if i remove that code, it does not give any errors.

can anyone find out why i get this error upon saving the template as a new
document with different location?

the code is..
------------
private sub commandbutton3_click()
Dim sFileName As String
Dim sFileName2 As String
Dim sPath As String
Me.MultiPage1.Value = 0
sFileName = TextBox1.Text
sFileName2 = TextBox3.Text
sPath = "G:\tests\"
ActiveDocument.SaveAs filename:=sPath & sFileName2 & " " & sFileName,
FileFormat:=wdFormatDocument
 
G

Graham Mayor

What do the two text boxes contain?
Is G: a hard drive?

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Haroon

works great, thanks


Graham Mayor said:
Your SaveAs uses the wrong variable as a filename! It should be
sCompleteName
or you could use

Private Sub CommandButton1_Click()
Dim sFileName As String
Dim sPath As String
sPath = "e:\tests\"
sFileName = TextBox1.Text
'(Replace with the name of the formfield you want to use)
ActiveDocument.SaveAs FileName:=sPath & sFileName,
FileFormat:=wdFormatDocument
End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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