Odd behavior when inserting TextBox1 into header
Author |
Message |
Susa #1 / 9
|
 Odd behavior when inserting TextBox1 into header
This time I'm creating a report template that works well to create a basic report that has three sections: the title page [section break], the table of contents [section break], and the first chapter. Now I'm being stymied by the macro I'm developing that will insert a new chapter. I'm using a sample document created with the report template and recording a macro to (1) insert a section break at the cursor location; (2) apply Heading 1 to the first paragraph in that new section and typing in "TB1" as a marker for the ultimate placement of TextBox1; (3) insert a manual page break; (4) go to the header on page two of the new section 4; (5) turn off the "Same as Previous" button; (6) select and delete the text carried forward from section 3's page two header; (7) type "TB1" in the section 4, page 2 header; and (8) return to the main document and delete the page break. Then I copy the text from this macro; insert it as the code for the OK command button in UserForm2 in the template; and change the two lines of Selection.Type TB1 code to Selection.InsertAfter TextBox1. After saving and exiting the template, I create a new sample document, run the InsertSection macro, and do a quality control check of the macro's output. What I get is the odd behavior. My document comes out as Title Page, Table of Contents, first page of Chapter 1, second page of Chapter 1 with the title for Chapter 2 in the header, first page of Chapter 2, second page of Chapter 2 with Chapter 1's title in the header. Any ideas why these two headers seem to have changed places without the cursor moving from one header to the other? I'm stumped!
|
Sun, 10 Apr 2005 08:37:44 GMT |
|
 |
Doug Robbins - Word MV #2 / 9
|
 Odd behavior when inserting TextBox1 into header
Hi Susan, Your problem is most likely caused by the fact that the macro recorder most times works on the Selection object, which can be a bit non specific. Using the Range object is far more precise. In your case, it is probably more appropriate to be using the Section object as in the following: ActiveDocument.Sections.Add ActiveDocument.Sections(4).Range.InsertBefore "TB1" ActiveDocument.Sections(4).Range.Paragraphs(1).Style = wdStyleHeading1 ActiveDocument.Sections(4).Headers(wdHeaderFooterPrimary).LinkToPrevious = False ActiveDocument.Sections(4).Headers(wdHeaderFooterPrimary).Range.Delete ActiveDocument.Sections(4).Headers(wdHeaderFooterPrimary).Range.InsertBefore "TB1" If I have interpreted what you are doing correctly, the above lines of code should be all that you need. It will be easier to help you sort out the problem if you copy and paste the code into a post back to this newsgroup as a continuation of this thread. Please post any response to the newsgroups for the benefit of others who may also be following the thread. Hope this helps, Doug Robbins - Word MVP
Quote: > This time I'm creating a report template that works well > to create a basic report that has three sections: the > title page [section break], the table of contents [section > break], and the first chapter. Now I'm being stymied by > the macro I'm developing that will insert a new chapter. > I'm using a sample document created with the report > template and recording a macro to (1) insert a section > break at the cursor location; (2) apply Heading 1 to the > first paragraph in that new section and typing in "TB1" as > a marker for the ultimate placement of TextBox1; (3) > insert a manual page break; (4) go to the header on page > two of the new section 4; (5) turn off the "Same as > Previous" button; (6) select and delete the text carried > forward from section 3's page two header; (7) type "TB1" > in the section 4, page 2 header; and (8) return to the > main document and delete the page break. > Then I copy the text from this macro; insert it as the > code for the OK command button in UserForm2 in the > template; and change the two lines of Selection.Type TB1 > code to Selection.InsertAfter TextBox1. > After saving and exiting the template, I create a new > sample document, run the InsertSection macro, and do a > quality control check of the macro's output. What I get > is the odd behavior. My document comes out as Title Page, > Table of Contents, first page of Chapter 1, second page of > Chapter 1 with the title for Chapter 2 in the header, > first page of Chapter 2, second page of Chapter 2 with > Chapter 1's title in the header. > Any ideas why these two headers seem to have changed > places without the cursor moving from one header to the > other? I'm stumped!
|
Sun, 10 Apr 2005 18:26:58 GMT |
|
 |
