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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting all Visio Background pages with VBA

Status
Not open for further replies.

mhartman1

Technical User
May 5, 2006
155
Hello, I am trying to delete all background pages in Visio 2003 using the following code:
Sub DeleteBackGroundPages()

Dim MyPage As Page
For Each MyPage In ActiveDocument.Pages
'Test for a "Background" page here
If MyPage.Background = True Then
'Delete the page
MyPage.Delete True
End If
Next MyPage
End Sub

However when the code runs it only deletes a few pages at a time. For testing purposes, I used a document with ONE foreground page and FOUR background pages. On the first run of the procedure, TWO background pages were deleted, then next run deleted another page and finally on the third run, the fourth background page was deleted. I have tried it using "False" for the page reordering as well with no success.

Any advice would be greatly appreciated.
Thanks in advance.
Mark
 
Just a thought. I've never used VBA in Visio, but often encounter similar problems when deleting in other MS applications. It seems to lose the count as it is deleting. Try counting backwards:
Code:
Dim MyPage As Page
Dim P as Integer
For P=ActiveDocument.Pages.Count to 1 step -1
If MyPage.Background = True Then
'Delete the page
MyPage.Delete True
End If
Next P

Don't know about the pages collection, so you may need to count to 0 rather than 1. As I say - just a thought - I may be completely wrong [pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top