1. 程式人生 > 實用技巧 >C#多程序開啟PPT的方法

C#多程序開啟PPT的方法

1、背景

PPT檔案開啟和操作是在一個程序中進行的,如果對多個PPT進行操作,PowerPoint程序預設會以阻塞的方式依次進行,如果開啟的PPT特別大(比如超過1GB)很容易造成PPT無響應,這樣幾乎所有的PPT操作都無法進行。

解決PPT無響應的一種方式是定時檢測PPT程序(POWERPNT.exe)是否無響應,如果無響應就將POWERPNT.exe程序Kill掉,重新開啟PPT。這種方式並不能解決需要多個PPT操作的問題,如果多個PPT檔案都很大,操作多個PPT會頻繁出現PPT無響應的情況。

解決PPT無響應的另一種方式是使用多程序方式開啟PPT。PPT預設是無法使用多個程序開啟的,在使用不同使用者開啟PPT的情況下,可以開啟不同的PPT程序,其原因見參考文件。

2、具體方法

使用多使用者開啟PPT的方法,首先需要內建不同的使用者,這可以通過程式碼實現(需要管理員許可權),也能通過Windows使用者設定新增指定的使用者。使用程式碼操作使用者稍後會有參考程式碼。

其次是使用指定使用者(及密碼)啟動封裝好PPT操作的程序,然後通過程序間通訊的方式(管道、COPYDATA 訊息、MQTT,推薦管道或者COPYDATA訊息,不用依賴第三方庫)進行PPT控制(開啟、翻頁、跳轉、媒體控制等)。

3、參考程式碼

(1)使用指定使用者開啟程序的程式碼片段(args引數可以傳遞管道名稱或者自定義訊息)

public void StartProcess(string
fileName, string userName, string password, string args) { var pwd = new SecureString(); if (!string.IsNullOrEmpty(password)) { password.ToCharArray().ToList().ForEach(c => pwd.AppendChar(c)); } try { var process = new Process { StartInfo
= { UseShellExecute = false, LoadUserProfile = true, UserName = userName, Password = pwd, Domain = ".", FileName = $"\"{fileName}\"", Arguments = args } }; process.Start(); } finally { pwd.Dispose(); } }

(2)PPT操作片段

private void OpenPpt()
{
    var app = new Microsoft.Office.Interop.PowerPoint.Application();
}

private void ClosePpt(ref Application app)
{
    try
    {
        app?.Quit();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }

    app = null;
    GC.Collect();
}

public void Open(object app, string filePath)
{
    var ppt = app as Microsoft.Office.Interop.PowerPoint.Application;
    if (ppt == null)
    {
        return;
    }

    //正斜槓替換為反斜槓
    filePath = filePath.Replace('/', '\\');

    try
    {
        //以只讀方式開啟,方便操作結束後儲存
        //使用一個不可能為密碼的值(AssemblyGuid)作為密碼開啟PPT,保證有密碼時開啟失敗
        var presentation = ppt.Presentations.Open(
            $"{filePath}::{Password}",
            MsoTriState.msoTrue, //ReadOnly: true
            MsoTriState.msoTrue, //Untitled: true
            MsoTriState.msoFalse); //WithWindow: false

        //獲取真實寬高
        var officeWidth = presentation.PageSetup.SlideWidth;
        var officeHeight = presentation.PageSetup.SlideHeight;

        //獲取頁數
        var pageNumber = presentation.Slides.Count;

        var slideShowSettings = presentation?.SlideShowSettings;
        if (slideShowSettings == null)
        {
            return;
        }

        var window = slideShowSettings.Run();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

public void Close(SlideShowWindow window)
{
    try
    {
        window?.View.Exit();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

public void GotoSlide(SlideShowWindow window, int index)
{
    try
    {
        window?.View.GotoSlide(index);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

public void Next(SlideShowWindow window)
{
    try
    {
        window?.View.Next();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

public void Previous(SlideShowWindow window)
{
    try
    {
        window?.View.Previous();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

4、參考文件

(1)我是如何讓PowerPoint能夠多程序同時匯出的