Log in

View Full Version : change/make copy of windows document file name based on content



gentle2005
10-28-2016, 03:02 AM
HI

I have nearly 580+ word documents(*.doc) file in one folder. I want to make copy of that file in the same folder with the name that is appearing inside the word file after "product name:******".

Example: original file name output file copy based on the content file name
xyz.doc product name:G5.doc
pqr.doc product name: G6.doc
abc.doc product name:G7.doc


sample file is attached. Can anybody try and explain the code logic?

mana
10-30-2016, 03:42 AM
Can you get the product name?



Option Explicit

Sub test()
Dim s As String
'
s = ActiveDocument.Tables(1).Cell(2, 2).Range.Text
s = Trim(Replace(Mid(s, 2), Chr(13) & Chr(7), ""))


MsgBox "Product Name is : " & s

End Sub

gmaxey
10-30-2016, 08:39 AM
After looking at your sample document getting the product name consistently could be problematic. However, if the Product name is defined consistently in your 580 documents, then you could process the folder using my Batch Process AddIn and the following user defined procedure. Save this procedure in your Normal template:


Function SaveAsProductName(oDoc As Document) As Boolean
Dim strProduct As String
Dim oDocSave As Document
On Error GoTo Err_Handler
strProduct = Trim(Left(oDoc.Tables(1).Cell(2, 2).Range.Text, Len(oDoc.Tables(1).Cell(2, 2).Range.Text) - 2))
strProduct = Right(strProduct, Len(strProduct) - 2)
oDoc.SaveAs2 oDoc.Path & Application.PathSeparator & strProduct, 0
SaveAsProductName = True
lbl_Exit:
Exit Function
Err_Handler:
SaveAsProductName = False
Resume lbl_Exit
End Function


You can download the addin here: http://gregmaxey.mvps.org/word_tip_pages/process_batch_folder_addin.html

gentle2005
10-30-2016, 09:55 AM
Thanks for your time and effort but it does not run at all. It just created the copy of same file name with .docx extension.

gmaxey
10-30-2016, 10:10 AM
It runs fine here. Put a stop in the code after the "On Error GoTo Err_Handler " e.g.,

On Error GoTo Err_Handler
Stop

Then run. When you reach the stop, step through using F8 and see what happens.