PDA

View Full Version : Bulk rename based on contents in document



enjam
08-03-2021, 07:13 PM
Hi there,

I have a set of 50 documents in a folder on my desktop with the following naming scheme:

50000.docx
50001.docx
50002.docx
.
.
.
50050.docx

I am trying to do a bulk rename of these documents based on contents stored within specific textboxes.

I can do this one at a time with the below code; however, it is quite a laborious process.

Sub FileSaveAs()
Dim StrNm As String
With ActiveDocument
StrNm = .Shapes(4).TextFrame.TextRange.Text & " " & .Shapes(36).TextFrame.TextRange.Text
With Dialogs(wdDialogFileSaveAs)
.Name = StrNm
.Show
End With
End With
End Sub


There are two complications here:

1) Not all of these word documents are actually word documents, and so the code must be able to move onto the next document if the Documents.Open function fails, and
2) Not all of the documents have textboxes from which there is data to extract

In both instances, the name of the file can remain the same.

I'm hoping 50000.docx can be renamed to the contents of 'Shape(4)' & 'Shape(36)'.docx in each document that is 1) able to be opened, and 2) where these shapes exist

Any guidance would be greatly appreciated.

Cheers,
enjam

Chas Kenyon
08-03-2021, 07:45 PM
The numbering of Shapes in Word is almost random in edited documents; to me, it is unpredictable. Named bookmarks are much more predictable as are xml nodes.
See Document Batch Processes by Graham Mayor, MVP (http://www.gmayor.com/document_batch_processes.htm) and Batch Folder Process Add-In by Greg Maxey (https://gregmaxey.com/word_tip_pages/process_batch_folder_addin.html) once you get your error handling down.

enjam
08-03-2021, 09:24 PM
Thanks Chas, I'll approach this problem slightly differently and post an alternative thread in the Excel section of this forum.