Powerpoint VBA Undo

Viewed 283

I have VBA code which processes many slides in a PowerPoint file. Part of the code temporarily resizes the slides so I can export images in a different aspect ratio.

I cannot resize them back again because PowerPoint never puts their contents back exactly the way it was before the resize, and so I execute

Application.StartNewUndoEntry

before I do the resize, and then

Application.CommandBars.ExecuteMso "Undo"

after I export to undo the resize and ensure things all go back exactly the way they were before the resize.

I then write some metadata to the slides notes page and repeat for the next slide.

The problem is that

Application.CommandBars.ExecuteMso "Undo"

is not blocking until the undo completes. This means the writing to the notes page (which is after then Application.CommandBars.ExecuteMso "Undo") gets undone also!

How can I get PowerPoint to wait until it has finished Application.CommandBars.ExecuteMso "Undo" before executing the code that writes to the notes page?

I have tried putting in a wait and a sleep of up to a couple of seconds even, between the undo and my other code, but it seems this doesn't help at all. It still undos trhe code I execute AFTER the call to executeMso "Undo"

(I have also thought of storing the ids of the slides i need ot update the notes pages with until aftger all the resizing and exporting is finished, and then going an updateing all the notes pages, but I suspect that would be a waste of time ebcasue the first one (or more) will still get lost due to the last undo not finishing its work.)

(I have also tried moving the notes updating code to before I resize (and then undo) the slide - but this doesn't work because it just moves the problem to the previous slide undo not finishing, rather than the current one.)

1 Answers

Created a workaround that works by waiting for the slide size to return to what it was before the resize happened..

i = 0
Do While ActivePresentation.PageSetup.SlideWidth <> dOldSlideWidth And i <= 100
    i = i + 1
    Sleep 100
    DoEvents
Loop
If i >= 100 Then MsgBox "Gave up after waiting 100 times for the slidewidth to revert after Undo"
oSi.sQHash = SlideInfoCreateQHASH(oSi)
SlideInfoWrite oSl, oSi

Sometimes it waits 1 or 2 times before it detects the undo has happened. Other times it waits for up to 40 iterations (4 seconds). Once it stopped itslef after 100 iterations (10 seconds).

Well, its not pretty but it works!!!

Related