Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 38 of 38

Thread: inserting page and info

  1. #21
    yes once the page has been reinserted then autotext is to be inserted on that page e.g.
    ActiveDocument.AttachedTemplate.AutoTextEntries("List of Tables").Insert Where:=Selection.Range, RichText:=True

  2. #22
    I have a
    -7 page Word template of which
    -2 pages are fixed; and
    -5 pages that need to be either removed or reinserted into the document depending on user selection
    I also have a userform with a frame containing 5 checkboxes.
    Each checkbox represents a page in the word template. e.g. checkbox number 1 is theTable of Contents.
    When the user unchecks one of these boxes i need to be able to remove the selected page from the word document template.
    When the user checks one of the boxes I need to be able to insert a page and then load the autotext information.
    If the page to be reinstated is old page 3 then i need it to insert this page back as page 3.

    Does anyone have a recommendation for doing this?

  3. #23
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    This is quite difficult to do. You are asking about a dynamic structure regarding "pages". Unfortunately, as Steve (lucas) mentioned, "pages" are not really things in Word. Oh I know we think they are, but they are not.

    Let me explain. You state for example:

    "If the page to be reinstated is old page 3 then i need it to insert this page back as page 3."

    You also stated: “I have a frame with 5 check boxes representing the 5 pages that i need to insert into my document. If the chkbox value =0 then it is to delete the selected page otherwise it is to insert the selected page”

    Except…there is no selected page. The 5 checkbox do NOT represent – and can never truly represent – the pages you think they do.

    The reality is this. There is no page 3. It is very possible that what you see as page 3 on your computer would NOT be page 3 on a different machine. That is how Word works. It is 100% structured by the current printer driver. I have personally seen the same document have a different page structure printed out by two people, even printing to the same printer - because one machine had an older printer driver than the other.

    OK, that covers the theory. Can you do what you seem to be asking. Yes, it is possible...maybe. However, you need to answer some questions.

    1. Are you working from a .DOT file, a template?

    2. Are these dynamic changes (page 3 IN, page 3 OUT...oooops, page 3 in again) common? I ask because if it is not, then you may want to reconsider trying to do this. Because of the nature of Word itself, getting this to be truly robust and error-free is going to take some effort. Is this really worth your effort? Admittedly, if you do get this working, you will have vastly increased your knowledge of both Word, and VBA. So I am not trying to discourage you, in fact I am encouraging you. This is a good project, but it is NOT a trivial one.

    3. Is this just you doing this, or is this for other people?

    4. If it is for other people – and you do say “users” – then what are you going to do about the fact you are using NormalTemplate.AutoTextEntries? Can you guarantee that the other person’s Normal.DOT file will have those AutoText entries? Because if they do not, then your autotext entry code will fail.

    This direct affects something you state:

    “so basically it is a checkbox with the ability to remove and replace the page in the document.”

    No, that is incorrect. The real logic is:

    The value of the checkbox determines if specific content (your AutoText entry) is in the document at a specific location, with that location being structured with a page break before, and a page break after, with logic determining if those page breaks are required, or not.

    In other words, if the location you which to have content (your AutoText entry) already has a page break, then you do NOT want a new page break, but if it does not, then you DO want a page break.

    I am sorry, I know this sounds so complicated. It seems so simple to ask – take page X out if I want, put page X back in if I want. Again, unfortunately (perhaps) this is just not the way Word works.

    5. I have concerns about you mentioning removing Table of Contents. Hmmm. What are you doing about if it is removed, and then replaced? I am assuming it is a real ToC, that is, a generated one. What are you doing about regenerating the field? Is it possible the ToC could (ever) go beyond one page. I know this is unlikely as it is a 7 page document, but…would it ever be possible?

    6. Please confirm that when the userform opens, ALL “pages” are in fact in the document. That is, you always start with a full document. In which case, do you have ALL checkboxes showing checked to start? Thus indicating that their associated “page” is, in fact, in the document. This ties in with my comment that the checkbox do NOT “represent” the pages. They do in your mind, true. But for the fact of coding, you must make them – logically – acts as IF they represent specific content at a specific location.

    7. What, exactly, causes your Sub - Sub InsertPage(iNum As Integer) – to fire?

    8. WHAT would make a user go oooops? They uncheck Page 3 on the userform…and…do what? Click a button to perform the actual action. Is that what fires Sub InsertPage(iNum As Integer)? Assumable the button checks the values of all the checkboxes and then does whatever it is supposed to do.

    OK, the userform is still up and visible, and the user have (somehow) removed Page 3. WHAT would make them go oooops, I want Page 3 back?

    OR, are you talking about the user has done something – remove Page 3 – and closed the userform, and realizes…ooooops…opens the userform again, and checks Page 3.

    In which case, go back and look at my #6. It is no longer valid. The userform now has to check to see if Page X is in the document (or not), and THEN make the checkbox checked (or not).

    The reason I say this is, say they removed Page 3, and the the userform is opened. If the checkbox for “page 3” is unchecked, then OK, checking it should put “page 3” in the document.

    Let me side-track for a second. You make a statement about “back”:

    “If the page to be reinstated is old page 3 then i need it to insert this page back as page 3.”

    Get this idea out of your head. There is NO “back”. There is no “Page 3”. There is specific content at a specific location. Word has ZERO knowledge about “old page 3”, once it has been removed. “old Page 3” is only in your mind.

    9. Are you using any bookmarks at all in the document? The reason I ask is this:

    Say you have the full 7 pages. You remove “page 3”. OK, guess what? What once “was” Page 6…is NOT page 6 anymore, is it? It is “page 5”. Therefore any code logic using actual numbers:[vba]
    Selection.GoTo what:=wdGoToPage, _
    which:=wdGoToAbsolute, Count:=Mycounter
    [/vba]could possibly fail. Say you pass a MyCounter value of 6, but you have already removed “page 3” and “page 5”.. Hmmmm, that means there are only 5 pages in the document now…hmmmm, a value of 6 will fail.

    10. which brings me to:
    [vba]
    iPages = ActiveDocument.ActiveWindow.Panes(1).Pages.Count
    For Mycounter = iPages To 1 Step -1
    [/vba]which does sort of cover the number issue I raised in the point above. But why are you doing this stepping? I am not quite following the logic here.

    BTW_1: what version of Word are you using??? The line:
    [vba]
    iPages = ActiveDocument.ActiveWindow.Panes(1).Pages.Count
    [/vba] fails for me. This is not valid syntax; Pages is NOT a valid member of Panes. Are you saying that this line works for you? Are you using Option Explicit? How did you get to put Pages as a member of Panes? In fact, if you search Object Browser…there IS no Pages as a member of anything, except a MultiPage control. I suspect you are using 2007, which – IMO – fakes the idea of page objects.

    BTW_2: I strongly suggest you use a Document object when coding. It makes it much clearer, and easier, to code pointing to a specific object (rather than ActiveDocument).

    OK, I know I have been going on here, so I will stop. I am in fact playing around with trying to do what you are trying to do. Again, this is not trivial. I encourage to persist though. It is a –perhaps – a worthwhile project. The perhaps depends on whether this is actually going to be used enough to make it worthwhile.

    What makes it difficult is your requirement to be able to remove and replace specific content at specific locations. The removal is fairly straightforward, but the replacing part is tricky.

    What I am having difficulties with is, say you UNcheck page 3, and page 5. So:

    page 1 = checked
    page 2 = checked
    page 3 = UNchecked
    page 4 = checked
    page 5 = UNchecked

    OK...fire the removal code.

    1
    2
    3
    4
    5
    6
    7

    now becomes:

    1
    2
    3 (was page 4)
    4 (was page 6)
    5 (was page 7)

    Putting page "back" in is not easy.

    Oh...I forgot. You mention 2 pages are "fixed". HOW, precisely, are you making that happen???? And what does that exactly mean? I can understand that the contents - actual text - remains. But that is not "fixed", it simply means you do not have code actions to remove it.

    Say you say "page 4" is "fixed". Well it certainly is NOT fixed as a "page". If you remove "page 3", then what was "page 4" is now "page 3"...whether you like it or not, whether you have "fixed" it or not.

    So I am not clear on what you mean by "fixed".

    I just want to reiterate that I think it is a good learning project, and I encourage you to keep going. I will help as much as I can, but the truth is...I have not been to do it robustly - error free - myself yet. I am working on though.
    Last edited by fumei; 04-06-2009 at 12:33 PM.

  4. #24
    Thanks fumei
    What a great analysis.
    To answer your questions.
    1. Yes it is a .dot file

    2. No not dynamic changes

    3. Other persons in a group

    4. Yes the other person’s Normal.DOT file will have those AutoText entries?

    5. Yes definately possible, could be 200 pages

    6. Yes when the userform opens, ALL “pages” are in fact in the document

    7. When the checkbox is negative then the sub InsertPage(iNum As Integer) is called e.g. InsertPage 3

    8. Rechecking the negative checkbox activates InsertPage

    9. Yes bookmarks are used.

    10. Yes loop not required
    I have amended the code to:-
    Sub InsertPage(iNum As Integer)
    On Error GoTo Err_Handler
    Dim Mycounter As Long
    'NOTE When you insert a break, the break is inserted immediately preceding the selection.

    Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=iNum + 1
    Selection.MoveUp
    Select Case iNum
    Case 1:
    ActiveDocument.AttachedTemplate.AutoTextEntries("Table Of Contents").Insert Where:=Selection.Range, RichText:=True
    Case 2:
    ActiveDocument.AttachedTemplate.AutoTextEntries("List of Tables").Insert Where:=Selection.Range, RichText:=True
    Case 3:
    ActiveDocument.AttachedTemplate.AutoTextEntries("List of Figures").Insert Where:=Selection.Range, RichText:=True
    Case 4:
    ActiveDocument.AttachedTemplate.AutoTextEntries("List of Appendices").Insert Where:=Selection.Range, RichText:=True
    Case 5:
    ActiveDocument.AttachedTemplate.AutoTextEntries("List of Schedules").Insert Where:=Selection.Range, RichText:=True
    End Select
    Exit Sub
    Err_Handler:
    MsgBox Error(Err)
    Resume
    End Sub


    Using Word 2003

    11. By rechecking the checkbox activates InsertPage. The number used is hard coded subject to which checkbox is being rechecked.

  5. #25
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Sorry, but: "7. When the checkbox is negative then the sub InsertPage(iNum As Integer) is called e.g. InsertPage 3" does not answer my question.

    Here is my question again.

    7. What, exactly, causes your Sub - Sub InsertPage(iNum As Integer) – to fire?

    Try again. What, exactly fires the Sub? It will not fire by itself. It will not fire checking or unchecking the checkbox. SOMETHING has to call it. So....what is it?

  6. #26
    example for the chkfigures checkbox
    Private Sub chkFigures_Change()
    If chkFigures.Value = 0 Then
    GetValues
    RemovePages 3
    Else
    InsertPage 3
    End If

    End Sub

  7. #27
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ummmm...sigh...and what does GetValues do?

    Why does RemovePages seem to have a hardcoded value of 3?

    What is the checkbox chkFigures? What does it do? What does it represent?

    Private Sub chkFigures_Change()

    I thought you stated there are five checkboxes, each one matched (in theory) to a specific "page". So what is the checkbox chkFigures for???

    Explain, as best you can, what: "8. Rechecking the negative checkbox activates InsertPage" actually means.

    I do not know what you mean by "negative" checkbox.

  8. #28
    in the example i give RemovePages 3 represents page 3....so when it goes to the removepages sub it is trying to remove page 3
    Yes there are 5 checkboxes...this is one of them called chkFigures
    Negative checkbox means the checkbox is unchecked
    checked it inserts the page number and
    Unchecked removes the page number

  9. #29
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Please do not give hardcoded examples. Your Sub MUST get a variable, yes? So your real Sub must not have "3", it must have a variable...which you get from...where? From GetValues? I asked what GetValues does and you did not answer. Does it get the value to be used by RemovePages/InsertPages?

    Let's try again.

    Your userform starts up with ALL the five checkboxes checked...yes?

    OK, there are five checkboxes. Please explain what each of them does. I still do not understand what chkFigures is for. One of the five is chkFigures...OK, what are the others, and what do they do?

    It almost seems like chkFigures ONLY processes against page 3. Is that the case? if so, then i understand the hardcoded "3".

    Try and walk me through the whole thing, step-by-step, listing EVERY single little step, no matter how small.

    BTW: I am trying another approach using INCLUDETEXT fields, rather than empty "pages" and AutoText.

    Question. Does any of your AutoText have graphical elements, or is it just text?

  10. #30
    userform starts with ALL five checkboxes checked

    the template when loaded contains the following:-
    page 1 is the title and contains other info.

    page 2 = Table of Contents
    page 3 = List of Figures
    page 4 = List of Tables
    page 5 = List of Appendices
    page 6 = List of Schedules

    once this is loaded, up comes the userform with checkboxes for pages 3-6 giving the user the option to remove one of the pages 3-6 or restore them if they have been removed.

    the autotext is like hereunder. It is the title of the page inserted
    list of figures
    Page
    Error! No table of figures entries found.

  11. #31
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I will ask again. Please walk me through step-by-step, no matter how small a step.

    "the template when loaded "

    What does that actually mean? I asked if it was a .DOT file - you answered yes. OK. So does the above mean:

    The user goes File > New and selects the template (.DOT file), thus cloning the .DOT file into a new document?

    If so...then SAY so. I need you to actually tell me EXACTLY what is going on. Better yet, if you can, post a file!

  12. #32
    user clicks on add-ins menu then selects the report wizard icon. A dot file is loaded with all of the 7 pages and then the VBA userform appears. It contains a multippage where the user enters information and on page 2 the checkboxes appear so the user can select which pages he/she wants in the report.
    I would love to post the file but is confidential protecting the user group

  13. #33
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am sorry, but I think I am having to give up on this thread. I will continue to work on my own efforts to have a dynamic page structure - even though you say:


    "2. No not dynamic changes"

    Excuse me, but I would consider being able to put specific pages in and out and in and out - as many times as a user wishes - as quite definitely dynamic.

    I am simply not getting enough information even though I have requested, repeatedly, a step-by-step walk through.

    "user clicks on add-ins menu then selects the report wizard icon."

    What addins menu? Do you mean the user goes Tools > Templates and addins?

    What report wizard icon?

    "A dot file is loaded with all of the 7 pages"

    A .DOT file that is used to clone a new document is NOT loaded. So I do not know what you mean by a dot file is loaded. It almost sounds like the .dot file ITSELF is opened. If that is the case, that is a BAD idea.

    It is certainly a bit of a challenge, and as I said, for my own interest I am continuing to see if I can do it properly. However, at this point, I do not see how I can be of much use for you. I am not getting full - or even consistent - information.

    You have repeatedly stated there are FIVE checkboxes. OK. How does that work with:

    "up comes the userform with checkboxes for pages 3-6 giving the user the option to remove one of the pages 3-6 or restore them if they have been removed."

    Let's see...

    "one of the pages 3-6"

    3
    4
    5
    6

    Hmmmmm, that makes...FOUR. Yet there are FIVE checkboxes you say. I have asked, repeatedly, what each of the five checkboxes do, and I still do not know. FOUR of them - apparently - are for pages 3 to 6 (3, 4, 5, 6 = FOUR), so what is the fifth for? Who knows?

    Good luck. I have tried to help, and would still like to help, but I do not see how I can. When (if) I get a working example of the dynamic paging structure you seem to want, I will post something.

  14. #34
    check box (1) is Table of Contents
    check box (2) is List of Tables
    check box (3) is List Of Figures
    check box (4) is List of Appendices
    check box (5) is List of Schedules
    I could give you some screen shots but i cannot include them here

  15. #35
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Sorry, but you are just not getting it. That is somewhat useful information, but it is just not enough. You simply do not answer direct questions.

    I asked if any of the AutoText has any graphical elements. No answer.

    I asked about "add-ins menu". No answer.

    I asked about report wizard icon. No answer.

    I asked you to clarify "dot file loaded". No answer.


    Here is another....

    list of figures
    Page
    Error! No table of figures entries found.

    THAT looks like a field. Is the AutoText entry a field? Does it have any other components, like plain text? If so, why not simply add/remove the field dynamically? Oh, right, you say this thing is not dynamic.

    Keep trying. It is a good project, and I hope you work something out. I will continue on my own, for my own interest.

  16. #36
    Thanks for all your help and analysis. Due to a time restraint, I have removed the option from the project, however I will look into it again later also.

  17. #37
    VBAX Regular
    Joined
    May 2011
    Posts
    10
    Location

    deleting a page break in VBA

    Hi Fumei,

    I know this thread is pretty much closed but I was hoping you could help as I have a similar project (basically using checkboxes to insert/delete/re-insert sections) in Word 2007.

    I think what I am trying to do is dynamic. I have a number of bookmarks and after each section (by which I mean a group of bookmarks which have info inserted in) I want to have a page break (or something similar). I have managed to achieve most of this.

    However, I cannot delete the page break as I cannot re-insert the bookmark around it (so that to delete, all I need to do is clear the bookmark). Is this possible?

    I have asked on another forum about this:
    p2p.wrox.com/word-vba/84085-numbering.html
    (Sorry, I haven't posted enough times for it to allow this link but it is most of it)

    (I asked on the reply of another question as it was a similar subject at the moment it is the latest reply named inserting a break within a bookmark )

    Thanks

  18. #38
    VBAX Regular Kenny22's Avatar
    Joined
    Mar 2012
    Location
    wa - pERTH
    Posts
    7
    Location
    I would have thought the following would work:

    hmm.. could i not get to a specific page using the following
    Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=4
    Then use your code
    Dim r As Range
    Set r = ActiveDocument.Range
    With r
    .Collapse Direction:=wdCollapseEnd
    .InsertBreak Type:=wdPageBreak
    End With
    Set r = Nothing

Posting Permissions

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