【问题标题】:Partial Underline check for a shape in Powerpoint not working?Powerpoint中形状的部分下划线检查不起作用?
【发布时间】:2019-04-21 10:23:02
【问题描述】:
ppShape.TextFrame.TextRange.Font.Underline == MsoTriState.msoTrue
ppShape.TextFrame.TextRange.Font.Underline == MsoTriState.msoFalse
ppShape.TextFrame.TextRange.Font.Underline == MsoTriState.msoTriStateMixed

上面的代码检查一个PowerPoint形状是否有... 1. 所有文字都加下划线 2.所有没有下划线的文字 3.部分文字加下划线

第三点,部分文本下划线不起作用,对于形状中的混合下划线文本,随机返回 false 或 true。

这对于粗体和斜体非常有效,即

ppShape.TextFrame.TextRange.Font.Bold == MsoTriState.msoTriStateMixed
ppShape.TextFrame.TextRange.Font.Italic == MsoTriState.msoTriStateMixed

我还在 GitHub 上向 Microsoft 提出了有关此问题的问题, https://github.com/MicrosoftDocs/VBA-Docs/issues/462

让我知道是否有任何方法可以解决此问题,或者至少有任何替代解决方法可以解决此问题???

【问题讨论】:

标签: c# powerpoint partial shapes underline


【解决方案1】:

作为一种解决方法,您可以检查 TextRange 中的每个 Run。在 VBA 中,您可以将形状传递给这样的函数:

Function IsUnderlined(oSh As Shape) As Boolean
    Dim oRng As TextRange
    For Each oRng In oSh.TextFrame.TextRange.Runs
        If oRng.Font.Underline Then
            IsUnderlined = True
            Exit Function
        End If
    Next
End Function

如果文本中的任何字符带下划线,该函数将返回 True。

【讨论】:

    【解决方案2】:

    感谢您阐明 TextRange.Runs 方法,这确实是一个很棒的功能,它还节省了很多性能而不是循环字符。

    我在 C#.Net 中创建了一个类似的函数,以便使用它类似于默认函数。

    using pp = Microsoft.Office.Interop.PowerPoint;
    using Microsoft.Office.Core;
    
    public static MsoTriState IsUnderlined(pp.Shape parShape)
    {
        int cntUnderline = 0;
    
        foreach (pp.TextRange textTR in parShape.TextFrame.TextRange.Runs())
        {
            if (textTR.Font.Underline == MsoTriState.msoTrue)
            {
                cntUnderline++;
            }
        }
    
        if (cntUnderline == 0)
        {
            //No Underline
            return MsoTriState.msoFalse;
        }
        else if (parShape.TextFrame.TextRange.Runs().Count == cntUnderline)
        {
            //All Underline
            return MsoTriState.msoTrue;
        }
        else if (parShape.TextFrame.TextRange.Runs().Count != cntUnderline)
        {
            //Mixed Underline
            return MsoTriState.msoTriStateMixed;
        }
    
        return MsoTriState.msoTriStateToggle; //Consider as error
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 2014-11-25
      • 2019-02-01
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多