+ Reply to Thread
Results 1 to 15 of 15

Vba Excel to word; deleting all text from last bookmark to end of document

  1. #1
    Forum Contributor
    Join Date
    01-18-2012
    Location
    nr Salisbury, UK
    MS-Off Ver
    Excel 2010
    Posts
    126

    Vba Excel to word; deleting all text from last bookmark to end of document

    This is driving me insane, from Excel I have opened an existing Word Doc, it contains 4 tables and from AFTER the last Bookmark
    to the end of the document there is an unknown amount of text.

    I want to deleted Table 1 but leave 2,3 &4 then delete all the unknown amount of text that is after the last Bookmark . I am struggling
    with the part of; identifying/selecting from last bookmark to end of the document.

    I have spent hours reading and trying different things all with different results, but ultimately none have worked.

    As a bit of a side question; just how different is vba for Excel than vba for Word and what are the limitations of controlling Word from Excel, is it just a case of terminology.



    Please Login or Register  to view this content.

    If you can help me regain my sanity I would really appreciate it.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Try something like this...


    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    01-18-2012
    Location
    nr Salisbury, UK
    MS-Off Ver
    Excel 2010
    Posts
    126

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    AlphaFrog; thanks so much for that it worked exactly as I wanted, my sanity is restored!

    Experimented with what you gave me, plus some additions and in various situations; found that this

    Please Login or Register  to view this content.
    will start from 5 characters after the bookmark, but couldn’t come up with solution to start FROM say line 2 after it.
    Of course everything I tried using Word vba failed.

    Can you suggest anything?
    Many thanks.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Maybe something like this...

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    01-18-2012
    Location
    nr Salisbury, UK
    MS-Off Ver
    Excel 2010
    Posts
    126

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Afraid not this time, it is selecting/high lighting the first line in the bookmark and everything else to the end of the document.

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Remove the highlighted wdDoc maybe
    Please Login or Register  to view this content.
    多么想要告诉你 我好喜欢你

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Quote Originally Posted by julhs View Post
    but couldn’t come up with solution to start FROM say line 2 after it.

    It may depend on how you define "Line 2"
    Are the "Lines" paragraphs? If yes, try this...

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 02-17-2014 at 09:44 PM.

  8. #8
    Forum Contributor
    Join Date
    01-18-2012
    Location
    nr Salisbury, UK
    MS-Off Ver
    Excel 2010
    Posts
    126

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Still the same with regards the selected/highlighted range.

    “Lines” could be; just paragraph symbols; one line of text or a full paragraph

    Not sure if at all relevant but it is counting from the top of the document and not from IN the Bookmark that it was pasted to.
    Needless to say nothing is deleted, tried simply substituting “Select” with “Delete”, that just through a 5904 error.

    Been wondering if problem lay with the copy&paste, and that it is not actually a Table, but simply a range of cells pasted in
    from Excel, keeping the source formatting.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Quote Originally Posted by julhs View Post
    Still the same with regards the selected/highlighted range.

    “Lines” could be; just paragraph symbols; one line of text or a full paragraph

    Not sure if at all relevant but it is counting from the top of the document and not from IN the Bookmark that it was pasted to.
    Needless to say nothing is deleted, tried simply substituting “Select” with “Delete”, that just through a 5904 error.

    Been wondering if problem lay with the copy&paste, and that it is not actually a Table, but simply a range of cells pasted in
    from Excel, keeping the source formatting.
    It works for my dummy Word doc. Can you post an example doc. ?

    I used .Select for testing instead of having to re-type new lines in the Word doc. The .Delete and .Select should be interchangeable once the Range is workng.
    Last edited by AlphaFrog; 02-18-2014 at 01:19 PM.

  10. #10
    Forum Contributor
    Join Date
    01-18-2012
    Location
    nr Salisbury, UK
    MS-Off Ver
    Excel 2010
    Posts
    126

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    I have lost track a little as to what errors there were, when and under what situation,
    I am at the point of just not being able to see the wood for the trees.
    So thought it prudent to upload the Job Book as well, chances are you will spot the problem immediately.
    Any mention of “Invoice” just ignore it, is simply left over stuff not yet edited out.

    Many thanks

    julhs
    Attached Files Attached Files
    Last edited by julhs; 02-19-2014 at 04:23 PM. Reason: Replace .xlsm file with one that works

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Bookmarks are numbered in the order they were created and not in the order they appear in the Doc. You created WorkSheet_Template_insert_point as the 4th bookmark. Insert_point_4 is the 3rd bookmark created.

    In the code, you could use the bookmark name instead of the bookmark index number e.g.
    Please Login or Register  to view this content.
    Each paragrah is the number of lines (empty ones as well) below the Bookmark.

    I would think it would be easier to create a "Notes" bookmark at the line just below the Notes header and then use something like this...
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    01-18-2012
    Location
    nr Salisbury, UK
    MS-Off Ver
    Excel 2010
    Posts
    126

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    It is late over here and I have completely lost my focus, a fresh head for the morning is what’s required.
    Get your point about using names as opposed to numbers, would certainly be easier to follow. However I do not understand about the significance of the numbering; (which I gave them) and order in which they were created because when I use the “Go To” button on the Bookmark tab they go to the correct location.
    Will pick this up again tomorrow.
    Many thanks

  13. #13
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Quote Originally Posted by julhs View Post
    “Lines” could be; just paragraph symbols; one line of text or a full paragraph
    Word doesn't really work with 'lines', which can vary from one PC to the next, depending on the OS version, Office version, what printer driver is active, and so on. Word works with paragraphs, tables, bookmarks, sections, etc. and can also work with manual line breaks.

    From looking at your sample document and your Excel code, two fundamental flaws are immediately apparent:
    1. You are simply re-using an existing document instead of creating a new one from a template (in Word, a .dotx file). If you were to create a new document from a Word template, you need have none of the problem tables to delete!
    2. The way you're populating the document results in the content being inserted after the named bookmarks, instead of within their ranges. That's not entirely your fault, as some circumlocution is required to get the bookmarked range to expand to encompass what has been inserted. If you were to expand the bookmark ranges, you could then erase their content in preparation for whatever might be inserted. But, if you were using a template that started off without any tables, that's a non-issue.

    From your code comments, it seems you actually want to delete everything from 'Notes' after the last table to the end of the document. That's quite easily achieved, though I don't know why you'd have the notes there if all you're going to do with them is to delete them before anyone gets to look at the document - unless, of course, they're from the last job, but that too wouldn't be an issue if you were using a template...

    So, the solution to all these problems is: use a template.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  14. #14
    Forum Contributor
    Join Date
    01-18-2012
    Location
    nr Salisbury, UK
    MS-Off Ver
    Excel 2010
    Posts
    126

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    AlpgaFrog; I have ironed out as to why your snippets were not working for me. Turned out it was an editing error,
    I had made so many alterations while testing, had ended up with 2 lines of code in the wrong order!!!
    Your suggestion of setting an additional BookMark for the “Notes” was the final part of the puzzle.

    Micripod; I can now see that you your suggestion makes sense and the problem was of my own making by doing it the
    way I have. I am glad I persevered with AlphaFrog help and got there in the end; who knows I may still have to go
    back to the drawing board and start again, if so the .dotx file route will be explored more fully

    Have replaced the previously Uploaded file (duff version) to a working version for those who come along in future.

    Many thanks indeed for your help and patience


    Final draft

    Please Login or Register  to view this content.

  15. #15
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Vba Excel to word; deleting all text from last bookmark to end of document

    Kind of like the tail wagging the dog - a mass of unnecessary code for its own sake, because all you're doing is addressing the symptoms not the cause.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Add text to Existing Word Document if it Doesn't exist create Word Document
    By unstable81 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-10-2013, 12:15 AM
  2. add additional data to word document's bookmark in excel
    By RonNCmale in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2011, 04:14 PM
  3. importing text from bookmark in Word to excel
    By mayanklal in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2006, 10:23 AM
  4. hyperlink from Excel to a Word bookmark
    By Bertie in forum Excel General
    Replies: 2
    Last Post: 05-16-2006, 02:55 PM
  5. inputting text on Word document using excel
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2005, 10:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1