PDA

View Full Version : Solved: Help to dividie mail merge files and save separately.



zebradoby
05-21-2008, 03:50 PM
I have this code to divide the huge word file (50 pages) into 50 files of 1 pages each. I want to save the file on a pre defined folder with a pre defined name. Can anyone please help?

To use code:
Open word.
Copy code
Click Run
Select a file with at least 4 pages (word file)

I want to save the file as "Loan Number.date.cor.doc" . The Loan Number is part of document on each page.

Let me know if anyone can help. Please see code below. If you want to help by writing an easier code please disregard my code.

'Macro recorded/Edited 10/07/07
Sub FileOpenTest()
Dim FileToOpen
Dim FileToSave

Dim szFileName As String
Dim wrdDoc As Document
'''This is just to demonstrate the below library function.
szFileName = FileOpenCustom1("", "*.Doc")

'''In reality it would be more productive to pass a document object back.
If szFileName <> "" Then
Set wrdDoc = Word.Documents(szFileName)
' disabled
' MsgBox wrdDoc.Path + Chr$(13) + wrdDoc.Name

FileToOpen = wrdDoc.Name

'Modified oct 6 2007
'Parse out .txt and replace with .doc
FileToSave = Left(wrdDoc.Name, Len(wrdDoc.Name) - 4) & ".doc"

Dim varDocOne As Variant
Dim j As Long
Dim r As Range
Dim thisdoc As Document
Dim Thatdoc As Document
Dim var

Documents.Open FileName:=FileToOpen, ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto

Set thisdoc = Activedocument

Dim S As Section
Dim Loan As Range
Dim TempNo As Long
Dim FileSuffix As String
Dim FilePrefix As String
FilePrefix = "C:\"
For Each S In thisdoc.Sections
Set Loan = S.Range.Duplicate
If Loan.Find.Execute(FindText:="Mortgage Loan Number:") Then
Loan.MoveWhile " " & vbTab & vbTab & vbTab & vbTab
Loan.MoveEndUntil Chr(13)
FileSuffix = Loan.Text
Else
TempNo = TempNo + 1
FileSuffix = "Temp" & TempNo
End If
S.Range.Copy
With Documents.Add
.Range.PasteAndFormat (wdFormatOriginalFormatting)
.Sections(1).Range.Characters.Last.Delete
If Dir(FilePrefix & FileSuffix & ".doc") <> "" Then
TempNo = TempNo + 1
FileSuffix = "Dup" & TempNo & " " & FileSuffix
End If
.SaveAs FilePrefix & FileSuffix & "." & Format(Date, "mmddyyyy") & "." & "cor" & ".doc", wdFormatDocument
.Close
End With
Next

Activedocument.Close savechanges:=wdDoNotSaveChanges

'Message displayed on completing the macro
MsgBox "Files have been saved at the specified location."


End If

End Sub

''' Function: FileOpenCustom


Function FileOpenCustom1(pszInitDir As String, pszFilter As String) As String
Dim dlgFileOpen As Word.Dialog
Dim szCurDir As String
Dim szdlgFullName As String
Dim wrdDoc As Word.Document
'''Store the current document path
szCurDir = Application.Options.DefaultFilePath(wdDocumentsPath)
'''If pszInitDir is empty use the default path.


If pszInitDir <> "" Then
ChDir pszInitDir
Else
pszInitDir = szCurDir
End If
'''Initailize and reference the dialog object.
Set dlgFileOpen = Word.Dialogs(wdDialogFileOpen)
With dlgFileOpen
'''Update the dialog object.
.Update
'''Set the filter
.Name = pszFilter
'''Display and execute the dialog.
If .Show() <> False Then
'''If the user didn't cancel...
'''The active document is the one just opened.
Set wrdDoc = Activedocument
'''Cheat and use the document object to do the parsing.
'''Return the name of the document.
FileOpenCustom1 = wrdDoc.Name
End If
End With
'''Restore the default path setting.
With Application.Options
If .DefaultFilePath(wdDocumentsPath) <> szCurDir Then
.DefaultFilePath(wdDocumentsPath) = szCurDir
End If
End With
End Function

gwkenny
05-21-2008, 06:34 PM
Honestly, I have not read your code at all.

My question is what do you really want? You have a problem and you have code. Does the code run correctly? If so, what do you want? Critique on making your code 'tighter'?

If your code does not run correctly, then you should upload a support file as you indicate that the individual files you want to save has a loan number which comes from the document you are performing the operation on. You should also indicate where or what is happening that is incorrect.

Good luck to you!

zebradoby
05-21-2008, 09:35 PM
This is the file i want to divide. I am attaching the file.

zebradoby
05-21-2008, 09:45 PM
I have attached the file that i want to divide.

