Results 1 to 11 of 11
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Insert File (Word 2000)

    I want to insert a file at the end of an existing paragraph. In fact, I'm inserting a range of the specified file. I know that the text of the existing paragraph (but not the paragraph mark) is enclosed in a bookmark. An example:

    [This is the text of the paragraph.]

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Insert File (Word 2000)

    A look at your code would be helpful...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Insert File (Word 2000)

    Thanks, Kevin. Here's the code:

    Sub test()
    Dim sFile As String, sRangeToInsert As String
    sFile = "c:MyFilesTest Document.doc"
    sRangeToInsert = "insert"
    Dim rng As Range
    Set rng = ActiveDocument.Bookmarks("test").Range
    If rng.Characters.Last = vbCr Then
    rng.End = rng.End - 1
    End If
    rng.Select
    Selection.Collapse wdCollapseEnd
    Selection.InsertFile sFile, sRangeToInsert
    End Sub

    This is what I did yesterday, and it worked fine. Today it no longer works consistently. For testing purposes, I have a file called "test document.doc" in the MyFiles folder. Within that file is a couple lines of text which is in bookmark "insert". That bookmark does not contain a pargraph mark.

    I am inserting that range into my current document, which contains bookmark "test." That bookmarks is around a paragraph, and the end - 1 line of code will shrink the range if by chance it includes a paragraph mark at the end.

    For testing purposes, this works perfectly. When I substitue the actual document and bookmark names, it no longer inserts the text within the paragraph, but in a NEW paragraph. If I copy and paste the actual bookmark into the test document, it works as expected. The actual document has the bookmarked text in a table, to simplify maintenance. (Three similar blocks of text are side by side in three columns to facilitate editing.) I added a table to my test document and put the bookmarked text in that. It works fine, so I can assume that the table is not the problem.

    The problem is so inconsistent that it's difficult to solve. As I said yesterday, it works on my laptop 100% of the time. On my desktop where I'm actually doing the work, it functions correctly at one point and I think I've got it nailed. A few hours later, it no longer works as expected.

    Perhaps there's a better way to insert the range of the file. I've tried adding a new document and inserting the text into that, copying the text, closing the temporary document, and returning to my "real" document. I can then use range.InsertAfter, and that works consistently well. But this will need to be done 20 or 30 times in the course of this application, and it looks messy and doesn't seem to me to be the most efficient way of approaching this.

    I'd appreciate any help you can offer.

    Thanks,
    Richard

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Insert File (Word 2000)

    Don't collapse the selection object. It looks like you want to keep the paragraph mark if one exists in the TEST bookmark. Then you select the bookmark's range (which now excludes the paragraph mark). If you collapse the selection object, then why select it in the line above?

    You want the bookmark in your Test Document to replace the selected range in the Activedocument, right? If so, leave off the collapse.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Insert File (Word 2000)

    Yes, I do want the existing paragraph to remain, but I want to insert the range of the file in front of it. No, I don't want to replace the existing text.

    If I did not change the bookmark from a range to a selection, the inserted text replaced the range, even if I had reduced to to a single character. Selecting the range and then collapsing the selection left me with the insertion point where I wanted it. The smallest range is apparently a character, while a selection can be reduced to the insertion point. I suppose I could have begun by selecting the bookmark, "shrinking" it to exclude the paragraph at the end (if there was one) and then collapsing the selection. Is there a better way to get to that particular location? (The end of the bookmark, but excluding the paragraph mark if it was within the bookmark.)

    The problem is that the inserted range brings a paragraph mark with it even if the range does not include a para mark.... sometimes. The documentation for InsertFile says that the inserted text will be inserted into a separate paragraph, but that's speaking of an inserted file, not a range of a file. The documentation is completely silent on what is intended when a range is inserted using InsertFile. It performs one way (inserting as a new paragraph) sometimes, and at other times the other way (inserting the range only, without add a new paragraph). Obviously I have to plan on the first scenario (adding a paragraph), even though it's not consistent. And of course that's not what I want. The difference which I seem to see is the size of the file which contains the range. If it's a small file, the range is inserted without adding a paragraph. After the file grows to a certain size, the range from that file seems to cause a new paragraph to be added. This is pretty consistent, but it doesn't make a lot of sense to me.... none, in fact.

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Insert File (Word 2000)

    Richard,

    Not sure why you're getting the inconsistent result with InsertFile (I would have expected it not to add a paragraph mark).

    Anyway, you should be able to collapse the range without resorting to selection. Rather than:

    If rng.Characters.Last = vbCr Then
    rng.End = rng.End - 1
    End If
    rng.Select
    Selection.Collapse wdCollapseEnd

    Try :

    If rng.Characters.Last = vbCr Then
    rng.End = rng.End - 1
    rng.Start = rng.End
    End If

    Gary

  7. #7
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Insert File (Word 2000)

    Thanks, Gary. That's so obvious: rng.Start = rng.End. That works fine.

    The real problem, with the added paragraph, remains. If I put the same text, within the same bookmark in a one or two page document, the range inserts without adding a paragraph. But when I insert it from the larger, actual document, a new paragraph is added. It's so frustrating!

    I'm guessing that the documentation is correct: An inserted file adds a paragraph unless it's being inserted at the very end of the document, just before the final paragraph mark. The fact that the range inserts without a paragraph mark sometimes is apparently an aberration. When I worked out this method in the first place, I was working with a small file, and I got the results I wanted and moved forward. All of a sudden, things did not work as expected, and the only difference I can see is the increase in the size of the document where I store the text that needs to be inserted. Perhaps this is just coincidence. There are 30 or 40 bookmarked ranges of text that may get used, depending upon choices made by the user. Most are, in fact, separate paragraphs, but in several cases one paragraph might have several possibilities for the concluding sentence. That's why I need to insert a range of text without adding a paragraph.

    I've got a couple workarounds in mind, but it would be best if I could use range.FileInsert as I had planned. Any other insight?

    Thanks,
    Richard

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Insert File (Word 2000)

    Richard,

    Come think of it, "rng.Collapse" is even simpler.

    As far as the extra paragraph getting added: in an earlier incarnation of our templates/data environment at work, we used to store a lot of bookmarked content in Word files and bring it into new documents via InsertFile>bookmarked range.

    I've just gone back and looked at one of these data docs; it's 24 pages long and in almost all cases the bookmarked text ends just before the paragraph mark. We never saw the problem you're reporting of the paragraph mark coming along with the bookmarked content. Would it make any difference if you inserted some kind of artifical barrier (such as some tabs) after the end of the bookmarked range, but before the paragraph mark?

    I guess the only other thing I can think of is: if it does seem to be affected by the size of the file, perhaps you can break it down into several smaller files - a pain, I know...

    Gary

  9. #9
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Insert File (Word 2000)

    Hi Gary,

    Thanks for your response to this and to my more recent post about the Index in the Paragraphs Collection.

    This has turned into a real frustrating thing. Inserting a range of a file has proven to be very inconsistent. It's not the size of the file as I once thought. I'm not sure what it is. I've rebuilt the file, and it worked fine for a while. Then -- with nothing changing -- the inserted range added a new paragraph. It does not "bring" the paragraph from the source file; a new paragraph is added in the file where I insert. Sometimes the range is inserted where I expect it to be (just before the bookmark), but other times it's outside the bookmark. I have dual monitors, and run in the code on one screen and watch what happens in Word on the other. It' just not consistent.

    I came up with a solution: I add two guillmets (<<) at the beginning of a paragraph which needs to be "pushed back" into the preceeding paragraph. At the end of assembling the document, I search for ^p<< and replace with nothing. Even that doesn't work so well. If I manullly delete a paragraph and the two following characters, the second paragraph takes on the formatting (heading level in this case) of the first. In other words, the second paragraphs is "absorbed" into the first. This is as expected. But when I used the find and replace code, the first paragraph sort of gets "pulled into" the second.... taking on its formatting. As a clause to be appended, they were just in normal style. I have found that all of those clauses need to be formatted with the same hading level as the paragraph to which they might be appended. Then I can search for ^p<< and replace with nothing and things work out fine. I first need to search for ^p^p<< because sometimes Word throws in an extra, empty paragraph.

    I was looking for a way to determine the index of a paragraph because I had an idea that seemed like it would work if I could determine that, for example, paragraph 17 was still paragraph 17 and not 18. I thought that paragraphs.count might help, but because an extra paragraph sometimes finds its way into the mix, that wasn't an accurate test.

    Thanks again for your help. If you can think of anything new to add, I'd be happy to hear it.

    Richard

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Insert File (Word 2000)

    Another grasp at straws, perhaps: are your target bookmarks in the recipient document empty or "wrapped" (i.e. bookmarks surrounding something like a space)? If the former, it might be worth trying it with the latter.

  11. #11
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Insert File (Word 2000)

    They are indeed "non-empty" bookmarks. The recipient paragraph would have just been inserted from the source file, and the bookmarks would be in place around the paragraph, including the para mark. I have up to three more clauses that might be needed to finish out the paragraph.

    RB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •