PDA

View Full Version : Solved: saving and naming docs mergefield value



s?ren
03-13-2006, 02:38 AM
Hi there!

This is my first time in this forum and I hope to find what I?m looking for.

I found an "old" thread about saving docs and naming them according to merfield values.
This is precisely what I need. But .... I also need to be able to save the docs in corresponding folders.
I have 8 main categories (Client: GK, SK, GU, SU etc), and within each categori I have up to a max of 200 subcategories (Client number: GK-001, GK-002 ...., GK-200 a.s.o.) The mergefield I hope to use for naming the doc, gives me the initial client and clientnumber (GK-001-01). I then need to add a "name" to the clientnumber to specify the type of document.
So, Maincategory, Subcategory, docname-type all depending on the value of mergefield "client".
Does this make sense, and can anyone help me.:help

mdmackillop
03-13-2006, 04:30 AM
There is a KB Item here re auto saving merge documents
http://vbaexpress.com/kb/getarticle.php?kb_id=290
Regards
MD

Anne Troy
03-13-2006, 08:21 AM
Hi, soren. I made your post its own thread. :)

s?ren
03-14-2006, 05:19 AM
Thank You, Princess

:bow:

s?ren
03-14-2006, 05:25 AM
this message was cancelled
Replaced by #6

s?ren
03-14-2006, 07:27 AM
I´m SORRY:( if anyone has tried to work this out, but

It DOES WORK:joy: after all...

I made a small typing error/thinking error (Should have been left instead of right)

THIS IS the working code: (If anyone is interested)

Sub Gem_clientnr()
'***************'
'IMPORTANT NOTICE'
'***************'
'You need to create a table of 1 row, 3 cells at the top of the first page of doc.
'In this table you must insert the mergefield, that you wish to use as docname
'I use the same mergefield as base for my doc name, so
'---------------'
' 1) Dimension of variables:
Dim Source As Document, oblist As Document, DocName As Range, DocumentName As String
Dim i As Long, doctext As Range, target As Document
Dim MinFolderName As Range
Dim MinSubFolderName As Range

' 2) tell macro that active document holds table for docname:
Set Source = ActiveDocument
Set oblist = ActiveDocument
' 3) Find the table and cell in which doc, folder and subfoldername is placed:
Counter = 1
For i = 1 To oblist.Tables(1).Rows.Count
Set DocName = oblist.Tables(1).Cell(i, 1).Range
DocName.End = DocName.End - 1
Set MinFolderName = oblist.Tables(1).Cell(i, 2).Range
MinFolderName.End = MinFolderName.End - 1
Set MinSubFolderName = oblist.Tables(1).Cell(i, 3).Range
MinSubFolderName.End = MinSubFolderName.End - 1

' 4) Set the names for folder and subfolder to the first 2, respectively 6, characters of the mergefield data
MinFolderName = Left(MinFolderName, 2)
MinSubFolderName = Left(MinSubFolderName, 6)
' 5) Change the path in the following command to suit where you want to Save the documents.
DocumentName = "C:\Documents and Settings\Project\Desktop\Clientarchive\" & MinFolderName.Text & "\" & MinSubFolderName.Text & "\" & DocName.Text
Set doctext = Source.Sections(i).Range
doctext.End = doctext.End - 1
Set target = Documents.Add
target.Range.FormattedText = doctext
target.SaveAs FileName:=DocumentName
target.Close
Next i
End Sub