Simple macro loop, not looping

Started by wheatman, September 08, 2021, 08:05:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wheatman

Okay, this is killing me. Just trying to set every sheet in my visio diagrams to ViewFit = 1 and set the page setup to fit.  Here is the code and it just does not loop each page.

Sub Macro2()

Dim pg As Visio.Page
Dim shpPg As Visio.Shape
For Each pg In ActiveDocument.Pages
Set shpPg = pg.PageSheet

    Application.ActiveWindow.ViewFit = 1

    '//Dim UndoScopeID1 As Long
    '//UndoScopeID1 = Application.BeginUndoScope("Page Setup")
    Application.ActivePage.Background = False
    Application.ActivePage.BackPage = ""
    Application.ActivePage.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesOnPage).FormulaU = "1"
    '//Application.EndUndoScope UndoScopeID1, True
Next pg


End Sub

Surrogate

Quote from: wheatman on September 08, 2021, 08:05:40 PMHere is the code and it just does not loop each page.
What is wrong ? Do you want set ViewFit for Background-pages ?

Paul Herber

You have the page reference in the loop as pg, then you are doing all the work on ActivePage, not the same thing.
Try

  pg.Background = False
  etc
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

wheatman

I was in a bit of hurry, so let me explain what I am trying to do.  I have a hundreds of vision diagrams. Each visio diagram has multiple pages. I am trying to use a macro so that when I open a visio diagram, I can run the macro which will fit the page to the window and set the page setup to "Fit to" 1. The macro needs to do this to all the pages in the Vision document, not have to be run for each page. So this way, when I run Adobe Acrobat DC to convert these all to PDF, the Visio output has each sheet on one page in the PDF.  Visio diagram has X number of pages should equal X number of pages in the pdf. I am not a big Office macro writer and was trying to piece this together from several posts from here and other articles I found on the web.  The code I posted works on only one page at a time, I have tried to make changes based on others input, but no joy.

wapperdude

If you do as Paul suggests, namely, replace every "Application.ActivePage" with "pg", your looping variable, then code should run fine.  You can also lose the shpPg code line as it doesn't do anything.
Visio 2019 Pro

wheatman

Ok, so I followed suggestion. It work for this part

'//Dim UndoScopeID1 As Long
    '//UndoScopeID1 = Application.BeginUndoScope("Page Setup")
    pg.Background = False
    pg.BackPage = ""
    pg.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesOnPage).FormulaU = "1"
    '//Application.EndUndoScope UndoScopeID1, True

But what do I need to change so it loops through every page for this to get set "Application.ActiveWindow.ViewFit = 1"

Current code>
Sub Macro2()

Dim pg As Visio.Page
Dim shpPg As Visio.Shape
For Each pg In ActiveDocument.Pages
Set shpPg = pg.PageSheet

    Application.ActiveWindow.ViewFit = 1

    '//Dim UndoScopeID1 As Long
    '//UndoScopeID1 = Application.BeginUndoScope("Page Setup")
    pg.Background = False
    pg.BackPage = ""
    pg.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesOnPage).FormulaU = "1"
    '//Application.EndUndoScope UndoScopeID1, True
Next pg


End Sub

wapperdude

As you loop thru each page, you have to make it the activepage. 

Sub Macro2()
    Dim pg As Visio.Page
'   Dim shpPg As Visio.Shape
   
    For Each pg In ActiveDocument.Pages
        ActiveWindow.Page = pg
        ActiveWindow.ViewFit = 1
        pg.Background = False
        pg.BackPage = ""
        pg.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesOnPage).FormulaU = "1"
    Next pg
End Sub
Visio 2019 Pro

wheatman

Genius, absolutely Genius! Thank you so much for the help!