VBA code won't run

A

aal

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
 
T

Tony Strazzeri

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/m...GoToAbsolute,+,+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
 
A

aal

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!
 
J

Jean-Guy Marcil

aal said:
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!

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?
 
T

Tony Strazzeri

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 GoTo
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 goesif
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!

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

Tony Strazzeri

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 sameas
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?

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

Cheers
TonyS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top