|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
How to rename the word doc with the name mentioned in the body of the document
Hi Experts
I am curious to know that is it possible to create a macro which will rename the word doc file using the number mentioned in the body of the word doc. Ex - Policy number - 12345 Is it possible to create a macro. The Macro will check the word "Policy number" in the body and when it find, it will use the number and rename the word doc with it "12345" as per the above example. Please find the sample file attached with the post. Regards, Jaggi |
#2
|
||||
|
||||
I am not sure what the relevance of the attached document is as it is already named with the number, but the following will do the job:
Code:
Sub SavePolicyDoc() Dim orng As Range Dim vNumber As Variant Dim strText As String Dim bFound As Boolean Const strPath As String = "" 'Put the folder path here e.g. "C:\Path\" Set orng = ActiveDocument.Range With orng.Find Do While .Execute(FindText:="Policy number - [0-9]{1,}", MatchWildcards:=True) strText = orng.Text bFound = True vNumber = Split(strText, "-") ActiveDocument.SaveAs Filename:=strPath & Trim(vNumber(UBound(vNumber))) & ".docx" Exit Do Loop If Not bFound Then MsgBox "The policy number was not found" End With Set orng = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Hi Graham
Thanks for the code it is working fine. Is it possible to change the "Policy number" from core integer number to combination of number and character like - "ABC3782/00088ABC" Ex - Policy number ABC3782/00088ABC In case if there is no hypen between Policy number and number, how to deal with it. Regards, Jaggi |
#4
|
||||
|
||||
Cross-posted at: http://www.vbaexpress.com/forum/show...ody-of-the-doc
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
The 'number' section of Policy number ABC3782/00088ABC contains an illegal filename character "/" So you would not be able to name a file with a string containing that character. If you were to replace it with (say) an underscore you could use
Code:
Do While .Execute(FindText:="Policy number [A-Z]{1,}[0-9]{1,}/[0-9]{1,}[A-Z]{1,}", MatchWildcards:=True) strText = orng.Text bFound = True vNumber = Split(strText, " ") ActiveDocument.SaveAs Filename:=strPath & Replace(Trim(vNumber(UBound(vNumber))), "/", "_") & ".docx" Exit Do Loop
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
Hi Graham
Thanks for the code.... Sorry for bothering you so much. I am curious to know is it possible to set the "number" dynamic. I mean to say the sequence of number and character used before or after the "/". Example - ABC3782/00088ABC 3782ABC/00088ABC 04674A14/00088ABC The above code will not going to work for 2nd and 3rd example. Is it possible to set the assigned number criteria dynamic Regards, Jaggi |
#7
|
||||
|
||||
Clearly not with that particular code. If the policy number in the document always has the '/' character it may be possible to do something, but unless we have a very clear idea of what formats to expect, it is not going to be possible to produce a one size fits all approach.
It might be easier if the number is always in its own paragraph or other location that can easily be defined, but so far you just keep moving the goal posts. Tell us EXACTLY what you have and what you want and we may be able to help.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
||||
|
||||
You could use:
FindText:="Policy number <[A-Z0-9]@/[A-Z0-9]@>"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Hi Graham
Sorry for all the confusion and let me give you clear picture what exactly I am looking for. Everyday we receive around 200 word doc with same format. What we have to do is to change the name of the word doc with its "Policy number". It is really a time consuming process where the person has to open each word doc, copy the policy number and rename it with the worddoc name. I want to make it automation. We save the all the files in a common location. Is there any way around to rename thes files with their respective policy number. Please let me know if you need further clarification. Regards, Jaggi |
#10
|
||||
|
||||
Presumbaly, then, the policy #s are always in a consistent place in the documents (e.g. 3rd paragraph, last word, or something such). Knowing that would make processing much simpler and quicker. And, since you have "around 200" documents to process, the whole job of renaming a folder full of them could be done without the user having to open each one.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Hi Paul
Yes, the policy number #s are always in a constant place at the top of the word doc. Please find the sample document attached. Regards, Jaggi |
#12
|
||||
|
||||
Your attachment has a space either side of the '/', which you hadn't mentioned before. That is a fairly fundamental difference. Furthermore, using spaces to align the content as your document depicts is extremely inefficient and is most unlikely to be used by any automation process. Ordinarily, one would expect to see such content in table cells or at least with tabs & tab-stops to manage the alignments. So, is this just something you've typed up to look like one of your documents, or does it reflect the content of an actual document produced by your system?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Hi Paul
Sorry for the confusion. I just checked the doc version and yes there is a space before and after the "/". Could we please amend it in the code. I created the sample file manually as in typed it and sent to you. This is just to give you idea where exactly the Policy number is located in the doc file. You are absulately right about the table cell. The word doc is in table format. Sorry again for the confusion Please let me know if you need further clarification. Regards, Jaggi |
#14
|
||||
|
||||
What is required is not something you simply typed up to look like the files you're processing but a sample of the actual output (you can change anything sensitive). There is a vast difference between processing something that uses tables and processing what you have posted so far.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
Hi Paul
Please check the sample file and let me know if it is fine? Regards, Jagdev |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to rename multiple Word file with same suffix | ozil61 | Word VBA | 2 | 05-06-2014 07:36 AM |
How to specify Document Title from the body of my Document | SymphonyTomorrow | Word | 9 | 08-05-2013 05:31 PM |
Rename Document & Save | d4okeefe | Word VBA | 4 | 05-23-2013 09:35 AM |
How to import Word Style Headings and body text into Excel | antztaylor | Excel | 5 | 11-08-2012 09:54 PM |
Advancing Paragraph Numbers in Merge Document Body | Wyskers | Mail Merge | 1 | 11-30-2011 04:46 AM |