Susa #3 / 9
|
 Odd behavior when inserting TextBox1 into header
Thanks for the prompt reply. I'll try your suggested coding this morning. If it doesn't work, I'll post again with the template attached and we'll try again. Quote: >-----Original Message----- >Hi Susan, >Your problem is most likely caused by the fact that the macro recorder most >times works on the Selection object, which can be a bit
non specific. Using Quote: >the Range object is far more precise. In your case, it is probably more >appropriate to be using the Section object as in the following: > ActiveDocument.Sections.Add > ActiveDocument.Sections(4).Range.InsertBefore "TB1" > ActiveDocument.Sections(4).Range.Paragraphs(1).Style = wdStyleHeading1 > ActiveDocument.Sections(4).Headers
(wdHeaderFooterPrimary).LinkToPrevious Quote: >= False > ActiveDocument.Sections(4).Headers
(wdHeaderFooterPrimary).Range.Delete Quote: >ActiveDocument.Sections(4).Headers
(wdHeaderFooterPrimary).Range.InsertBefore Quote: >"TB1" >If I have interpreted what you are doing correctly, the above lines of code >should be all that you need. >It will be easier to help you sort out the problem if you copy and paste the >code into a post back to this newsgroup as a continuation of this thread. >Please post any response to the newsgroups for the
benefit of others who may Quote: >also be following the thread. >Hope this helps, >Doug Robbins - Word MVP
>> This time I'm creating a report template that works well >> to create a basic report that has three sections: the >> title page [section break], the table of contents [section >> break], and the first chapter. Now I'm being stymied by >> the macro I'm developing that will insert a new chapter. >> I'm using a sample document created with the report >> template and recording a macro to (1) insert a section >> break at the cursor location; (2) apply Heading 1 to the >> first paragraph in that new section and typing in "TB1" as >> a marker for the ultimate placement of TextBox1; (3) >> insert a manual page break; (4) go to the header on page >> two of the new section 4; (5) turn off the "Same as >> Previous" button; (6) select and delete the text carried >> forward from section 3's page two header; (7) type "TB1" >> in the section 4, page 2 header; and (8) return to the >> main document and delete the page break. >> Then I copy the text from this macro; insert it as the >> code for the OK command button in UserForm2 in the >> template; and change the two lines of Selection.Type TB1 >> code to Selection.InsertAfter TextBox1. >> After saving and exiting the template, I create a new >> sample document, run the InsertSection macro, and do a >> quality control check of the macro's output. What I get >> is the odd behavior. My document comes out as Title Page, >> Table of Contents, first page of Chapter 1, second page of >> Chapter 1 with the title for Chapter 2 in the header, >> first page of Chapter 2, second page of Chapter 2 with >> Chapter 1's title in the header. >> Any ideas why these two headers seem to have changed >> places without the cursor moving from one header to the >> other? I'm stumped! >.
|
Mon, 11 Apr 2005 00:17:39 GMT |
|
 |
Mark Tangar #4 / 9
|
 Odd behavior when inserting TextBox1 into header
Susan, DOn't attach the template here. A lot of people who answer questions have their newsreader software set to block all messages that have attachments (reasons: virus risk, and major increases to download time for the entire newsgroup); so quite often, a post with an attachment won't even be seen by the audience you're addressing. It's quite rare that a problem here requires the actual template, and when it is, you'll generally be asked to email it privately. More at http://www.mvps.org/word/FindHelp/Posting.htm. Hope this helps. --
Reply ONLY to the newsgroup. MVPs do not work for Microsoft. MVP FAQ: http://www.mvps.org/word "Life is nothing if you're not obsessed." --John Waters Quote:
> Thanks for the prompt reply. I'll try your suggested > coding this morning. If it doesn't work, I'll post again > with the template attached and we'll try again. > >-----Original Message----- > >Hi Susan, > >Your problem is most likely caused by the fact that the > macro recorder most > >times works on the Selection object, which can be a bit > non specific. Using > >the Range object is far more precise. In your case, it > is probably more > >appropriate to be using the Section object as in the > following: > > ActiveDocument.Sections.Add > > ActiveDocument.Sections(4).Range.InsertBefore "TB1" > > ActiveDocument.Sections(4).Range.Paragraphs(1).Style > = wdStyleHeading1 > > ActiveDocument.Sections(4).Headers > (wdHeaderFooterPrimary).LinkToPrevious > >= False > > ActiveDocument.Sections(4).Headers > (wdHeaderFooterPrimary).Range.Delete > >ActiveDocument.Sections(4).Headers > (wdHeaderFooterPrimary).Range.InsertBefore > >"TB1" > >If I have interpreted what you are doing correctly, the > above lines of code > >should be all that you need. > >It will be easier to help you sort out the problem if you > copy and paste the > >code into a post back to this newsgroup as a continuation > of this thread. > >Please post any response to the newsgroups for the > benefit of others who may > >also be following the thread. > >Hope this helps, > >Doug Robbins - Word MVP
> >> This time I'm creating a report template that works well > >> to create a basic report that has three sections: the > >> title page [section break], the table of contents > [section > >> break], and the first chapter. Now I'm being stymied by > >> the macro I'm developing that will insert a new chapter. > >> I'm using a sample document created with the report > >> template and recording a macro to (1) insert a section > >> break at the cursor location; (2) apply Heading 1 to the > >> first paragraph in that new section and typing in "TB1" > as > >> a marker for the ultimate placement of TextBox1; (3) > >> insert a manual page break; (4) go to the header on page > >> two of the new section 4; (5) turn off the "Same as > >> Previous" button; (6) select and delete the text carried > >> forward from section 3's page two header; (7) type "TB1" > >> in the section 4, page 2 header; and (8) return to the > >> main document and delete the page break. > >> Then I copy the text from this macro; insert it as the > >> code for the OK command button in UserForm2 in the > >> template; and change the two lines of Selection.Type TB1 > >> code to Selection.InsertAfter TextBox1. > >> After saving and exiting the template, I create a new > >> sample document, run the InsertSection macro, and do a > >> quality control check of the macro's output. What I get > >> is the odd behavior. My document comes out as Title > Page, > >> Table of Contents, first page of Chapter 1, second page > of > >> Chapter 1 with the title for Chapter 2 in the header, > >> first page of Chapter 2, second page of Chapter 2 with > >> Chapter 1's title in the header. > >> Any ideas why these two headers seem to have changed > >> places without the cursor moving from one header to the > >> other? I'm stumped! > >.
|
Mon, 11 Apr 2005 01:48:41 GMT |
|
 |
Susa #5 / 9
|
 Odd behavior when inserting TextBox1 into header
I can understand the hesitation with .doc or .dot files. I subsequently posted a reply to Mark's post, when his coding didn't work in a second activiation, and attached a .pdf file to illustrate what was occurring. The errant coding was included as a cut-and-paste. I generally don't have qualms opening acrobat files, but I can see how filters look for any attachments rather than specific types of attachments. Quote: >-----Original Message----- >Susan, >DOn't attach the template here. A lot of people who answer >questions have their newsreader software set to block all >messages that have attachments (reasons: virus risk, and >major increases to download time for the entire newsgroup); >so quite often, a post with an attachment won't even be seen >by the audience you're addressing. It's quite rare that a >problem here requires the actual template, and when it is, >you'll generally be asked to email it privately. >More at http://www.mvps.org/word/FindHelp/Posting.htm. >Hope this helps. >--
>Reply ONLY to the newsgroup. MVPs do not work for Microsoft. >MVP FAQ: http://www.mvps.org/word >"Life is nothing if you're not obsessed." --John Waters
>> Thanks for the prompt reply. I'll try your suggested >> coding this morning. If it doesn't work, I'll post again >> with the template attached and we'll try again. >> >-----Original Message----- >> >Hi Susan, >> >Your problem is most likely caused by the fact that the >> macro recorder most >> >times works on the Selection object, which can be a bit >> non specific. Using >> >the Range object is far more precise. In your case, it >> is probably more >> >appropriate to be using the Section object as in the >> following: >> > ActiveDocument.Sections.Add >> > ActiveDocument.Sections(4).Range.InsertBefore "TB1" >> > ActiveDocument.Sections(4).Range.Paragraphs (1).Style >> = wdStyleHeading1 >> > ActiveDocument.Sections(4).Headers >> (wdHeaderFooterPrimary).LinkToPrevious >> >= False >> > ActiveDocument.Sections(4).Headers >> (wdHeaderFooterPrimary).Range.Delete >> >ActiveDocument.Sections(4).Headers >> (wdHeaderFooterPrimary).Range.InsertBefore >> >"TB1" >> >If I have interpreted what you are doing correctly, the >> above lines of code >> >should be all that you need. >> >It will be easier to help you sort out the problem if you >> copy and paste the >> >code into a post back to this newsgroup as a continuation >> of this thread. >> >Please post any response to the newsgroups for the >> benefit of others who may >> >also be following the thread. >> >Hope this helps, >> >Doug Robbins - Word MVP
>> >> This time I'm creating a report template that works well >> >> to create a basic report that has three sections: the >> >> title page [section break], the table of contents >> [section >> >> break], and the first chapter. Now I'm being stymied by >> >> the macro I'm developing that will insert a new chapter. >> >> I'm using a sample document created with the report >> >> template and recording a macro to (1) insert a section >> >> break at the cursor location; (2) apply Heading 1 to the >> >> first paragraph in that new section and typing in "TB1" >> as >> >> a marker for the ultimate placement of TextBox1; (3) >> >> insert a manual page break; (4) go to the header on page >> >> two of the new section 4; (5) turn off the "Same as >> >> Previous" button; (6) select and delete the text carried >> >> forward from section 3's page two header; (7) type "TB1" >> >> in the section 4, page 2 header; and (8) return to the >> >> main document and delete the page break. >> >> Then I copy the text from this macro; insert it as the >> >> code for the OK command button in UserForm2 in the >> >> template; and change the two lines of Selection.Type TB1 >> >> code to Selection.InsertAfter TextBox1. >> >> After saving and exiting the template, I create a new >> >> sample document, run the InsertSection macro, and do a >> >> quality control check of the macro's output. What I get >> >> is the odd behavior. My document comes out as Title >> Page, >> >> Table of Contents, first page of Chapter 1, second page >> of >> >> Chapter 1 with the title for Chapter 2 in the header, >> >> first page of Chapter 2, second page of Chapter 2 with >> >> Chapter 1's title in the header. >> >> Any ideas why these two headers seem to have changed >> >> places without the cursor moving from one header to the >> >> other? I'm stumped! >> >. >.
|
Mon, 11 Apr 2005 02:53:26 GMT |
|
 |
Doug Robbins - Word MV #6 / 9
|
 Odd behavior when inserting TextBox1 into header
Hi Susan, If the section that you are adding is always at the end of the document, after adding it, the index number for it can by obtained by using ActiveDocument.Sections.Count. Use the number returned by that function in place of the 4 in the code that I suggested. Please post any response to the newsgroups for the benefit of others who may also be following the thread. Hope this helps, Doug Robbins - Word MVP
Quote: > As promised in my previous post, I used the code you > provided as the code for the OK command button in > UserForm2. It worked. The headings in both chapters > contained the correct wording in the correct places. > My concern, tho, when I saw the (4) in your code was that > if I used that coding to insert Chapter 3 (or any other > additional chapters), it wouldn't work. What I get is > shown in the attached .pdf file. > Is there a way to automatically up the (4) part of your > code to be n+1, where n is the number of the section in > which the cursor is positioned? > If we can't do that, how can we fix the coding from the > original switcharoo problem (provided below)? When the > macro is activiated, the cursor should be at the location > where the new section is to be inserted. > Private Sub CommandButton1_Click() > ' insert section and page breaks and > ' insert section title in heading 1 > Selection.TypeParagraph > Selection.InsertBreak Type:=wdSectionBreakNextPage > Selection.TypeParagraph > Selection.MoveUp Unit:=wdLine, Count:=1 > Selection.Style = ActiveDocument.Styles("Heading 1") > Selection.TypeText Text:=vbTab > Selection.InsertAfter TextBox1 > Selection.MoveDown Unit:=wdLine, Count:=1 > Selection.InsertBreak Type:=wdPageBreak > ' cursor should now be on page 2 of new section > ' go to header on page 2, unlink it from previous header, > ' erase existing text, and insert section title > If ActiveWindow.View.SplitSpecial <> wdPaneNone Then > ActiveWindow.Panes(2).Close > End If > If ActiveWindow.ActivePane.View.Type = wdNormalView Or > ActiveWindow. _ > ActivePane.View.Type = wdOutlineView Then > ActiveWindow.ActivePane.View.Type = wdPrintView > End If > ActiveWindow.ActivePane.View.SeekView = > wdSeekPrimaryHeader > If Selection.HeaderFooter.LinkToPrevious = True > Then > Selection.HeaderFooter.LinkToPrevious = False > End If > Selection.Extend > Selection.MoveDown Unit:=wdParagraph, Count:=1 > Selection.MoveLeft Unit:=wdCharacter, Count:=1 > Selection.Delete Unit:=wdCharacter, Count:=1 > Selection.InsertAfter TextBox1 > ' return to main document and erase the page break > ' cursor should be at text entry position > ' in page 1 of Chap 2 > ActiveWindow.ActivePane.View.SeekView = > wdSeekMainDocument > Selection.TypeBackspace > UserForm2.Hide > End Sub > >-----Original Message----- > >Your problem is most likely caused by the fact that the > macro recorder most > >times works on the Selection object, which can be a bit > non specific. Using > >the Range object is far more precise. In your case, it > is probably more > >appropriate to be using the Section object as in the > following: > > ActiveDocument.Sections.Add > > ActiveDocument.Sections(4).Range.InsertBefore "TB1" > > ActiveDocument.Sections(4).Range.Paragraphs(1).Style > = wdStyleHeading1 > > ActiveDocument.Sections(4).Headers > (wdHeaderFooterPrimary).LinkToPrevious > >= False > > ActiveDocument.Sections(4).Headers > (wdHeaderFooterPrimary).Range.Delete > >ActiveDocument.Sections(4).Headers > (wdHeaderFooterPrimary).Range.InsertBefore > >"TB1" > >If I have interpreted what you are doing correctly, the > above lines of code > >should be all that you need. > >It will be easier to help you sort out the problem if you > copy and paste the > >code into a post back to this newsgroup as a continuation > of this thread. > >Doug Robbins - Word MVP
> >> This time I'm creating a report template that works well > >> to create a basic report that has three sections: the > >> title page [section break], the table of contents > [section > >> break], and the first chapter. Now I'm being stymied by > >> the macro I'm developing that will insert a new chapter. > >> I'm using a sample document created with the report > >> template and recording a macro to (1) insert a section > >> break at the cursor location; (2) apply Heading 1 to the > >> first paragraph in that new section and typing in "TB1" > as > >> a marker for the ultimate placement of TextBox1; (3) > >> insert a manual page break; (4) go to the header on page > >> two of the new section 4; (5) turn off the "Same as > >> Previous" button; (6) select and delete the text carried > >> forward from section 3's page two header; (7) type "TB1" > >> in the section 4, page 2 header; and (8) return to the > >> main document and delete the page break. > >> Then I copy the text from this macro; insert it as the > >> code for the OK command button in UserForm2 in the > >> template; and change the two lines of Selection.Type TB1 > >> code to Selection.InsertAfter TextBox1. > >> After saving and exiting the template, I create a new > >> sample document, run the InsertSection macro, and do a > >> quality control check of the macro's output. What I get > >> is the odd behavior. My document comes out as Title > Page, > >> Table of Contents, first page of Chapter 1, second page > of > >> Chapter 1 with the title for Chapter 2 in the header, > >> first page of Chapter 2, second page of Chapter 2 with > >> Chapter 1's title in the header. > >> Any ideas why these two headers seem to have changed > >> places without the cursor moving from one header to the > >> other? I'm stumped!
|
Mon, 11 Apr 2005 04:10:34 GMT |
|
 |
