Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a New Document from a Template Using VBA

Status
Not open for further replies.

JasonEnsor

Programmer
Sep 14, 2010
193
GB
Hey,

I have setup several templates now which all work nicely, however i want each instance of the template to be named in a certain way. The information that i want to use for the name in the document is from information gathered in a userform on the template file. Is this possible?

I do not know where the user will be saving the file so i can't hard code the whole save path. i just want it so when the document is generated from the template it has a unique name from variables stored. eg it wont be called New Document 1, it maybe called Username date and subject....(if those 3 were inputted on to the userform). I hope that makes sense.

I am using word 2003, but if some1 has code for excel 2003 i am sure i can modify it

Thank you in advance

Jason
 
New documents do not have a name until they are saved. That is why they are indicated as "Document 1" etc.

I take it you have a userform displayed with the _New event of the template. OK. You get string variables from the userform.

Who is going to control this? YOU, as the developer can grab the strings, and do a SaveAs (which must be done).

"i just want it so when the document is generated from the template it has a unique name from variables stored. "

Nope. sorry. It does not have a unique name until it is saved. Well, it sort of does - Document 1, Document 2 etc. etc.


unknown
 
So i guess i am best to do a .SaveAs then get them to move the documents to wherever they desire before sending them through. i thought it might be the case where i would need to save the documents. I suppose it is quite logical that one would need to Save the file to assign the name.

Thanks for confirming the information for me. Much appreciated :)
 
I do not know what you mean by "sending them through".

But naming them (by saving them), and then moving them seems odd. Why? Why not save them to the location desired in the first place?

You could get your name strings (from th evariables set through the userform), and then if you have enough information simply save th efile with the unique name you want.

If the ONLY thing you need to complete the path/filename, is the path (you have the name itself), then give the user a folder dialog selection process. They choose the folder, click OK, and the name (from the userform) is added to make the complete SaveAs name.

Code:
Function PickFolder(strStartDir As Variant) As String
    Dim SA As Object, F As Object
    Set SA = CreateObject("Shell.Application")
    Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
    If (Not F Is Nothing) Then
        PickFolder = F.items.Item.path
    End If
    Set F = Nothing
    Set SA = Nothing
End Function

Sub Yadda()
Dim myName As String
myName = "yadda.doc"  ' replace with your userform string
ActiveDocument.SaveAs PickFolder("c:\") & "\" & myName
End Sub

The function (PickFolder) displays a folder dialog - a starting folder is given as a parameter. The user navigates to the folder they want, and clicks OK. That folder name is given to PickFolder (the string).

The Sub takes that folder string name and uses it with the string name (from your userform variables), and saves the file.

Note: you need to add the slash after the PickFolder value. Say you picked the c:\whatever\blah folder. Just adding the filename string (say Client.doc") would end up being:

c:\whatever\blahClient.doc

The file would be named blahClient.doc, in the whatever folder. So add the slash.

Also, you probably need to add the .doc extension to the name string variable (from the userform).


unknown
 
The template is for an exam paper, so what would happen is the exam paper originator (The one that writes it) would write the exam, this would then have to be checked by an internal moderator to make sure it is correct. To do this they would save the file (Normally with a stupid name), then send it to the Internal Moderator. Once this is complete it is then emailed to my department to make any final changes. This normally involves changing the file name, and altering some of the formatting of the document.

My idea was if i set the name in stone when the exam paper is generated from the template it is easier to trace when it goes to the moderator, as sometimes we get the Originators version named xxx.doc and then we have the moderator who sends it to us and it is called somethingelse.doc and then we have our final version.

If people would follow procedures then i wouldnt have all of this hassle....but then i probably wouldnt have a job. Thank you for the Save code, i think i will set up an area for the papers to be saved using your method and see how that works.

Thanks

Jason

 
My idea was if i set the name in stone when the exam paper is generated from the template it is easier to trace when it goes to the moderator
That's OK IFthe 'originator' doesn't decide to change the name for whatever reason ... As you say:
If people would follow procedures then i wouldnt have all of this hassle....


Cheers
[MS MVP - Word]
 
If people would follow procedures then i wouldnt have all of this hassle...."

Well....ummmm....yeeessssss, that is true.


unknown
 
Well i have put the template out there for the staff to use, i guess i just have to wait now for them to think of all the ways they can to screw it all up for me.

Thanks again everyone for the help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top