PDA

View Full Version : Solved: Identifying Selected Paragraph



MWE
02-08-2006, 09:06 PM
I have a document that identifies "requirements" for a new product. Approximately 75% of the individual paragraphs are background and explanatory text, the remaining 25% are requirement paragraphs, i.e., the new product will have "this functionality" with appropriate tolerance, capability, reliability, etc. words

I wish to "identify" each of these rqmt paras with a unique alphanumeric code, e.g., ABCD001. To make things easier to see, I wish to have this code in the "left margin" for any relevant paragraph. Further I wish to insert a bookmark at each rqmt para and use the rqmt # as the bookmark "name".

Ideally, the process would work as follows:



each rqmt para is manually identified with, say, a special character or two.
a procedure would be run that would sequence through the document, identify rqmt paras, add the relevant rqmt # in the left margin (initially incrementing the rqmt # as it goes along) and add the relevant bookmark
a 2nd proc would be used to "reexamine" the document in the future and would add rqmt #s and bookmarks to any recently added rqmt paras. But I do not want to "renumber" the document. Once a rqmt # is assigned to a specific para, it stays there. If the para is subsequently deleted, the rqmt # is abandoned and never used again. So, the first thing this 2nd macro does is find the last rqmt # so it know what the next one will be.
I can probably cobble up some code to do this, but I am no great VBA wizard with Word. So if anyone has some ideas about approaches or spefic code examples, I would appreciate feedback. Whatever I do has to run in Word2000 and newer versions.

Thanks

fumei
02-08-2006, 10:37 PM
Forget about the number in the margin. Simply make each requirement paragraph a bookmark in itself. You can still USE the numbers for names.

So select the paragraph you wanted to number ABCD001. Now go Insert > Bookmark and give the name ABCD001.

Now anytime you want to get that paragraph you can:

Select it:ActiveDocument.Bookmarks("ABCD001").Select

Copy it to another location. In this example, to another new document:ActiveDocument.Bookmarks("ABCD001").Copy
Documents.Add
Selection.Paste

Replace it with new text:ActiveDocument.Bookmarks("ABCD001").Range.Text = _
"Blah blah new text"

Although, please note there are some issues with replacing the text of bookmarks. There is simple code that makes sure the replacement text retains the bookmark.

You can have a macro that easily identifies what the bookmark name is.Sub WhichBookmark()
MsgBox Selection.Bookmarks(1).Name
End Sub
Putting the cursor in the bookmark ABCD001 would display a msgbox "ABCD001".

fumei
02-09-2006, 12:26 AM
Further more, you could use this system of bookmarks to compile new documents based on them.

Say you determined that you needed ABCD001, EFGH001, EFGH002, IJKL002 and MNOP004 copied to a new document. Or maybe even better, say you have a new document, and want to be able to identify requirement paragraphs (in the source document), grab those specific paragraphs and copy them to your new document. Let's assume the source document - the one with the requirement paragraphs is named "source.doc". Attached is a ZIP file with two documents.

source.doc - with some paragraphs bookmarked. There are four - you can make as many as you like. They are bookmarked as "abcd", "efgh", "ijkl", and "mnop".

getbookmarks.doc - this is an empty document, but with a Userform and some code.

First of all, unzip the files into some folder. Now go into getbookmarks.doc and the VBE. Find the code module MyCode. Find the procedure Get_EM(). Change:

"C:\Documents and Settings\Gerry\My Documents\source.doc"

to whatever folder you have unzip source.doc into. You are ready to test.

The shortcut key is Alt-G. This fires a procedure which checks to see if source.doc is open - if it isn't, it opens it. It then fires open the userform which loads all bookmarks - the ones corresponding to the whole paragraphs - into a dropdown combobox. Select one or more - or all four if you like - of the bookmarks. You add each one with the Add button. When you are done, click Done.

This will then copy each of the identified bookmarks (paragraphs) into the document GetBookmarks.

See? Bookmarks are fun. Bookmarks work.

fumei
02-09-2006, 12:37 AM
Further again....

To ALL, feel free to adapt this, but please do not submit this - or a variant of - to the KB....I will submit this myself shortly.

I use this as an alternative to AutoText. I have a document with many, many bookmarked paragraphs, images. Actually, a few document with many bookmarks. The code that retrieves is in one of my global templates so it is accessible anywhere.

I can grab identified chunks of text (or images) to use in multiple documents. Variations can identify if the selected bookmark ranges are to go into headers or footers. Note that bookmarks can be nested within other bookmarks - but they function independently.

As the source document is simply a document it is very easily edited. I find this much easier and more direct than editing AutoText entries. Unlike AutoText entries, you can go into the source document and easily change text, insert new text INTO a bookmark chunk etc. etc. Plus of course you can make adjustments to styles along the way.

