View Single Post
 
Old 04-07-2016, 09:32 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
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

The function I posted would not be listed in macros as it is intended to be called from either another macro or the process in the link.

The process can work with individual documents, but it is an overhead that would irritate for your regular work pattern. I included it to increase traffic to my web site, which provides a small income to help justify the time spent in these forums, and of course if you were processing large numbers of documents, it works well to handle the folders where the documents are saved.

As I suggested, it can easily be modified to work as a stand alone macro (see below) to extract the required contents of the table. However having seen your sample document, it has an extra row at the top, which does not appear in your illustration, so the row count needs to be incremented by one, to read the required cells.
Code:
Sub RenameDoc()
Dim oDoc As Document
Dim oTable As Table
Dim oCell As Range
Dim sFname As String
Const sPath As String = "C:\Path\"    'the path to save the documents
    On Error GoTo err_Handler
    Set oDoc = ActiveDocument
    Set oTable = oDoc.Tables(1)
    Set oCell = oTable.Rows(7).Cells(3).Range
    oCell.End = oCell.End - 1
    sFname = oCell.Text & Chr(32)
    Set oCell = oTable.Rows(5).Cells(1).Range
    oCell.End = oCell.End - 1
    sFname = sFname & oCell.Text & Chr(32)
    Set oCell = oTable.Rows(3).Cells(1).Range
    oCell.End = oCell.End - 1
    sFname = sPath & sFname & oCell.Text & ".docx"
    oDoc.SaveAs2 Filename:=sFname, Addtorecentfiles:=False
lbl_Exit:
    Exit Sub
err_Handler:
    Err.Clear
    GoTo lbl_Exit
End Sub
Paul's macro does much the same but uses a different method to extract the data from the cells. I'll leave it to you to decide which is the easier to comprehend.
__________________
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