susa #7 / 9
|
 Odd behavior when inserting TextBox1 into header
Thanks again for the phenomenally quick replies. Probably 95% of the time the new section will be the last section and I can add code to move the cursor to the end of the document. I'll just put something in the written instructions and UserForm2 to forewarn the user. I guess that, as much as we'd like to, we just can't handle 100% of the situations 100% of the time. I understand the concept of what you're advising me to do vis-a-vis the ActiveDocument.Sections.Count, but I'm at a loss as to the mechanics of how to do it. How do I get the number to return and then get it into the former (4) position? Side Question: Since Help isn't always helpful (it's written in a foreign language, right?), can you recommend a book that gives some step-by-step, introductory lessons on the rudiments of what I'm trying to do by the "maybe that'll work" method. Quote: >-----Original Message----- >Hi Susan, >If the section that you are adding is always at the end of the document, >after adding it, the index number for it can by obtained by using >ActiveDocument.Sections.Count. Use the number returned by that function in >place of the 4 in the code that I suggested.
|
Mon, 11 Apr 2005 04:59:18 GMT |
|
 |
Doug Robbins - Word MV #8 / 9
|
 Odd behavior when inserting TextBox1 into header
Hi Susan Use Dim i as Integer i = ActiveDocument.Sections.Count ActiveDocument.Sections(i).etc Please post any response to the newsgroups for the benefit of others who may also be following the thread. Hope this helps, Doug Robbins - Word MVP
Quote: > Thanks again for the phenomenally quick replies. > Probably 95% of the time the new section will be the last > section and I can add code to move the cursor to the end > of the document. I'll just put something in the written > instructions and UserForm2 to forewarn the user. I guess > that, as much as we'd like to, we just can't handle 100% > of the situations 100% of the time. > I understand the concept of what you're advising me to do > vis-a-vis the ActiveDocument.Sections.Count, but I'm at a > loss as to the mechanics of how to do it. How do I get > the number to return and then get it into the former (4) > position? > Side Question: > Since Help isn't always helpful (it's written in a foreign > language, right?), can you recommend a book that gives > some step-by-step, introductory lessons on the rudiments > of what I'm trying to do by the "maybe that'll work" > method. > >-----Original Message----- > >Hi Susan, > >If the section that you are adding is always at the end > of the document, > >after adding it, the index number for it can by obtained > by using > >ActiveDocument.Sections.Count. Use the number returned > by that function in > >place of the 4 in the code that I suggested.
|
Mon, 11 Apr 2005 17:14:54 GMT |
|
 |
Doug Robbins - Word MV #9 / 9
|
 Odd behavior when inserting TextBox1 into header
Hi Susan, Re your quest for a book, I honestly can't recommend any unless you have trouble getting to sleep. Then I find any book will do. Take a look under the Macros tab at http://www.mvps.org/word/FAQs/index.html. There you will find a distillation of the combined knowledge of the Word MVP's, something akin to a good liquer. Please post any response to the newsgroups for the benefit of others who may also be following the thread. Hope this helps, Doug Robbins - Word MVP
Quote: > Thanks again for the phenomenally quick replies. > Probably 95% of the time the new section will be the last > section and I can add code to move the cursor to the end > of the document. I'll just put something in the written > instructions and UserForm2 to forewarn the user. I guess > that, as much as we'd like to, we just can't handle 100% > of the situations 100% of the time. > I understand the concept of what you're advising me to do > vis-a-vis the ActiveDocument.Sections.Count, but I'm at a > loss as to the mechanics of how to do it. How do I get > the number to return and then get it into the former (4) > position? > Side Question: > Since Help isn't always helpful (it's written in a foreign > language, right?), can you recommend a book that gives > some step-by-step, introductory lessons on the rudiments > of what I'm trying to do by the "maybe that'll work" > method. > >-----Original Message----- > >Hi Susan, > >If the section that you are adding is always at the end > of the document, > >after adding it, the index number for it can by obtained > by using > >ActiveDocument.Sections.Count. Use the number returned > by that function in > >place of the 4 in the code that I suggested.
|
Mon, 11 Apr 2005 17:29:50 GMT |
|
|
|