MWE
02-09-2006, 05:55 AM
Forget about the number in the margin. Simply make each requirement paragraph a bookmark in itself. You can still USE the numbers for names.

So select the paragraph you wanted to number ABCD001. Now go Insert > Bookmark and give the name ABCD001.

Now anytime you want to get that paragraph you can:

Select it:ActiveDocument.Bookmarks("ABCD001").Select

Copy it to another location. In this example, to another new document:ActiveDocument.Bookmarks("ABCD001").Copy
Documents.Add
Selection.Paste

Replace it with new text:ActiveDocument.Bookmarks("ABCD001").Range.Text = _
"Blah blah new text"

Although, please note there are some issues with replacing the text of bookmarks. There is simple code that makes sure the replacement text retains the bookmark.

You can have a macro that easily identifies what the bookmark name is.Sub WhichBookmark()
MsgBox Selection.Bookmarks(1).Name
End Sub
Putting the cursor in the bookmark ABCD001 would display a msgbox "ABCD001".
thanks for your reply.

The rqmt #s MUST be very obvious and very visible to the reader (who may be viewing the document electronically or in paper form). Placing them in the left margin is one solution (and quite appealing); there might be others (like unique font, special character sets, etc.) The bookmarks are to "connect" specific paragraphs to other programs. I have used bookmarks a lot in the past (including most of what you outlined) and am quite comfortable with what they can do (and their limitations). I was looking for a more streamlined approach to what I wish to do that was less manually intensive initially.

MWE
02-09-2006, 06:22 AM
Further more, you could use this system of bookmarks to compile new documents based on them.

Say you determined that you needed ABCD001, EFGH001, EFGH002, IJKL002 and MNOP004 copied to a new document. Or maybe even better, say you have a new document, and want to be able to identify requirement paragraphs (in the source document), grab those specific paragraphs and copy them to your new document. Let's assume the source document - the one with the requirement paragraphs is named "source.doc". Attached is a ZIP file with two documents.

source.doc - with some paragraphs bookmarked. There are four - you can make as many as you like. They are bookmarked as "abcd", "efgh", "ijkl", and "mnop".

getbookmarks.doc - this is an empty document, but with a Userform and some code.

First of all, unzip the files into some folder. Now go into getbookmarks.doc and the VBE. Find the code module MyCode. Find the procedure Get_EM(). Change:

"C:\Documents and Settings\Gerry\My Documents\source.doc"

to whatever folder you have unzip source.doc into. You are ready to test.

The shortcut key is Alt-G. This fires a procedure which checks to see if source.doc is open - if it isn't, it opens it. It then fires open the userform which loads all bookmarks - the ones corresponding to the whole paragraphs - into a dropdown combobox. Select one or more - or all four if you like - of the bookmarks. You add each one with the Add button. When you are done, click Done.

This will then copy each of the identified bookmarks (paragraphs) into the document GetBookmarks.

See? Bookmarks are fun. Bookmarks work.
Thanks again for the additional information. I am sure that some of your code will be useful in my application.

I agree that bookmarks work very well inside the Word environment and are extremely valuable when you want to quickly move around inside a document, create links to other documents, etc. External to Word there are some problems. I have found that bookmarks are inconsistently handled by other MS applications trying to "use" them as specific targets inside the target document (at least in Office2000). For example, if you are trying to MANUALLY create a hyperlink out of Excel into a Word document and specifically to a bookmark in that word document, Excel is unable to examine the bookmark structure in the target doc and allow you to pick the target bookmark. There is a button to do so, i.e., the "BookMark" button, but when you try to use it you get an error message that "Excel could not open this file or could not parse a file of this type." As I recollect, this worked in Office97. However, if you know the bookmark name and just manually add #bookmarkname to the hyperlink address, it works fine. I can not remember what problems, if any, I have had hyperlinking to bookmarks from, say, PowerPoint, or MSProject.

fumei
02-09-2006, 11:15 PM
OK, I reviewed your original post. You are STILL identifying requirement paras by "special characters", that are manually inserted. Hmmmm.

OK. So what exactly is your problem? It would be easy enough to make the paragraphs in a two column table, with the numbers in the left column.

Of course then you have all the tables issues.....

a procedure would be run that would sequence through the document, identify rqmt paras, add the relevant rqmt # in the left margin (initially incrementing the rqmt # as it goes along) and add the relevant bookmarkMay I ask...WHAT relevant bookmark? Especially since you seem to have problems with bookmarks and other applications.

What exactly is the process here? WHAT are you trying to do for the users. It would be easy enough to have the requirement numbers visible to the user. However, WHY is that important? Is the user going to use those numbers? If so...how? What is the actual point of the numbers?

