FaheemAhmadGul
asked on
Help writing a macro that will cut out all the paragraphs from a main document past them one by one into seprate files
I have a document (called say C:\Main.doc ) which has several paragraphs. The first two words of the paragraph (which are always on the first line of the paragraph) are the first and the last names of a person about whom there is some information in the rest of the paragraph. I have a folder (C:\Important Document ) which contains separate files for all the those persons about whom there may be a paragraph in the Main.doc.
I would like to have a macro that will select the first two words of the first paragraph in the Main document. The macro should then check the folder (C:\Important Documents) to see if there is a file corresponding to the name of that person (there will always be such a file in that folder). On finding this file, the macro will open it, then the macro will select the whole of the first paragraph from Main.doc, cut it and past it into the open document. It will then save and close this document and then Select the first two words of the next paragraph (which was originally the 2nd paragraph but due to cutting and pasting in the previous step would now have become the first paragraph in the Main document. After selecting the first two letters the macro will repeat the process it did with the first paragraph. It will keep on doing this until all paragraphs from the main document have been cut out one by one and pasted into their respective files in the folder (C:\Important Documents\
To clarify this problem further as an example say my folder (C:\Important Documents) contains three files named Phillip Kaplan.doc John Martin.doc and Kim Slater.doc
Assuming that my Main.doc has three paragraphs as shown below. When the Macro runs it will first select the first paragraph and past it into the document called Phillip Kaplan.doc it will then cut 2nd Para and past it into John Martin.doc and lastly it will cut the 3rd Paragraph and paste it into Kim Slater.doc (the actual main Main.doc could have a lot more paragraphs like 30-40, and the actual total number of paragraphs in the Main.doc will vary from day to do, so the macro should basically keep on looping until it can find no paragraph in the Main.doc)
Contents of the Hypothetical Main.doc before the marco run will be as follows:
Phillip Kaplan:
Date of Visit: 18 June 2008
FBC: Leucocytosis.
Enzymes: Raised
John Martin:
Date of Visit: 18 June 2008
FBC: Leucocytosis.
Enzymes: Raised
Kim Slater:
Date of Visit: 18 June 2008
FBC: Leucocytosis.
Enzymes: Raised
And the Main.doc will be empty after the macro has run.
Many thanks in anticipation.
I would like to have a macro that will select the first two words of the first paragraph in the Main document. The macro should then check the folder (C:\Important Documents) to see if there is a file corresponding to the name of that person (there will always be such a file in that folder). On finding this file, the macro will open it, then the macro will select the whole of the first paragraph from Main.doc, cut it and past it into the open document. It will then save and close this document and then Select the first two words of the next paragraph (which was originally the 2nd paragraph but due to cutting and pasting in the previous step would now have become the first paragraph in the Main document. After selecting the first two letters the macro will repeat the process it did with the first paragraph. It will keep on doing this until all paragraphs from the main document have been cut out one by one and pasted into their respective files in the folder (C:\Important Documents\
To clarify this problem further as an example say my folder (C:\Important Documents) contains three files named Phillip Kaplan.doc John Martin.doc and Kim Slater.doc
Assuming that my Main.doc has three paragraphs as shown below. When the Macro runs it will first select the first paragraph and past it into the document called Phillip Kaplan.doc it will then cut 2nd Para and past it into John Martin.doc and lastly it will cut the 3rd Paragraph and paste it into Kim Slater.doc (the actual main Main.doc could have a lot more paragraphs like 30-40, and the actual total number of paragraphs in the Main.doc will vary from day to do, so the macro should basically keep on looping until it can find no paragraph in the Main.doc)
Contents of the Hypothetical Main.doc before the marco run will be as follows:
Phillip Kaplan:
Date of Visit: 18 June 2008
FBC: Leucocytosis.
Enzymes: Raised
John Martin:
Date of Visit: 18 June 2008
FBC: Leucocytosis.
Enzymes: Raised
Kim Slater:
Date of Visit: 18 June 2008
FBC: Leucocytosis.
Enzymes: Raised
And the Main.doc will be empty after the macro has run.
Many thanks in anticipation.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I can't explain that.
I did have problems when I copied the sample text that you gave to the main document, because all lines are teminated with newline characters. This meant that I had one big paragraph. When I changed the empty lines to a paragraph marks it worked OK.
The paragraphs are appended to the end of the individual documents.
I did have problems when I copied the sample text that you gave to the main document, because all lines are teminated with newline characters. This meant that I had one big paragraph. When I changed the empty lines to a paragraph marks it worked OK.
The paragraphs are appended to the end of the individual documents.
ASKER
My apologies. Your solution is actually working perfectly. The only thing is it does not work if there are any stray double paragraph marks anywhere in the document. If there are such paragraph marks it returns an error message (Requested members of the collection does not exist. Run-Time Error 5941).
What I have done is created a macro that removes all the stray paragraph marks from the document before I run the code you have provided.
I am now becoming a little addicted to your code it works like magic. I get the same pleasure from reading it, and seeing it working, as I get from reading a beautiful piece of poetry.
If you could spare a little more time, could you explain the syntax of line No 9 in your code. This would be just to help my learning.
9. strName = Trim$(para.Range.words(1). Text & para.Range.words(2).Text)
I can guess that it assigns the first two words of the paragraph to the variable strName but I do not know what Trim$ is telling or doing here , in particular what does the sign $ indicates here.
Many thanks again for your kind help. I am greatly indebted.
What I have done is created a macro that removes all the stray paragraph marks from the document before I run the code you have provided.
I am now becoming a little addicted to your code it works like magic. I get the same pleasure from reading it, and seeing it working, as I get from reading a beautiful piece of poetry.
If you could spare a little more time, could you explain the syntax of line No 9 in your code. This would be just to help my learning.
9. strName = Trim$(para.Range.words(1).
I can guess that it assigns the first two words of the paragraph to the variable strName but I do not know what Trim$ is telling or doing here , in particular what does the sign $ indicates here.
Many thanks again for your kind help. I am greatly indebted.
ASKER
My apologies. Your solution is actually working perfectly. The only thing is it does not work if there are any stray double paragraph marks anywhere in the document. If there are such paragraph marks it returns an error message (Requested members of the collection does not exist. Run-Time Error 5941).
What I have done is created a macro that removes all the stray paragraph marks from the document before I run the code you have provided.
I am now becoming a little addicted to your code it works like magic. I get the same pleasure from reading it, and seeing it working, as I get from reading a beautiful piece of poetry.
If you could spare a little more time, could you explain the syntax of line No 9 in your code. This would be just to help my learning.
9. strName = Trim$(para.Range.words(1). Text & para.Range.words(2).Text)
I can guess that it assigns the first two words of the paragraph to the variable strName but I do not know what Trim$ is telling or doing here , in particular what does the sign $ indicates here.
Many thanks again for your kind help. I am greatly indebted.
What I have done is created a macro that removes all the stray paragraph marks from the document before I run the code you have provided.
I am now becoming a little addicted to your code it works like magic. I get the same pleasure from reading it, and seeing it working, as I get from reading a beautiful piece of poetry.
If you could spare a little more time, could you explain the syntax of line No 9 in your code. This would be just to help my learning.
9. strName = Trim$(para.Range.words(1).
I can guess that it assigns the first two words of the paragraph to the variable strName but I do not know what Trim$ is telling or doing here , in particular what does the sign $ indicates here.
Many thanks again for your kind help. I am greatly indebted.
Thank you for those kind words.
When Word divides a range into a Words collection, it will append any delimiting trailing blank to the end of the range. That is why we can concatenate the two words of the name and expect that they are still separated by the space at the end of the first word. If there were a blank after the surname, the file would not be found, so we use the Trim$() function, which removes space characters from the beginning and end of a string.
The dollar is mainly historical, but still functional. Variables and functions can have a type character as a suffix and the $ is used for strings. % is used for integers an ! indicates a Single
So
Dim mystring$
has the same meaning as
Dim mystring as String
and
Function MyFunction$() can be used to return a string.
When there is no type indication, a Dim or a Function takes the variant type, where the actual type is decided at run time. Built-in string functions have a variant version, so Trim() will do the same as Trim$(), but will take a little more processing time to find out that it will have to return a string. The same applies to Left$(), Right$(), Mid$() etc.
The extra time is no longer considered important with today's CPU speeds and so VB.Net only has the variant version - but I started with VB1 and old habits die hard. I also think that the original purpose of indicating the type of the variable or function is still valid, but has largely been superseded by the use of prefixes (strMyString, etc) in the case of variables.
When Word divides a range into a Words collection, it will append any delimiting trailing blank to the end of the range. That is why we can concatenate the two words of the name and expect that they are still separated by the space at the end of the first word. If there were a blank after the surname, the file would not be found, so we use the Trim$() function, which removes space characters from the beginning and end of a string.
The dollar is mainly historical, but still functional. Variables and functions can have a type character as a suffix and the $ is used for strings. % is used for integers an ! indicates a Single
So
Dim mystring$
has the same meaning as
Dim mystring as String
and
Function MyFunction$() can be used to return a string.
When there is no type indication, a Dim or a Function takes the variant type, where the actual type is decided at run time. Built-in string functions have a variant version, so Trim() will do the same as Trim$(), but will take a little more processing time to find out that it will have to return a string. The same applies to Left$(), Right$(), Mid$() etc.
The extra time is no longer considered important with today's CPU speeds and so VB.Net only has the variant version - but I started with VB1 and old habits die hard. I also think that the original purpose of indicating the type of the variable or function is still valid, but has largely been superseded by the use of prefixes (strMyString, etc) in the case of variables.
ASKER
Thank you very much for the detailed explanation. It is much appreciated. I can now fully understand line 9. Regards. Faheem
ASKER