none
VBA code working in code window but not from the powerpoint application RRS feed

  • Question

  • Hello,

    I created the below vba code for toggling the chart scale to auto or fixed. I used a wscript message box. The code is working from the code window. But If I run from the button from powerpoint window, the wscript message box doesn't close automatically with the timer given 2 seconds. Below is my code. Thanks for your great help.

    Sub AxisFixedorAuto()
    Const ShowDurationSecs As Integer = 2
    Dim wsWShell As Object
    Dim Rslt As Integer

    With ActiveWindow.Selection.ShapeRange(1).chart.Axes(xlValue)
    If .MinimumScaleIsAuto = False And .MaximumScaleIsAuto = False And .MajorUnitIsAuto = False And .MinorUnitIsAuto = False Then
        .MinimumScaleIsAuto = True
        .MaximumScaleIsAuto = True
        .MajorUnitIsAuto = True
        .MinorUnitIsAuto = True
        
        Set wsWShell = CreateObject("WScript.Shell")
        Rslt = wsWShell.PopUp("Scale - Auto", ShowDurationSecs, "Chart Scale", 0)

    - 'The above code works, if it is run in codewindow by F5 or F8. But if I run by clicking the button from powerpoint, the msgbox, doesn't close after the duration of 2 secs.

    Else
        .MinimumScaleIsAuto = False
        .MaximumScaleIsAuto = False
        .MajorUnitIsAuto = False
        .MinorUnitIsAuto = False
    End If
    Set wsWShell = Nothing
    End With
    End Sub

    Thanks and Regards,
    balumail75.

    Thursday, July 31, 2014 5:41 PM

All replies

  • Hi,

    What's the version of your PowerPoint?

    After testing in my PowerPoint 2013, 2010 and 2007, I failed to reproduce your issue. The code above works well as you said, but when I insert a command button into a slide, call the function "AxisFixedorAuto" in the click event of the button, then switch the presentation in Slide Show mode to click the button, it throw error in the line "With ActiveWindow.Selection..." since there is no Window object.

    After editing the code as followed, I can click the button to run the code successfully. But I find the popped up message box will close automatically after the duration of 2 secs.

    Sub AxisFixedorAuto()
     Const ShowDurationSecs As Integer = 2
     Dim wsWShell As Object
     Dim Rslt As Integer
    
     With ActivePresentation.Slides(2).Shapes(1).Chart.Axes(xlValue)
     If .MinimumScaleIsAuto = False And .MaximumScaleIsAuto = False And .MajorUnitIsAuto = False And .MinorUnitIsAuto = False Then
         .MinimumScaleIsAuto = True
         .MaximumScaleIsAuto = True
         .MajorUnitIsAuto = True
         .MinorUnitIsAuto = True
         
         Set wsWShell = CreateObject("WScript.Shell")
         Rslt = wsWShell.PopUp("Scale - Auto", ShowDurationSecs, "Chart Scale", 0)
    
    'The above code works, if it is run in codewindow by F5 or F8. But if I run by clicking the button from powerpoint, the msgbox, doesn't close after the duration of 2 secs.
    
    
    Else
         .MinimumScaleIsAuto = False
         .MaximumScaleIsAuto = False
         .MajorUnitIsAuto = False
         .MinorUnitIsAuto = False
     End If
     Set wsWShell = Nothing
     End With
     End Sub
    

    Since I cannot reproduce your issue, I suggest you testing in other machine. In addition, I'm not sure whether my steps are same as yours. You could sharing more details about your steps and the presentation for us to troubleshoot.

    I have uploaded my presentation to OneDrive, and you can compare it with yours.

    http://1drv.ms/1s91RzS


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 1, 2014 8:17 AM
    Moderator
  • Hi Luna,

    Thanks for the reply.

    it works with your code of "With ActivePresentation.Slides(2).Shapes(1).Chart.Axes(xlValue)" in the normal view itself. But I am trying to to do with the selected chart "With ActiveWindow.Selection.ShapeRange(1).chart.Axes(xlValue)" code.

    Please help me.

    Regards,
    Balumail75.


    Friday, August 1, 2014 1:38 PM
  • Hi,

    >>it works with your code of "With ActivePresentation.Slides(2).Shapes(1).Chart.Axes(xlValue)" in the normal view itself<<

    Yes, I knew it. But as I said, if I call your function "AxisFixedorAuto" in the click event of the button, then switch the presentation in Slide Show mode to click the button, it throw error since there is no Window object.

    As I know, it's hard to click the button to call its function in Normal Mode, do you click the button to call the function in the Slide Show mode? Since you said you could run it successfully, would you mind sharing your more detail steps for us to troubleshoot?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 4, 2014 1:12 AM
    Moderator