0

I routinely produce visio documents with images on each page that I paste in from another app. The files are large so need compressing, which isn't fun by hand. I want to be able to loop through each page and compress any images a set amount (75%). So far I've got a sub that selects all images on a page and I've got a sub that uses send keys to bring up the image compression window and run compression at 75%. The problem is the two won't run together. SendKeys compression works fine if I select all images first then run compression separately. But executing in the same sub doesn't work. Code I'm using below. All I can think is that something is breaking the sendkeys rhythm because instead of compressing I'll get mc 75 inserted as text below the image, which is the sendkeys keys. Or I'm stringing them together in the wrong way.

If I run the following separately they work. ie manually executing each one

    'selects all images on a page
Sub SelectImagesOnPage()
    Dim vsoSelection As Selection
    'add all pictures to selection
    Set vsoSelection = ActivePage.CreateSelection(visSelTypeByType, visSelModeSkipSuper, visTypeSelBitmap)
    ActiveWindow.Selection = vsoSelection
End Sub

'uses sendkeys to bring up the compress image dialogue
Sub compressImageSelection()
    'should check an image is selected. 
    SendKeys "%(jp)", True 'holds ALT while pressing JP
    SendKeys "m", True
    SendKeys "c"
    SendKeys "{TAB}{TAB}"
    SendKeys "75{ENTER}"
End Sub

But putting the two together like this doesn't work.

Sub compressPicturesOnPage()
    
    'add all pictures to selection
    Call SelectImagesOnPage
    
    'compress
    Call compressImageSelection
    
End Sub

Update: Turns out that for the context sensitive 'Compress Image' button and 'Picture Format' tab to appear on the ribbon menu all Sub's have to complete. ie. visio has to 'regain control'. Blows a hole in what I'm trying to do but at least I know now! Possible solution by Jon Fournier in the comments to try.

Taylor
  • 1,700
  • 4
  • 17
  • 18
  • When you debug, does your selection object have shapes properly selected? Also, I've found that using the Sleep function can make stuff like this a little more reliable. – Jon Fournier Oct 16 '20 at 13:57
  • I'd tried a waiting function before thinking it might just need time. But what you said about whether the shapes are selected got me thinking. After a tedious amount of debugging it turns out that for the context sensitive 'Picture Format' tab to appear on the ribbon menu all Sub's have to complete. ie. visio has to 'regain control'. Blows a hole in what I'm trying to do but at least I know now! I'm going to see if there's a way of doing this in code somehow. – Taylor Oct 16 '20 at 18:16
  • Yikes, that is a tough one. For internal tools I've built exe's in VB6 that I shell to from VBA, so my code in VBA terminates, and then VB6 calls another function for me using ExecuteLine. I mean, that would technically work, but it's very hacky. – Jon Fournier Oct 19 '20 at 14:28
  • I may have a try of that, thanks. I've not built an exe before so will have to see how involved that is. What I've done as a compromise is add the two macros as shortcut actions to each image when pasting. Bit nicer to use, just not automated. – Taylor Oct 20 '20 at 15:26

0 Answers0