Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-20-2014, 04:44 AM
Jagdev Jagdev is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 64bit
Novice
How to rename the word doc with the name mentioned in the body of the document
 
Join Date: Nov 2014
Posts: 15
Jagdev is on a distinguished road
Post 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
Attached Files
File Type: docx 12345.docx (12.4 KB, 12 views)
Reply With Quote
  #2  
Old 11-20-2014, 06:31 AM
gmayor's Avatar
gmayor gmayor is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

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
Reply With Quote
  #3  
Old 11-20-2014, 09:09 PM
Jagdev Jagdev is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 64bit
Novice
How to rename the word doc with the name mentioned in the body of the document
 
Join Date: Nov 2014
Posts: 15
Jagdev is on a distinguished road
Post

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
Reply With Quote
  #4  
Old 11-20-2014, 10:04 PM
macropod's Avatar
macropod macropod is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 11-20-2014, 10:15 PM
gmayor's Avatar
gmayor gmayor is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

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
You can search for any unique string using wildcards - see http://www.gmayor.com/replace_using_wildcards.htm
__________________
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
  #6  
Old 11-20-2014, 10:57 PM
Jagdev Jagdev is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 64bit
Novice
How to rename the word doc with the name mentioned in the body of the document
 
Join Date: Nov 2014
Posts: 15
Jagdev is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 11-20-2014, 11:14 PM
gmayor's Avatar
gmayor gmayor is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

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
Reply With Quote
  #8  
Old 11-20-2014, 11:24 PM
macropod's Avatar
macropod macropod is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could use:
FindText:="Policy number <[A-Z0-9]@/[A-Z0-9]@>"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 11-20-2014, 11:29 PM
Jagdev Jagdev is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 64bit
Novice
How to rename the word doc with the name mentioned in the body of the document
 
Join Date: Nov 2014
Posts: 15
Jagdev is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 11-20-2014, 11:49 PM
macropod's Avatar
macropod macropod is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 11-21-2014, 12:14 AM
Jagdev Jagdev is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 64bit
Novice
How to rename the word doc with the name mentioned in the body of the document
 
Join Date: Nov 2014
Posts: 15
Jagdev is on a distinguished road
Post

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
Attached Files
File Type: docx Sample.docx (12.6 KB, 10 views)
Reply With Quote
  #12  
Old 11-21-2014, 12:24 AM
macropod's Avatar
macropod macropod is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #13  
Old 11-21-2014, 02:18 AM
Jagdev Jagdev is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 64bit
Novice
How to rename the word doc with the name mentioned in the body of the document
 
Join Date: Nov 2014
Posts: 15
Jagdev is on a distinguished road
Default

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
Reply With Quote
  #14  
Old 11-21-2014, 02:22 AM
macropod's Avatar
macropod macropod is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #15  
Old 11-21-2014, 02:40 AM
Jagdev Jagdev is offline How to rename the word doc with the name mentioned in the body of the document Windows 7 64bit How to rename the word doc with the name mentioned in the body of the document Office 2010 64bit
Novice
How to rename the word doc with the name mentioned in the body of the document
 
Join Date: Nov 2014
Posts: 15
Jagdev is on a distinguished road
Post

Hi Paul

Please check the sample file and let me know if it is fine?

Regards,
Jagdev
Attached Files
File Type: docx Sample_Final.docx (24.1 KB, 22 views)
Reply With Quote
Reply



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 rename the word doc with the name mentioned in the body of the document How to specify Document Title from the body of my Document SymphonyTomorrow Word 9 08-05-2013 05:31 PM
How to rename the word doc with the name mentioned in the body of the document Rename Document & Save d4okeefe Word VBA 4 05-23-2013 09:35 AM
How to rename the word doc with the name mentioned in the body of the document How to import Word Style Headings and body text into Excel antztaylor Excel 5 11-08-2012 09:54 PM
How to rename the word doc with the name mentioned in the body of the document Advancing Paragraph Numbers in Merge Document Body Wyskers Mail Merge 1 11-30-2011 04:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:13 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft