【问题标题】:Add custom XML using C# VSTO addon使用 C# VSTO 插件添加自定义 XML
【发布时间】:2021-02-11 05:45:12
【问题描述】:

我对 VSTO 插件开发和 C# 也很陌生。我有以下代码。

using Microsoft.Office.Tools.Ribbon;
using System;
using System.Diagnostics;

namespace POC_Powerpoint
{
  public partial class Ribbon1
  {
    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {

    }

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        Debug.WriteLine("POC Running");
        AddCustomXmlPartToPresentation()
    }

    private void AddCustomXmlPartToPresentation(PowerPoint.Presentation presentation)
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";

        Office.CustomXMLPart employeeXMLPart =
            presentation.CustomXMLParts.Add(xmlString, missing);
    }
 }
}

单击 button_1 后,我想将一些自定义 xml 添加到演示文稿中。而且我不知道如何运行此代码以及如何获取 Powerpoint 和 Office 类部分。

我从ms-office docs 中获取了这些代码。谁能帮我这个?如何将自定义 XML 插入到 powerpoint 文件中。

【问题讨论】:

    标签: c# visual-studio powerpoint vsto powerpoint-addins


    【解决方案1】:

    假设您能够单击该按钮,您的代码无论如何都不会工作,就像许多 MS 示例一样。您必须将演示文稿传递给AddCustomXmlPartToPresentation

    这可以通过从托管您的插件的应用程序实例中获取 ActiveDocument / ActivePresentation 来完成。

    一个很好的关于把它放在一起的教程是这个: HowTo: Create a Powerpoint AddIn in C#

    【讨论】:

    • @jidatevdocs 你能解释一下我如何用我的代码做到这一点
    • 从我的角度来看,您可以像这样在点击处理程序中调用AddCustomXmlPartToPresentationAddCustomXmlPartToPresentation(Globals.ThisAddin.Application.ActivePresentation)。也许您想在将参数传递给方法之前检查参数是否为 null。
    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    相关资源
    最近更新 更多