PDA

View Full Version : Solved: File saveas a word from the file as name - Urgent



svirk
11-20-2007, 05:29 AM
Hi

I have lots of word files and i want to save those files in a different folder but with a name coming from that file.

1. Read file, when word "reference: XXX" is found.( there will be only one occurance of this word in the document and will be probably in third row)
2. Save this file as XXX.doc (this XXX will be numbers)
3. Repeat this process untill all the files are saved as with new XXX.doc
in another folder.

Thanks
Svirk

fumei
11-20-2007, 08:55 AM
Using the word "urgent" does not actually help.

What have you tried? Have you tried using the macro recorder?

If ALL the files in the folder have "reference: XXX", then you can use the Dir function on that folder.

If only some of the files do, then you could use TextOrProperty of Application.FileSearch to go through the folder and only open the files containing that string.

Once the file is opened, it is fairly easy to search for the string, extract the "XXX", and use SaveAs (with that as the filename) to a different folder.

lwildernorva
11-20-2007, 09:01 AM
I use this macro to save a document by its assigned file number. To make this macro work, I created a special Word paragraph style, VWC File No., that I only assign to the line that contains the file number. It also helps that our file numbers are in a standard format: XXX-XX-XX. If yours are not, you'll have to vary the method of calculating the portion of the file name that constitutes the number. This macro also puts the word, "Order," at the end of the file name. This macro also would need to be modified to take into account your need to address multiple documents stored in a folder rather than one active document open in Word.
Sub SaveOrder()
Dim ThisDoc As Document
Dim oPara As Word.Paragraph
Dim rngParagraph As Range
Set ThisDoc = ActiveDocument
For Each oPara In ThisDoc.Paragraphs

If oPara.Style = "VWC File No." Then
Set rngParagraph = oPara.Range
With rngParagraph
rngParagraph.End = rngParagraph.End - 1
rngParagraph.start = rngParagraph.start + 17
End With
End If
Next
ThisDoc.SaveAs FileName:="G:\LEW Work\" & rngParagraph & " Order"
ThisDoc.Close
Set rngParagraph = Nothing
Set ThisDoc = Nothing
End Sub

fumei
11-20-2007, 12:28 PM
Assumption for the following code:

1. Folder to get the files from = c:\yadda_A
2. Folder to put the new files = c:\yadda_B
3. the XXX in "reference: XXX" are different in each file.
Sub ToSomePlace()
Dim r As Range
Dim file
Dim StartPlace As String
Dim NewPlace As String
Dim NewFileNum As String
StartPlace = "c:\yadda_A\"
NewPlace = "c:\yadda_B\"

file = Dir(StartPlace & "*.doc")
Do While file <> ""
Documents.Open StartPlace & file
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.Text = "reference: "
.Execute
If .Found = True Then
r.Collapse Direction:=wdCollapseEnd
r.MoveEnd Unit:=wdCharacter, Count:=3
NewFileNum = r.Text
ActiveDocument.SaveAs _
FileName:=NewPlace & NewFileNum & ".doc"
End If
End With
ActiveDocument.Close wdDoNotSaveChanges
file = Dir
Loop
End SubWhat this does.

1. sets the starting folder as a string
2. sets the target folder as a string
3. sets the Dir function to action all .doc files in the starting folder
4. opens first doc file
5. sets a Range for the document
6. searches the Range for "reference: "
7. if found, collapses the range to the end
8. makes the range the next three characters (ie. the XXX)
9. sets a string variable to be those characters, the XXX
10. does a SaveAs using the target folder, the XXX and .doc
11. closes the file (without saving, as it IS saved)
12. opens the next file in the Dir

Result?

In c:\yadda_A, there are four files:

doc1.doc - containing "reference: 123"
doc2.doc - containing "reference: 456"
doc3.doc - containing "reference: 789"
doc4.doc - containing "reference: 514"

c:\yadda_B will have four files created:

123.doc
456.doc
789.doc
514.doc

Again, if ALL the files in the source folder have the "reference: XXX", then Dir would be efficient.

If some of the files have "reference: XXX", then FileSearch could be more efficient, as you could restrict the files opened to just the ones with the search string.

It also depends on whether XXX is the same number, or different numbers. I am assuming different numbers.

svirk
11-20-2007, 08:07 PM
Spot on Fumai, worked lika charm!!!!!

Thanks Heaps.