You further mention having a bookmark "at" the paragraph. I see, but not OF the paragraph. You just want to mark the start of the paragraph, but not the paragraph itself? If so, again...WHY? What exactly does the end product look/act like?

MWE
02-10-2006, 07:06 AM
Thanks for your reply. The answer to all of your questions are in my original post, but let me try again by answering your specific questions.


OK, I reviewed your original post. You are STILL identifying requirement paras by "special characters", that are manually inserted. Hmmmm. The target word document is a Requirements Documents for product design (as defined by, say, FDA, ISO, etc). Document control is paper with electronic backup.

Most of the requirements document is background. However, some of the paragraphs contain specific requirements or specifications that must be uniquely identified with a visible ID, e.g., ABC### so that these requirements can be specifically and uniquely referenced in the future (same idea as a drawing number or document number). Only the user knows what paragraphs are requirements and what are background, so the user must identify them.


OK. So what exactly is your problem? It would be easy enough to make the paragraphs in a two column table, with the numbers in the left column.

Of course then you have all the tables issues.....I have considered tables to achieve the visibility I want, but that is awkward. There are lots of (other) tables, figures, etc., and handling them in Word without overarching tables is hard enough.

As indicated in previous posts, there are lots of ways to high light the IDs. Putting them in the left margin makes them really stand out. That's important because I want the rqmt paras to be easy to find and for there to be no ambiguity that they are formal requirements (no confusion between background info and real requirements).


May I ask...WHAT relevant bookmark? Especially since you seem to have problems with bookmarks and other applications.my original text:

a procedure would be run that would sequence through the document, identify rqmt paras, add the relevant rqmt # in the left margin (initially incrementing the rqmt # as it goes along) and add the relevant bookmark

Perhaps "insert the relevant bookmark" would have been clearer.

I suspect that I am not the only one who has problems with, say, hyperlinking from Excel to bookmarks in Word. But that is not an issue here as I plan to do everything with VBA. Perhaps my real point was suggesting that bookmarks are not quite the solution to all problems of the universe.


You further mention having a bookmark "at" the paragraph. I see, but not OF the paragraph. You just want to mark the start of the paragraph, but not the paragraph itself? If so, again...WHY? What exactly does the end product look/act like?You are right, it should be "of the paragraph". I wish to have each rqmt para identified visually (so readers can identify them easily) and in some way that makes it easier to find them with, say, a VBA appl. That appl may be word-based or some other appl. Bookmarks make the process of other appls finding target paras easier.

What exactly is the process here? WHAT are you trying to do for the users. It would be easy enough to have the requirement numbers visible to the user. However, WHY is that important? Is the user going to use those numbers? If so...how? What is the actual point of the numbers?
Already answered.

fumei
02-10-2006, 12:38 PM
Already answered.Perhaps you think so...but I do not.

Let's say....all requirement paragraphs are identified. The number is in the margin - although you do not state how you are going to put those in the "margin". Any new ones do the increment thing exactly as you want. My question was - now what? What happens next? What is the pirpose of this?

I do not, in any way, feel that is answered. However, you clearly know what you are doing, so I guess I will leave this alone.
I wish to have each rqmt para identified visually (so readers can identify them easily) and in some way that makes it easier to find them with, say, a VBA appl.This has two parts. Easy identification of the paragraph (can be done). "that makes it easier to find them with a VBA appl". This is clearly nonsense, sorry. VBA does not "see" anything. The fact that something is visibly noticeable in the document is irrelevant to VBA. What makes it easy for VBA is if something has a name. A whole paragraph bookmarked by name - whether visible to the user or not - is accesible to VBA.

Anyway, again, say the numbers ARE visible, the paragraphs ARE bookmarked...I asked what next, what is it for? Apparently I am missing something and the answer is there.

So I will let that go, and just ask two last question. How are you getting the numbers into the margin? And are the users going through the document deciding which paragraphs are to be numbered as requirements? it seems that way. In which case, perhaps unfortunately as the Selection object has its weaknesses, all your code must use Selection. So let me summarize.

1. the user works their way through the document looking for requirement paragraphs.

2. they find one. they put the selection into so it cam ebe identified by the code.

3. the code checks back for the LAST requirement number in the margin.

4. increments that number.

5. expands the cover the paragraph.

6. makes a bookmark of that paragraph.

7. puts the incremented number in the margin beside that paragraph.

Is this correct?

OK...done. I say again, and please excuse my dumbness, as I am simply missing the answer that apparently is there, what now?

fumei
02-10-2006, 12:54 PM
Since you are putting (I think) the Selection into a paragraph - the one the user is going to identify as a requirement - and making it a bookmark (I think), then you will want to expand the selection so that it IS the whole paragraph.Selection.Expand unit:=wdParagraph