在 Word 中将 shape / inlineshape 设置为 Decorative

Set shape / inlineshape as Decorative in Word

在 Office 365 中,Microsoft 为 Shapes / InlineShapes(图片)引入了新的 属性:Decorative

我可以使用 VBA 操作它,如此处所示,但是 不能 使用 VSTO,这才是我真正需要的。

值类型是 Microsoft.Office.Core.MsoTriState,如果设置为 msoTrue,它会启用图片上的功能。

我已经能够使用 VBA 轻松切换此功能,如此处所示(要尝试,只需在 Microsoft Word 365 的空文档中插入内联图像)

Sub ToggleDecorativePic()
    Dim pic As InlineShape
    Set pic = ActiveDocument.InlineShapes(1)
    pic.Decorative = Not pic.Decorative
End Sub

这将切换活动文档中第一个 InLineShape 的装饰 属性。

但实际问题是:

我需要从 VSTOC#VB.NET)执行此操作,但是 属性 .Decorative 在 Intellisense 中不可用,甚至如果我尝试通过将 InlineShape 对象类型转换为 Dynamic 来强制执行它,它只会在执行代码时崩溃。

我正在使用这个 Office Interop nuGet 包与 Word 交互: Bundle.Microsoft.Office.Interop 版本 15.0.4569 日期 October 23rd 2018 所以它应该是最新的。 该捆绑包包含 Microsoft Excel、Outlook、PowerPoint 和 Word.

的 Interop 资源

我也试过使用 Microsoft.Office.Interop.Word 但是它已经有两年的历史了,所以它显然也不包含这个新功能。

我希望有人能在正确的方向上帮助我。

根据关于无障碍文档的新欧盟法律,使用 Microsoft Word 生成 PDF 文档时,这是一个 关键功能

或者:是否可以从 VSTO 以某种方式动态地执行它作为 VBA,而无需在文档等中创建宏?我猜不,至少在没有启用对对象模型的信任的情况下不会,这不是一个可行的选择:-/

确实是个有趣的问题。起初,我认为可以通过将对 Microsoft.Office.Interop.Word 的引用设置为 GAC 中的 PIA 而不是 Visual Studio (这是 VSTO 项目的默认源)安装的 PIA 并通过设置来解决"embed interop types" 为假。然而,两者都没有效果。

只剩下使用 PInvoke 的选项,这对我有用:

    Word.InlineShape ils = doc.InlineShapes[1];

    object oIls = ils;
    object isDec = oIls.GetType().InvokeMember("Decorative", System.Reflection.BindingFlags.GetProperty, null, oIls, null);

    System.Diagnostics.Debug.Print("Is the InlineShape marked as decorative: " + isDec.ToString());

    if ((int)isDec != -1)
    {
        object[] arg = new object[] { -1 };
        oIls.GetType().InvokeMember("Decorative", System.Reflection.BindingFlags.SetProperty, null, oIls, arg);
    }