Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New page with VBA

Status
Not open for further replies.

rs2k

IS-IT--Management
Oct 16, 2003
7
GB
I have tried recording a macro to insert a new page,then copy & paste the template from page-1 onto it. This works OK, until I need to insert more pages. The error shown is 'A page named Page-2 already exists' what is the command to insert a new page and give it a unique (or even blank) name, is this possible?

Many thanks
Colin.
 
You can probably use the Pages.Count property

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
johnwm,
Not sure what that is, I will go and have a look and let you know the outcome.

Many thanks
Colin.
 
johnwm,
No joy, I couldn't figure out how to incorparate it into the macro I recorded. Below is the macro, how would it be amended to create a new page when triggered?

Sub NewPage()

Dim UndoScopeID1 As Long
UndoScopeID1 = Application.BeginUndoScope("Insert Page")
Dim vsoPage1 As Visio.Page
Set vsoPage1 = ActiveDocument.Pages.Add
vsoPage1.Name = "Page-3"
vsoPage1.Background = False
vsoPage1.Index = 3
vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageWidth).FormulaU = "420 mm"
vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageHeight).FormulaU = "297 mm"
vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPageLayout, visPLOSplit).FormulaU = "1"
vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesPageOrientation).FormulaU = "2"
vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesPaperKind).FormulaU = "8"
Application.EndUndoScope UndoScopeID1, True

Application.ActiveWindow.Page = Application.ActiveDocument.Pages.ItemU("aaa")

ActiveWindow.DeselectAll
ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(1), visSelect
Application.ActiveWindow.Selection.Copy

Application.ActiveWindow.Page = Application.ActiveDocument.Pages.ItemU("Page-3")

Application.ActiveWindow.Page.Paste

End Sub

Many thanks
Colin.
 

The Pages.Count property represents the total current number of pages. Instead of giving the new page a 'fixed' name:
Code:
    vsoPage1.Name = "Page-3"

Use the Pages.Count value as a variable:
Code:
    vsoPage1.Name = "Page-" & Pages.Count

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
johnwm,
Bare with me, I have tried but no joy.
"Use the Pages.Count value as a variable:"
I declared the variable with;
Dim Pages as Integer
Dim Pages.Count as Integer and,
Dim x as Integer
x= Pages
X= Pages.Count
I am still getting a runtime error. I have tried various combinations of the above, with no luck.

I reckon I am probably declaring them wrong, can you please show me the corect way.

I will be away for two weeks (Holiday) so please don't think I have given up with your help.

Many thanks

Colin.
 
Pages is a property of the Document object and shouldn't be declared. Just try putting a command button on Sheet1 with this code only underneath.
Code:
Private Sub CommandButton1_Click()
    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Insert Page")
    Dim vsoPage1 As Visio.Page
    Set vsoPage1 = ActiveDocument.Pages.Add
    vsoPage1.Name = "Page-" & Pages.Count
    vsoPage1.Background = False
    vsoPage1.Index = 3
    vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageWidth).FormulaU = "420 mm"
    vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageHeight).FormulaU = "297 mm"
    vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPageLayout, visPLOSplit).FormulaU = "1"
    vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesPageOrientation).FormulaU = "2"
    vsoPage1.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesPaperKind).FormulaU = "8"
    Application.EndUndoScope UndoScopeID1, True

End Sub

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Drop the line assigning a page name and let Visio create it's own.

Also, you can delete the statements referring to Undoscope from the macro you recorded.


John... Visio MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top