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

Save a document as text from document 1

Status
Not open for further replies.

DECNET

Technical User
Apr 28, 2001
56
0
0
GB
Does anybody know of a way (macro) to save a Word document by using an item of text within that document?

I write many documents based on the same template and wondered if a macro could be written that would save the document using two of the titles / fields within that document.

For instance, the document would have the name of the recipient and a job number, ideally I would like the document to be saved as;

Job Number - Name.doc

I have set up Autotext and update fields that do the most of the work for me, if I could click on an icon to save it as well, using the above details, that would save me so much time, effort, energy etc. etc.

Word experts, it's over to you.

Thanks

DEC

If it helps, let me know, if it doesn't, let me know. It's the only way I'll learn.
 
Hi Clifford,

Assuming the items you refer to are stored in a bookmark in your document. And assuming that the path you save to is your user options path. This will do it for you:

===========================================================
Sub FileSave()
Dim strJobnumer As String
Dim strName As String
Dim strPath As String


'get the user options path
strPath = Options.DefaultFilePath(wdUserOptionsPath) & "\"

'check if the bookmarks still exists in the document
If ActiveDocument.Bookmarks.Exists("bmkJobnumber") Then
ActiveDocument.Bookmarks("bmkJobnumber").Select
strJobnumer = Selection
End If

If ActiveDocument.Bookmarks.Exists("bmkName") Then
ActiveDocument.Bookmarks("bmkName").Select
strName = Selection
End If

'create the full path + name to save the document
ActiveDocument.SaveAs FileName:=strPath & strJobnumer & " - " & strName & ".doc"

End Sub
============================================================

When you don not want the file to be saved directly, but want the SaveAs window to show, you can use this code:
Note: you can also put this code in the sub FileSave, the the SaveAs window then will always show.

============================================================
Sub FileSaveAs()
Dim strJobnumer As String
Dim strName As String
Dim strPath As String


'get the user options path (path can be set in Tools / options / File location tab)
strPath = Options.DefaultFilePath(wdUserOptionsPath) & "\"

'check if the bookmarks still exists in the document
If ActiveDocument.Bookmarks.Exists("bmkJobnumber") Then
ActiveDocument.Bookmarks("bmkJobnumber").Select
strJobnumer = Selection
End If

If ActiveDocument.Bookmarks.Exists("bmkName") Then
ActiveDocument.Bookmarks("bmkName").Select
strName = Selection
End If

'show the SaveAs window with the compound name
With Dialogs(wdDialogFileSaveAs)
.Name = strPath & strJobnumer & " - " & strName & ".doc"
.Show
End With


End Sub
===========================================================


 
I thought Bookmarks would have something to do with the answer. I have been looking at this and will give it a try. Would it be possible to change the path for saving the document? DEC

If it helps, let me know, if it doesn't, let me know. It's the only way I'll learn.
 
Hi Unica,

having a problem with the code, it only recognises the first bookmark and doesn't join them together, as a compound name.

I have deleted and recreated the bookmarks several times, but can't the compounding to work, any suggestions.

I'm going to re write the code now from the above details, if I have made a blunder I'll let you know.

Anyway thanks for what you have done thus far, it's exactly what I need. DEC

If it helps, let me know, if it doesn't, let me know. It's the only way I'll learn.
 
Unica,

resolved all the problems now, I had to write a small macro to automate the 'bookmarking' for the JobNumber, because it kept on entering spaces after the digits and it wouldn't compound the two bookmarks.

Anyhoo, all OK now, many thanks. DEC

If it helps, let me know, if it doesn't, let me know. It's the only way I'll learn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top