Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

VBA code won't run

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aal - 26 Mar 2008 17:58 GMT
I have the following (I found it in another post).  I am trying to use it to
duplicate pages.  When I run it, it just simply goes to a page that already
exists and does not dupliate and add pages.  Word will only ask me a page to
"go to" and won't do anything else.  Any ideas?

   Page1 = InputBox("Please Enter First Page Number")
   Page2 = InputBox("Please Enter Last Page Number")
   Count = InputBox("Please Enter Number of times to duplicate")
   With ActiveDocument.Range
       .Start = .GoTo(wdGoToPage, wdGoToAbsolute, , Page1).Start
       .End = .GoTo(wdGoToPage, wdGoToAbsolute, , Page2 + 1).Start
       .Copy
   End With
   With ActiveDocument.Range
       For i = 1 To Count
           .Collapse wdCollapseEnd
           .Paste
       Next
   End With
Tony Strazzeri - 27 Mar 2008 02:41 GMT
Hi aal,

I encourage posters to give feedback by writing back after receiving
the advice so that we can know whether the solution worked or not
etc.  Even if the reply is just "I gave up because I couldn't get it
to work".  I say this because I tracked down the post you got the
sample from (I think its this
http://groups.google.com.au/group/microsoft.public.word.vba.general/browse_threa
d/thread/d9df65dce7314e9f/a65f4e9c704c40c7?lnk=gst&q=.Start+%3D+.GoTo(wdGoToPage
%2C+wdGoToAbsolute%2C+%2C+Page1).Start#a65f4e9c704c40c7

).

That response correctly indicates a caveat that "Unless your pages are
separate by manual page breaks this is likely to go wrong."

However  the solution is incorrect because it attempts to modify the
Activedocument'e end range (it cant be modified!.)
That cause the .copy to fail with error: "This Method or property is
not available beause no text is selected"

The following modified code (although not elegant) will do what you
want.
Let us know if it does.

Cheers
TonyS.

   Page1 = InputBox("Please Enter First Page Number")
   Page2 = InputBox("Please Enter Last Page Number")
   Count = InputBox("Please Enter Number of times to duplicate")
   Dim thisRng As Range

   Set thisRng = ActiveDocument.Range
   With ActiveDocument.Range

       .GoTo wdGoToPage, wdGoToAbsolute, , Page1  '.Start
       thisRng.Start = .Start
       .GoTo wdGoToPage, wdGoToAbsolute, , Page2 + 1 '.Start
       'Note; the following line could also be "= .start" since the
location it goes to is a selection point
       thisRng.End = .End
       thisRng.Select
       thisRng.Copy
   End With
   With ActiveDocument.Range
       For I = 1 To Count
           .Collapse wdCollapseEnd
           .Paste
       Next
   End With
aal - 27 Mar 2008 14:01 GMT
Thanks, Tony.  Maybe I just don't understand how to run it.  I'm really not
that great at VBA.  I pasted in the code then returned to the document,
pressed F5 to run it and Find and Replace window pops up already on the Go To
tab.  It asks me to enter a page number.  I had page 1, inserted a page break
to make page two before I ever entered the code.  What am I doing wrong?

Also, if this code doesn't work, maybe we can do this another way.  I am
going to make a two-page template with each page having a separate layout.  I
want the user to be able to add pages to a document based on the template but
they will need to have two pages added each time.  For example, page 1 & 2
exists and they need to add another "set" (page 3 & 4 simultaneously).  Page
3 & 4 will have the same layouts as page 1 & 2, respectively.  Same goes if
they need page 5 & 6 added.  Understand?  I can't seem to get a good answer
no matter how I pose the question.  I'm not giving up.  There must be a way!

> Hi aal,
>
[quoted text clipped - 44 lines]
>         Next
>     End With
Graham Mayor - 27 Mar 2008 14:52 GMT
Press ALT+F8, select the macro and click Run
See http://www.gmayor.com/installing_macro.htm

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Thanks, Tony.  Maybe I just don't understand how to run it.  I'm
> really not that great at VBA.  I pasted in the code then returned to
[quoted text clipped - 61 lines]
>>         Next
>>     End With
Jean-Guy Marcil - 27 Mar 2008 15:58 GMT
> Also, if this code doesn't work, maybe we can do this another way.  I am
> going to make a two-page template with each page having a separate layout.  I
[quoted text clipped - 4 lines]
> they need page 5 & 6 added.  Understand?  I can't seem to get a good answer
> no matter how I pose the question.  I'm not giving up.  There must be a way!

You have to describe this with more details.

When the user adds pages, I understand that he must always add a set of two
pages.

From your description, and from the code I have seen, is it necessary that
the content of the added pages be the same as the content of existing pages?
In other words, if the user adds pages 3 & 4, must the content be the same as
the content from pages 1 & 2? Or just the lay out? What if pages 5 & 6 are
added in a document that contains 10 pages? Should the content of the new
pages 5 & 6 be identical to the content of 2 existing pages (That the user
would choose), or again, should just the layout be forced?
Can pages be added anywhere in teh document? Or just at the end? If
anywhere, since pages are added as a set of 2 pages, can the added pags be
inserted between an odd-numbered page and an even-numbered page (e.g. between
pages 3 and 4); or always between an even-numbered page and an odd-numbered
page, like between pages 4 and 5 or pages 2 and 3?

If you are working with layouts only, the whole copy/paste/InputBox approach
is wrong.

Also, are you working with templates (*.dot) or documents (*.doc)?
What Word version?
Tony Strazzeri - 28 Mar 2008 01:52 GMT
On Mar 28, 1:58 am, Jean-Guy Marcil
<JeanGuyMar...@discussions.microsoft.com> wrote:

> > Also, if this code doesn't work, maybe we can do this another way.  I am
> > going to make a two-page template with each page having a separate layout.  I
[quoted text clipped - 28 lines]
> Also, are you working with templates (*.dot) or documents (*.doc)?
> What Word version?

Additionally see my response to your other post "Auto Duplicate a Page
in Word?"

Cheers
TonyS.
Tony Strazzeri - 28 Mar 2008 01:24 GMT
> Thanks, Tony.  Maybe I just don't understand how to run it.  I'm really not
> that great at VBA.  I pasted in the code then returned to the document,
[quoted text clipped - 59 lines]
> >         Next
> >     End With

Put your in the macro in the VBA IDE then Press F5.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.