Thread: [Solved] Word file naming macro
View Single Post
 
Old 10-15-2016, 08:58 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I have a feeling that you are approaching this from the wrong angle. Do we take it that you are using an existing document and renaming it for each new letter? If so, don't!

Save the letter as a template and create new letters from it. By saving it as a macro enabled template you can save any macros that the template needs in the template itself.

You say that the filename is the same name as the name of a related PDF, but you don't say what the relationship to the PDF is. You cannot automate the filename from the PDF if the macro that does the naming knows nothing about the PDF.

The initials can usually be derived from the Username (see below), but if the users have names with e.g. suffixes or titles, a lookup might be simpler than catering for each individual option.

The shortened recipient name can probably be derived from the recipient name, but you have not explained how it is shortened, or if it is shortened in a consistent manner.

Where do the Claim number and Invoice number come from? Are they manually input? If so, it would make sense to create a useform, that fires when you create a new document from the template. The userform would collect the variable data used in the template and write it to the document either using bookmarks or docvariable fields to position the data in the letter. It would also assemble the filename and save the document. The PDF however remains an unknown quantity.

See http://www.gmayor.com/Userform.htm which covers the basic principles. You will also find the FillBM function on my web site, that will write a value to a bookmark.

If you want any assistance over the detail than you will need to provide more information.
Code:
Option Explicit

Function GetInitials() As String
Dim sInitials As String: sInitials = ""
Dim vName As Variant
Dim i As Long
    vName = Split(Environ("Username"), Chr(32))
    For i = 0 To UBound(vName)
        sInitials = sInitials & Left(Trim(vName(i)), 1)
    Next i
    GetInitials = sInitials
lbl_Exit:
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote