Renaming an entire folders contents using VBA

dscott21

New Member
Joined
Oct 11, 2011
Messages
7
Hi guys, really hope someone can help with this problem as it's driving me insane!

I currently have a VBA code that will open a TIFF file and then copy all the text detected within the TIFF file into a standard text file which is done using OCR as it's a TIFF image file.

This works great but need to take it one step further but I'm struggling at the moment.

Within the TIFF file there is a 6 digit reference number that is contained within the first 2 sentances of the text. I need VBA to rename the filename of the TIFF file with this 6 digit reference number, and then repeat the process for every TIFF file within the folder and also the sub-folders.

Does anyone have any ideas how I can change the code below to do what I need it to do?

Hope you can help, you're my only hope!

Many thanks in advance,
Dave

Here's the code I'm using but please be aware I could only get it to work by selecting 'Microsoft Office Document Imaging 11.0 Type Library' from Tools>References Menu in Excel VBA and I don't think this option is available in any of the more recent versions of Excel (i'm using Excel 2003)

Really hope you can help, I'm so close to getting this nailed.


Sub RenameTIFF()

Dim doc1 As MODI.Document
Dim inputFile As String
Dim strRecText As String
Dim imageCounter As Integer

inputFile = Application.GetOpenFilename
strRecText = ""
Set doc1 = New MODI.Document
doc1.Create (inputFile)
doc1.OCR
For imageCounter = 0 To (doc1.Images.Count - 1)
strRecText = strRecText & doc1.Images(imageCounter).Layout.Text
Next
fnum = FreeFile()
Open "C:\TEMP\testmodi.txt" For Output As fnum
Print #fnum, strRecText
Close #fnum
doc1.Close

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Are you able to pick up the 6 digit reference number using code?
 
Upvote 0
Are you able to pick up the 6 digit reference number using code?

No. I'm able to pick out the entire text within the TIFF file using the above code, but can't figure out how to pick out just the 6 digit reference number contained within the first 2 sentances of the text.
 
Upvote 0
Can you show us a clear example of the text file output?
 
Upvote 0
Can you show us a clear example of the text file output?

All the TIFF files are scanned letters which are all the same format (which helps!).

The letters go something like this:

Reference No: 123456

Dear Sir / Madam,

We are writing to you in connection with ..........

Yours Sincerley......

Does that help?
 
Upvote 0
Hello

The output aways starts with: "Reference No:" ?

To rename a file, you could use the "Name ... As ..." command in VBA. You can look up the details in the help files. If you can't get it to work, please post an update in this topic.
 
Upvote 0
Hello

The output aways starts with: "Reference No:" ?

To rename a file, you could use the "Name ... As ..." command in VBA. You can look up the details in the help files. If you can't get it to work, please post an update in this topic.

Yes, most of them start with: "Reference No:", however I'm not sure how to get the 'strRecText' to pickup the 6 digit reference number rather than the entire text content of the TIFF file.
 
Upvote 0
Hello

Give this a try, I cannot test it since I have not all references and files.

Code:
Sub RenameTIFF()

    Dim doc1 As MODI.Document
    Dim inputFile As String
    Dim strRecText As String
    Dim imageCounter As Integer
    Dim s6DigitNumber As String, v6DigitNumber As Variant
    
    inputFile = Application.GetOpenFilename
    strRecText = ""
    Set doc1 = New MODI.Document
    doc1.Create (inputFile)
    doc1.OCR
    v6DigitNumber = Split(Trim(doc1.Images(0).Layout.Text))
    s6DigitNumber = v6DigitNumber(UBound(v6DigitNumber))
    For imageCounter = 0 To (doc1.Images.Count - 1)
        strRecText = strRecText & Trim(doc1.Images(imageCounter).Layout.Text)
    Next
    fnum = FreeFile()
    Open "C:\TEMP\testmodi.txt" For Output As fnum
    Print #fnum, strRecText
    Close #fnum
    doc1.Close
    MsgBox s6DigitNumber

End Sub

What is picked up from the file, is everything after the last space on the first line of the file.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top