zebradoby
05-21-2008, 09:53 PM
When i try running this from a button on word, i get an error message on this line "FilePrefix & FileSuffix & "." & Format(Date, "mmddyyyy") & "." & "cor" & ".doc", wdFormatDocument"...can you please help.

gwkenny
05-21-2008, 10:34 PM
I took out the file open stuff. You need to open the huge Word file and then run the following code:

Sub FileOpenTest()
Dim FileToOpen
Dim FileToSave
'Dim szFileName As String
'Dim wrdDoc As Document
''''This is just to demonstrate the below library function.
'szFileName = FileOpenCustom1("", "*.Doc")
'
''''In reality it would be more productive to pass a document object back.
'If szFileName <> "" Then
'Set wrdDoc = Word.Documents(szFileName)
'' disabled
'' MsgBox wrdDoc.Path + Chr$(13) + wrdDoc.Name
'
'FileToOpen = wrdDoc.Name
'
''Modified oct 6 2007
''Parse out .txt and replace with .doc
'FileToSave = Left(wrdDoc.Name, Len(wrdDoc.Name) - 4) & ".doc"
Dim varDocOne As Variant
Dim j As Long
Dim r As Range
Dim thisdoc As Document
Dim Thatdoc As Document
Dim var
'Documents.Open FileName:=FileToOpen, ConfirmConversions:=False, ReadOnly:= _
'False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
'"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
'Format:=wdOpenFormatAuto
Set thisdoc = ActiveDocument
Dim S As Section
Dim Loan As Range
Dim TempNo As Long
Dim FileSuffix As String
Dim FilePrefix As String
FilePrefix = "C:\"
For Each S In thisdoc.Sections
Set Loan = S.Range.Duplicate
If Loan.Find.Execute(FindText:="Mortgage Loan Number:") Then
Loan.MoveWhile " " & vbTab & vbTab & vbTab & vbTab
Loan.MoveEndUntil Chr(13)
FileSuffix = Loan.Text
Else
TempNo = TempNo + 1
FileSuffix = "Temp" & TempNo
End If
S.Range.Copy
With Documents.Add
.Range.PasteAndFormat (wdFormatOriginalFormatting)
.Sections(1).Range.Characters.Last.Delete
If Dir(FilePrefix & FileSuffix & ".doc") <> "" Then
TempNo = TempNo + 1
FileSuffix = "Dup" & TempNo & " " & FileSuffix
End If
.SaveAs FilePrefix & FileSuffix & "." & Format(Date, "mmddyyyy") & "." & "cor" & ".doc", wdFormatDocument
.Close
End With
Next
'ActiveDocument.Close savechanges:=wdDoNotSaveChanges
'Message displayed on completing the macro
MsgBox "Files have been saved at the specified location."
End Sub


I didn't bother blocking the code cause the sample you uploaded had no code. Just the sample file so I got the code from your first post. This worked for me, though be aware that in the sample file you gave, the mortgage loan number was the same so it kept overwriting with the same file name. If I changed the loan number, then I would get different files.

This code is hard to read because it is not formatted and it looks like it was cobbled together from different sources to do different things.

Good luck!

zebradoby
05-22-2008, 08:00 AM
They click this button on top. There is a pop up to select document to be divided. And then the divided files are saved with different names at pre defined location. How do I do that ?

My set of letters to be divided are saved as Test.doc. The macro is saved on file Test4.doc

When i run my code though, it saves it on 2 pages instead of 1, what should i do?

zebradoby
05-22-2008, 08:07 AM
the file is attached. the file ,


Test4.doc -- code from g. i donot get a pop to select file to be divided.


test5.doc -- my code--file saved on 2 pages instead of 1.

zebradoby
05-22-2008, 08:08 AM
Test5.doc attached. this is how i want. but file saved on 2 pages instead of 1 when divided. can anyone help????

gwkenny
05-23-2008, 04:18 AM
Just a very quick reply.

1) You state Test4.doc is code from me. Ummm, let me be REAL CLEAR:

THIS IS NOT MY CODE.

lol. I would never write something like this. I took the code that you submitted and took out the bit that was screwing things up (which was the whole file open routine) so it works.

2) I stated this worked if you opened the large file to be divided yourself and then run the code I cited. If you follow this procedure, it DOES work.

3) The way you presented the problem it seemed like this was your code.

My current understanding is that this is not your code. You are just looking for someone to write something you want that works and not really interested in understanding why.

It seems you are not interested in understanding why things work, so if I have the time and inclination I'll return and rewrite the whole code as I get satisfaction helping others learn (and learning myself) rather than doing someone else's work for them just because they need it done (without any renumeration). Though I do that sometimes when I have the time and inclination as I stated previously.

Good luck to you!

zebradoby
06-12-2008, 12:24 PM
Hi g-

i resolved this..THANKS for your help.Really appreciated.

s~

gwkenny
06-14-2008, 05:01 AM
Glad you got what you needed!

:)