Solved

Help writing a macro that will cut out all the paragraphs from a main document past them one by one into seprate files

Posted on 2008-06-23
7
336 Views
Last Modified: 2010-04-30
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.
0
Comment
Question by:FaheemAhmadGul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 21850587
This doesn't actually delete the paragraphs from the main document. It runs through the Paragraphs collection instead.
Sub CopyParagraphs()
    Dim docData As Document
    Dim docIndividual As Document
    Dim para As Paragraph
    Dim strName As String
    
    Set docData = ActiveDocument
    For Each para In docData.Paragraphs
        strName = Trim$(para.Range.words(1).Text & para.Range.words(2).Text)
        If Dir$("C:\Important Documents" & "\" & strName & ".doc") = "" Then
            MsgBox "C:\Important Documents" & "\" & strName & ".doc not found"
            Exit Sub
        End If
        Set docIndividual = Documents.Open("C:\Important Documents" & "\" & strName & ".doc")
        para.Range.Copy
        docIndividual.Bookmarks("\EndOfDoc").Range.Paste
        docIndividual.Close True
    Next para
End Sub

Open in new window

0
 

Author Comment

by:FaheemAhmadGul
ID: 21851134
Thanks for the prompt response to my request. There seems to be some problem. When I run the macro it seems to be opening and closing different files from (C:\Important Documents) folder but after the macro has run its course and I open the individual files I cannot find in them the paragraphs I would have expected the macro to haved copied into them.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21851315
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:FaheemAhmadGul
ID: 31469945
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.
0
 

Author Comment

by:FaheemAhmadGul
ID: 21860378
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21861172
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.



0
 

Author Comment

by:FaheemAhmadGul
ID: 21861320
Thank you very much for the detailed explanation. It is much appreciated. I can now fully understand line 9. Regards. Faheem
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question