|
From: daniel lie on 2 Jul 2008 02:26 Hi, I was wondering if anyone might be able to help me with the following problem that i'm having. Is there a way of figuring out where the cursor is in a document? The reason for that is I have a code that would insert a new Section in the document report. Here is the code ' Insert a heading for the new section. Selection.Style = ActiveDocument.Styles("Section Heading 1") ' Insert the heading's text based on InputBox entry. ' Add a paragraph break to return to "Normal" text. Selection.InsertBreak Type:=wdPageBreak Selection.TypeText Text:=InputBox("Please enter section heading:", "Enter Section Heading") Selection.TypeParagraph This will work if the cursor is on the newline. It would place another section (e.g. Section 2) on the next page. However, the problem arises when the cursor is not on the newline. For example, Section 1. Water Sewage Blah blah Classification report If the user by accident leaves the cursor at the end of the word "report" and select the macro to insert a new section, it'd be stuffed up. Instead of getting Section 2 to show on the next page, i'd get as follows: Section 1. Water Sewage Blah blah Section 2. Classification report --------- page break ------- Section 3. Reconciliation Not sure if anyone would like to throw in some ideas here. What i come up with is if we could find out where the cursor is and can check to see if it's at the newline or not. Your help'd be greatly appreciated. Thank you in advance url:http://www.ureader.com/gp/1022-1.aspx
From: Doug Robbins - Word MVP on 2 Jul 2008 02:47 You should use the .Range object rather than the Selection object and set/manipulate the range to be the place where you want the Break inserted. -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "daniel lie" <ubuntu76(a)yahoo.com.au> wrote in message news:a1afb10bb8bf41589c30b1a56c57efda(a)newspe.com... > Hi, > > I was wondering if anyone might be able to help me with the following > problem that i'm having. > > Is there a way of figuring out where the cursor is in a document? > > The reason for that is I have a code that would insert a new Section in > the > document report. > > Here is the code > > ' Insert a heading for the new section. > Selection.Style = ActiveDocument.Styles("Section Heading 1") > > ' Insert the heading's text based on InputBox entry. > ' Add a paragraph break to return to "Normal" text. > > Selection.InsertBreak Type:=wdPageBreak > > Selection.TypeText Text:=InputBox("Please enter section heading:", "Enter > Section Heading") > Selection.TypeParagraph > > This will work if the cursor is on the newline. It would place another > section (e.g. Section 2) on the next page. However, the problem arises > when > the cursor is not on the newline. > > For example, > > Section 1. Water Sewage > Blah blah > Classification report > > If the user by accident leaves the cursor at the end of the word "report" > and select the macro to insert a new section, it'd be stuffed up. > > Instead of getting Section 2 to show on the next page, i'd get as follows: > > Section 1. Water Sewage > Blah blah > > Section 2. Classification report > > --------- page break ------- > Section 3. Reconciliation > > Not sure if anyone would like to throw in some ideas here. What i come up > with is if we could find out where the cursor is and can check to see if > it's at the newline or not. > > Your help'd be greatly appreciated. > > Thank you in advance > > url:http://www.ureader.com/gp/1022-1.aspx
From: StevenM on 2 Jul 2008 08:27 To: Daniel, << Is there a way of figuring out where the cursor is in a document? >> Of course, but in relation to what? You speak of a "newline," do you mean an empty line, or something else? If you're looking for the next empty line, then perhaps you need something like: Selection.Bookmarks("\line").Select While Len(Selection.Text) > 1 Selection.Move wdLine, 1 Selection.Bookmarks("\line").Select Wend Selection.Collapse wdCollapseStart Each empty line has a paragraph mark, and so a lenght of 1. Steven Craig Miller "daniel lie" wrote: > Hi, > > I was wondering if anyone might be able to help me with the following > problem that i'm having. > > Is there a way of figuring out where the cursor is in a document? > > The reason for that is I have a code that would insert a new Section in the > document report. > > Here is the code > > ' Insert a heading for the new section. > Selection.Style = ActiveDocument.Styles("Section Heading 1") > > ' Insert the heading's text based on InputBox entry. > ' Add a paragraph break to return to "Normal" text. > > Selection.InsertBreak Type:=wdPageBreak > > Selection.TypeText Text:=InputBox("Please enter section heading:", "Enter > Section Heading") > Selection.TypeParagraph > > This will work if the cursor is on the newline. It would place another > section (e.g. Section 2) on the next page. However, the problem arises when > the cursor is not on the newline. > > For example, > > Section 1. Water Sewage > Blah blah > Classification report > > If the user by accident leaves the cursor at the end of the word "report" > and select the macro to insert a new section, it'd be stuffed up. > > Instead of getting Section 2 to show on the next page, i'd get as follows: > > Section 1. Water Sewage > Blah blah > > Section 2. Classification report > > --------- page break ------- > Section 3. Reconciliation > > Not sure if anyone would like to throw in some ideas here. What i come up > with is if we could find out where the cursor is and can check to see if > it's at the newline or not. > > Your help'd be greatly appreciated. > > Thank you in advance > > url:http://www.ureader.com/gp/1022-1.aspx >
From: daniel lie on 4 Jul 2008 02:53 Message-ID: <407c95c028fd467e8d1c14db1b433852(a)newspe.com> X-Mailer: http://www.umailcampaign.com, ip log:58.6.69.163 Newsgroups: microsoft.public.word.vba.general NNTP-Posting-Host: 22.bb.5446.static.theplanet.com 70.84.187.34 Path: TK2MSFTNGP01.phx.gbl!TK2MSFTNGP03.phx.gbl!newspe.com Lines: 1 Xref: TK2MSFTNGP01.phx.gbl microsoft.public.word.vba.general:121581 Thank you Doug and Steven for your replies. Steven, apologise for not making it a lot clearer for you to understand my problem. Yes, your assumption's right. It's the empty line I was talking about. However, not sure what you meant by "but in relation to what?". I tried the code you gave and it still doesn't work. I think this may be to do with the fact that there is a page break involved there in the code. Here is the code i got myValue = InputBox("Enter here: ") If myValue <> "" Then myText = Trim(myValue) ' Insert a heading for the new section. Selection.Style = ActiveDocument.Styles("Section Heading 1") Selection.Bookmarks("\line").Select While Len(Selection.Text) > 1 Selection.Move wdLine, 1 Selection.Bookmarks("\line").Select Wend Selection.Collapse wdCollapseStart 'move cursor to the start of the new paragraph ' Add a paragraph break to return to "Normal" text. Selection.InsertBreak Type:=wdPageBreak Selection.TypeText (myText) Selection.TypeParagraph End If In the mean time, trying to figure how to use .range Thank you in advance url:http://www.ureader.com/msg/10228359.aspx
From: Doug Robbins - Word MVP on 4 Jul 2008 05:20 The following code will insert a next page section break after the paragraph in which the cursor is located: Dim myrange As Range Set myrange = Selection.Paragraphs(1).Range myrange.Collapse wdCollapseEnd myrange.InsertBreak wdSectionBreakNextPage -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP "daniel lie" <ubuntu76(a)yahoo.com.au> wrote in message news:... > Message-ID: <407c95c028fd467e8d1c14db1b433852(a)newspe.com> > X-Mailer: http://www.umailcampaign.com, ip log:58.6.69.163 > Newsgroups: microsoft.public.word.vba.general > NNTP-Posting-Host: 22.bb.5446.static.theplanet.com 70.84.187.34 > Path: TK2MSFTNGP01.phx.gbl!TK2MSFTNGP03.phx.gbl!newspe.com > Lines: 1 > Xref: TK2MSFTNGP01.phx.gbl microsoft.public.word.vba.general:121581 > > Thank you Doug and Steven for your replies. > > Steven, apologise for not making it a lot clearer for you to understand my > problem. > > Yes, your assumption's right. It's the empty line I was talking about. > However, not sure what you meant by "but in relation to what?". > > I tried the code you gave and it still doesn't work. I think this may be > to > do with the fact that there is a page break involved there in the code. > > Here is the code i got > myValue = InputBox("Enter here: ") > > If myValue <> "" Then > myText = Trim(myValue) > > ' Insert a heading for the new section. > Selection.Style = ActiveDocument.Styles("Section Heading 1") > > Selection.Bookmarks("\line").Select > While Len(Selection.Text) > 1 > Selection.Move wdLine, 1 > Selection.Bookmarks("\line").Select > Wend > Selection.Collapse wdCollapseStart 'move cursor to the start of > the > new paragraph > > ' Add a paragraph break to return to "Normal" text. > Selection.InsertBreak Type:=wdPageBreak > Selection.TypeText (myText) > Selection.TypeParagraph > End If > > In the mean time, trying to figure how to use .range > > Thank you in advance > > url:http://www.ureader.com/msg/10228359.aspx
|
Pages: 1 Prev: figure out cursor location in word 03 Next: Controling Custom Dialog Box - Word 2000 |