Save the document with a name found in this doc.

philoche63 Posted messages 15 Status Membre -  
C-Claire Posted messages 4562 Registration date   Status Membre Last intervention   -
Hello,
Yet another problem with a macro:
With the valuable help of C-Claire, I have a macro that allows me to split a Word file based on page breaks. Here is this macro:

Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage

For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")

'Select and copy the text to the clipboard.
ActiveDocument.Bookmarks("\page").Range.Copy

' Open new document to paste the content of the clipboard into.
Documents.Add
ligne = Cells(4, 4)
Selection.Paste
' Removes the break that is copied at the end of the page, if any.
Selection.TypeBackspace
ChangeFileOpenDirectory "F:\PDF CREATOR\"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.Close

' Move the selection to the next page in the document.
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub



Now, I would like the generated Word file to be named after the person I find on the 26th line of the document. This line appears as follows:
"Article 1er : La situation de Madame Francois MARTIN, "

How can I save my document in the form of "MARTIN Francois"?

Is it possible in VBA for Word?

Thank you very much in advance for your help!
Have a great day

Philippe

Configuration: Windows 7 / Internet Explorer 10.0

4 réponses

philoche63 Posted messages 15 Status Membre
 
Thank you for your response, but it doesn't seem to be working because I am working on the result of the merge and therefore I no longer have my merge fields.

I used the macro recorder, and I saw the generated code, but how do I specify when I am on the line where the first name and last name are located that the first name is between the 7th and 8th space and that the last name is between the 8th and 9th space?

Thank you again for your help.
0
C-Claire Posted messages 4562 Registration date   Status Membre Last intervention   2 250
 
You are right, I forgot that the merge was finished.

You can use the following instruction, which selects the 8th word of the line and copies it
Selection.MoveRight Unit:=wdWord, Count:=7, Extend:=wdExtend
Selection.Copy
You modify the Count value for the next word.

Other possibly better options: use bookmarks in your template document so that the first name and last name are identified, allowing you to refer to them in the merge result and thus in the SaveAs instruction, or use the document properties and input the first name and last name there.

C-Claire
0
philoche63 Posted messages 15 Status Membre > C-Claire Posted messages 4562 Registration date   Status Membre Last intervention  
 
As I am completely clueless about macros, I still need your insights!
How can I make it understood that I want to put the 7th word into a variable 'NomAgent'?

We used the macro recorder to arrive at the line where the person's name is located (these are the lines that start with Selection.
Here's the code:
Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage

For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")

'Select and copy the text to the clipboard.
ActiveDocument.Bookmarks("\page").Range.Copy

' Open new document to paste the content of the clipboard into.
Documents.Add
Selection.PasteAndFormat (wdFormatOriginalFormatting)
' Removes the break that is copied at the end of the page, if any.
Selection.MoveDown Unit:=wdLine, Count:=25
Selection.MoveRight Unit:=wdCharacter, Count:=23
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=16, Extend:=wdExtend
Selection.Copy
Selection.TypeBackspace
ChangeFileOpenDirectory "F:\PDF CREATOR\"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.Close

' Move the selection to the next page in the document.
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub


Regarding bookmarks, I don't really believe in it since the merge document is handled by our payroll software for generating this mail merge, but I will inquire for next time.

A big thank you for your help.

See you later!
0
C-Claire Posted messages 4562 Registration date   Status Membre Last intervention   2 250 > philoche63 Posted messages 15 Status Membre
 
We will do something cleaner and shorter by using the position of the word instead of selecting it by moving the cursor.
You said the person is on the 26th line. If your lines = paragraphs, then you indicate (26) in the instruction below. If they are lines, count the paragraphs.

FirstName = ActiveDocument.Paragraphs(26).Range.Words(8)
LastName = ActiveDocument.Paragraphs(26).Range.Words(9)
ActiveDocument.SaveAs FileName:= LastName & " " & FirstName & ".docx"

The first two instructions store the last name and first name.

C-Claire
0
philoche63 Posted messages 15 Status Membre > C-Claire Posted messages 4562 Registration date   Status Membre Last intervention  
 
It's true that this solution is simpler (by the way, I almost understood everything ;-) )
But this time it’s stuck and I have the message:
"the required member of the collection does not exist"
What do you say?
Thanks for your help
See ya+
0
C-Claire Posted messages 4562 Registration date   Status Membre Last intervention   2 250 > philoche63 Posted messages 15 Status Membre
 
I'm sorry, as I am working on several files outside the forum, I did it quickly.
You need to declare the variables. Under the "Sub" add
Dim FirstName
Dim LastName

C-Claire
0
philoche63 Posted messages 15 Status Membre
 
With your help, I'm very close to the goal!
I have recounted the paragraphs, and the line where the person's name is located is on the 21st line.
For the first time, the macro saves with the person's name, but not on the split file, but on the mail merge.

I’m giving you the code again since I’m a novice, I might not have placed the code in the right spot!

Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage

For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")

'Select and copy the text to the clipboard.
ActiveDocument.Bookmarks("\page").Range.Copy

' Open new document to paste the content of the clipboard into.
Documents.Add
Selection.PasteAndFormat (wdFormatOriginalFormatting)
' Removes the break that is copied at the end of the page, if any.
Selection.TypeBackspace
ChangeFileOpenDirectory "F:\PDF CREATOR\"
'DocNum = DocNum + 1
Prenom = ActiveDocument.Paragraphs(21).Range.Words(7)
Nom = ActiveDocument.Paragraphs(21).Range.Words(8)
ActiveDocument.SaveAs FileName:=Nom & " " & Prenom & ".doc"

' ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
ActiveDocument.Close

' Move the selection to the next page in the document.
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub



A big thank you for your help and your patience!
See you later
Philippe
0
philoche63 Posted messages 15 Status Membre
 
look no further, it's all set!
A HUGE HUGE THANK YOU for your help!
see you!
Philippe
0
C-Claire Posted messages 4562 Registration date   Status Membre Last intervention   2 250
 
I wasn't looking anymore... I was waiting for you to find it and give us the answer ;-)

C-Claire, curious...
0
C-Claire Posted messages 4562 Registration date   Status Membre Last intervention   2 250
 
Hello Philippe, and thanks, but I just sent you to Microsoft :-)

If the first name and last name are in 2 merge fields, you should be able to retrieve their value using .DataFields.

This could look something like:

FirstName = ActiveDocument.MailMerge.DataSource.DataFields(1).Value
LastName = ActiveDocument.MailMerge.DataSource.DataFields(2).Value

ActiveDocument.SaveAs FileName:=LastName & " " & FirstName & ".doc"

(1) and (2) are the field order numbers in your data file.
If you're using Word 2007 or later, you can save in .docx format.

I'm not a VBA expert. There will likely be other suggestions.

--
C-Claire
-1