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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specifying bookmarks in VBA 1

Status
Not open for further replies.

JanesC

Technical User
Jan 15, 2002
29
GB
Using Word I have created a template form (.dot) with 'text form fields' for users to enter text. One of these fields is called OrderNo. Word has automatically made them bookmarks.
I have created a command button which i want users to click which will save this document in a specific folder. I want the new document name to include the text which was entered in the OrderNo bookmark.
Is there a way of getting this data?

(I'm not very good at code so i've included what i've done for some kind person to modify for me!)

Private Sub CommandButton1_Click()
ChangeFileOpenDirectory_ "\\Server\WORK\Projects\Templates\PurchaseRequests\"
ActiveDocument.SaveAs FileName:="PurchaseRequestForm" & "OrderNo" & ".doc",
MsgBox (" Saving file in: Server1\WORK\Projects\Templates\PurchaseRequests")

End Sub

Thanks
 
JanesC,

Try this:

Code:
Private Sub CommandButton1_Click()
Const Path = "\\Server\WORK\Projects\Templates\PurchaseRequests\"
Dim FName As String
Dim OrderNum As String

OrderNum = ActiveDocument.FormFields("Text2").Result
FName = Path & "PurchaseRequestForm_OrderNo" & OrderNum
ActiveDocument.SaveAs Filename:=FName, FileFormat:=wdFormatDocument, SaveFormsData:=True
MsgBox " Saving file in: " & Path, vbInformation + vbOkOnly,"File Save"

End sub

Notes: I assigned the destination directory to a constant, which is fine if this never or rarely changes. You could also reference a publicly declared variable if the path needs to be determined at run-time. Replace "Text2" with the bookmark name of your Text Form Field.

Hope this helps [smile]

M. Smith
 
It works! Thanks very much!
Just one more question. Can I save the layout of the document instead of just the data in the fields?
This is so i can view and print the form again.
 
JanesC,

Use the following, in which I've highlighted the changes:

Code:
Private Sub CommandButton1_Click()
Const Path = "\\Server\WORK\Projects\Templates\PurchaseRequests\"
Dim FName As String
Dim OrderNum As String

OrderNum = ActiveDocument.FormFields("Text2").Result
FName = Path & "PurchaseRequestForm_OrderNo" & OrderNum
Code:
& ".doc"
Code:
ActiveDocument.SaveAs FileName:=FName, FileFormat:=wdFormatDocument, SaveFormsData:=
Code:
False
Code:
MsgBox " Saving file in: " & Path, vbInformation + vbOKOnly, "File Save"

End Sub

One caveat: The
Code:
Document.SaveAs
method will overwrite an existing file with the same name without warning.


Regards,
M. Smith
 
Sorry to pester you again. Everytime i change Saveformsdata to false and run the code i get an error message saying i'm out of disk space. I can't be out of space because when i change it to true it works fine.

Any ideas? If you don't thats fine, at least i can save the raw data.

Thanks again.

Jane
 
Jane,

Sounds like a bug. I'll search for a more definitive answer, but in the meantime, see what happens if you eliminate the
Code:
SaveFormsData
parameter altogether, since this is an optional param and defaults to False.

Btw, I tested the code using Word 97. What version are you using?


Regards,
Mike
 
The document must have got corrupted with all the messing about i did. I've re-created it and it works like a charm!

Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top