简体   繁体   中英

Select into view a slide master page with PowerPoint Interop

Is it possible in PowerPoint Interop used from C# to programatically select a slide master page into view, just like you would select a regular slide? Either by giving the ID of that master page or from the slide that has it as template.

I managed to switch the view to the slide master:

_pptApplication.ActiveWindow.ViewType = PpViewType.ppViewMasterThumbnails;

I tried selecting a slide first and then switching to the master view, but this instruction always places into view the first slide master page, not the one associated to the selected slide.

Likewise, I'd like to know if this is possible for notes, handouts and their masters.

You need to use the method .Select() on a CustomLayout object in addition to setting .ViewType as you had already figured out.

Here are two examples:

using NetOffice.OfficeApi.Enums;
using NetOffice.PowerPointApi.Enums;
using System;
using PowerPoint = NetOffice.PowerPointApi;

namespace ExportSlides
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var app = PowerPoint.Application.GetActiveInstance())
            {
                SelectSlideMasterLayoutOfActiveSlide(app);
                ActiveSlideMasterLayoutByIndex(app.ActivePresentation, 4);
            }
        }

        private static void ActiveSlideMasterLayoutByIndex(PowerPoint.Presentation activePresentation, int customLayoutIndex)
        {
            activePresentation.Windows[1].ViewType = PpViewType.ppViewSlideMaster; //PpViewType.ppViewMasterThumbnails doesn't work for me for some reason
            activePresentation.SlideMaster.CustomLayouts[customLayoutIndex].Select();
        }

        private static void SelectSlideMasterLayoutOfActiveSlide(PowerPoint.Application app)
        {
            var activeWindow = app.ActiveWindow;

            var slideObj = activeWindow.View.Slide;

            if (slideObj.GetType() == typeof(PowerPoint.Slide))
            {
                var slide = (PowerPoint.Slide)slideObj;

                activeWindow.ViewType = PpViewType.ppViewSlideMaster; //PpViewType.ppViewMasterThumbnails doesn't work for me for some reason

                slide.CustomLayout.Select();

            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM