PDA

View Full Version : set all text after bookmark (but on same line) as a string variable



adme22
05-02-2012, 03:01 PM
so I have basically created a macro that parses text in from multiple word files into one based on bookmarks. The problem is that I need to isolate only the first line of one of those docs and set it as a string (i dont care about extra spaces, i can trim it later). My initial thought is to insert the file into the master document at its relevant bookmark (which provides the text i need as the only text on that line). I have gotten that far, but must now isolate only the first line, this particular source file i inserted has 3 lines... For what its worth, its also the first line of text in the master document (although there are some spaces and headers and other stuff above it). Is there an easy way to do this in VBA?

Thanks much in advance and please let me know if you need any clarification.

fumei
05-02-2012, 03:48 PM
"I need to isolate only the first line of one of those docs |

Do you mean first paragraph? If you mean first paragraph...
Dim MyString As String
MyString = ActiveDocument.Paragraph(1)
The string variable MyString is now the text of the first paragraph.

adme22
05-03-2012, 07:25 AM
thanks for the response! unfortunately that returns a blank string ("") for me. I think it's because there are blank lines above the text? The template i'm attempting to extract info from and modify look like this:

Header

blank
blank
blank
blank

[Name
Adress
Adress2


the "[" is a bookmark and the full adress (all 3 lines) is located in a different word file (i have the insert working already).

I need to isolate just the Name portion above as its own string. Is there another method other than your example above?

thanks!

Frosty
05-03-2012, 09:01 AM
Try

Sub Tester
dim sString As String
sString = ActiveDocument.Bookmarks("myBookmark").Range.Paragraphs(1).Range.Text
msgbox sString
End Sub

You will need to replace "myBookmark" with the name of that bookmark that is around all three lines of the full address.

Now, if your address is a single paragraph (the "lines" are created by soft-returns rather than paragraph marks), you may need to address it differently.

And, since Fumei will say it, I'll beat him to the punch: don't use blank paragraphs for the purposes of formatting (creating space). Use formatting (space before, space after, etc)

adme22
05-03-2012, 09:49 AM
excellent! that code works for me.

Do you mind elaborating on the "the "lines" are created by soft-returns rather than paragraph marks"? or possibly a link explaining the concept

those are the first 3 lines of the source file so i think i lucked out in that regard. You are correct that in creating the template I just hit enter several times and put in the bookmark. is there some hidden character i should be putting in all blank lines in a template?

also, one more thing: the bookmark is wrapped around the text? it was my understanding that a bookmark is a single point that you can insert something after.


I usually only work in excel so creating a somewhat complex word macro has been a learning experience .

fumei
05-03-2012, 11:21 AM
And, since Fumei will say it...Yes I most certainly would have.


You are correct that in creating the template I just hit enter several times and put in the bookmark. is there some hidden character i should be putting in all blank lines in a template?Properly done, you should NEVER have "blank" lines, at all. As Frosty mentions, if you want space...make space (using space before or space after). NOT "blank" paragraphs. If you use Styles properly, having space in a document is very very easy.



also, one more thing: the bookmark is wrapped around the text? it was my understanding that a bookmark is a single point that you can insert something after.
You understand incorrectly. Bookmarks are containers. They can hold text, be it a single space, multiple paragraphs, a table, or no characters at all. This is the power of bookmarks. If you want to get the contents of the container (the bookmark) it is an easy single instruction. Plus, you can manipulate the contents directly.

So yes, the bookmark is "wrapped around the text". However, it is best to think of them as containers. It is better to think of the text being INSIDE the bookmark.

When you say soft returns...are you sure that is what they are? "Soft" returns use Shift-Enter. If they are "blank" lines (paragraphs) made by pressing Enter, then they are NOT soft.

BTW: the reason I use quotations mark - "blank" lines - is that they appear empty, but in fact if it is a paragraph mark (with no text), the paragraph mark itself contains a HUGE amount of infomation. So they are not (especially from a VBA point of view) really "blank".

Frosty
05-03-2012, 02:48 PM
I would adjust one part of fumei's explanation, but only in that I would say bookmarks are a range, rather than a container. Bookmarks in Word are analogous to a named range in Excel. That range can contain text or not, in much the same way your Selection can be an insertion point or whatever characters (or paragraphs or more) you have selected. But container vs. range is only semantics... whichever helps you understand the concept is what is "more" right.

It gets a little tricky if you start adding things like graphics... but for now we can keep it simple.

fumei
05-03-2012, 06:33 PM
Frosty is quite correct. Bookmarks are ranges, and are equivalent to named ranges in Excel. Shrug. They ARE exactly that...named ranges.

Conceptually though I always taught them as being containers. The difference being that in Excel you are dealing with discreet chunks (ummmm, containers...that is cells) - you can not have a half a cell. Thus an extension, or dimension so to speak, of cells is a "range" of cells, like a mountain range.

Word is not made up of discreet chunks like cells, but rather numerical points; the Start and End points of ranges.

These are just words bandied about, somewhat in jest. Bookmarks are named ranges. Well they are ranges, with names.

See? Containers!