View Full Version : Solved: Save As
mgonzalez07
09-13-2005, 06:29 PM
I'm trying to automatically save a document with the filename being whatever is in 3 separate formfield textboxes (for example, Date FirstName LastName.doc). I don't know how to do it with more than one text box. This is what I have so far:
Sub Save()
strvar = Replace(ActiveDocument.Bookmarks("Text9").Range, "FORMTEXT ", "")
ActiveDocument.SaveAs FileName:="C:\Documents and Settings\Compaq_Owner\My Documents" & _
"" & strvar, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
Can someone help me modify this to work for 3 formfield text boxes?
Tommy
09-14-2005, 06:38 AM
Hi mgonzalez07,
I edited your post to add the VBA tags so the formating will be the same as in the VBA editor.
You need to concatenation with the & character
Sub Save()
strvar = Replace(ActiveDocument.Bookmarks("Text9").Range, "FORMTEXT ", "") _
& Replace(ActiveDocument.Bookmarks("Text10").Range, "FORMTEXT ", "") _
& Replace(ActiveDocument.Bookmarks("Text11").Range, "FORMTEXT ", "")
ActiveDocument.SaveAs FileName:="C:\Documents and Settings\Compaq_Owner\My Documents" _
& "" & strvar, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
fumei
09-14-2005, 08:54 AM
Uh........yo! People, what is with this FORMTEXT crap? And Replace? Phoooey.
ActiveDocment.Bookmarks("bookmarkName").Result returns the contents of the formfield.
if it is a straightforward concatentation:
strvar = ActiveDocument.Bookmarks("Text9").Result & _
ActiveDocument.Bookmarks("Text10").Result & _
ActiveDocument.Bookmarks("Text11").Result
1. You may want to consider making your document into an explicit object. If you have a lot of code that uses ActiveDocument it makes it a lot easier.
Dim ThisDoc As Document
Set ThisDoc = ActiveDocument
Now you can write:
strvar = ThisDoc.Bookmarks("Text9").Result & _
ThisDoc.Bookmarks("Text10").Result & _
ThisDoc.Bookmarks("Text11").Result
2. It is a very good idea to explicitly name formfields. It is NOT a good idea to keep Word default names ("Text2", "Text9") etc.
Tommy
09-14-2005, 09:22 AM
LOL Gerry no doubt you are right! :)
I prefer to use the formfields as:
Dim FormInfo As FormFields
Set FormInfo = ThisDocument.FormFields
strVar = FormInfo("Text9").Result & _
FormInfo("Text10").Result & FormInfo("Text11").Result
This is just me I hate to type :)
MOS MASTER
09-14-2005, 03:39 PM
This is just me I hate to type :)
LOL! :rotlaugh:
Welcome aboard buddy! :thumb (Hi gerry!!)
mgonzalez07
09-14-2005, 05:13 PM
Thanks for the help guys. It works great.
MOS MASTER
09-15-2005, 02:27 PM
Glad to see you've found your sollution! :thumb
geekgirlau
09-15-2005, 10:20 PM
Sorry to be picky (no, actually I LIVE to be picky http://vbaexpress.com/forum/images/smilies/tongue.gif ) but you might also want to clean up your "SaveAs" command.
ActiveDocument.SaveAs _
FileName:="C:\Documents and Settings\Compaq_Owner\My Documents\" & strvar
When you record a macro it gives you every possible parameter. However unless you are setting a parameter to something other than the default value, it serves no purpose.
... on the never-ending journey towards streamlined code ...
fumei
09-16-2005, 12:29 AM
Two BIG thumbs up geekgirlau! Bang on.
fumei
09-16-2005, 12:45 AM
Oh and Tommy....agreed. Using the Formfields collection as an object itself is MUCH more efficient.
mgonzalez07, if you use some of the suggestions here, rememeber that if you use Set something = whatever, you should always end your code with Set something = Nothing.
This destroys the object - that is, it frees the memory address allocated for it.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.