在文本框架中设置项目符号缩进的问题

时间:2019-02-24 23:02:09

标签: vsto powerpoint rulers

我正在尝试将一些现有的VBA代码移植到C#。一个例程控制项目符号的缩进,大致为:

indentStep = 13.5
For Each parag In shp.TextRange.Paragraphs()
    parag.Parent.Ruler.Levels(parag.IndentLevel).FirstMargin = indentStep * (parag.IndentLevel - 1)
    parag.Parent.Ruler.Levels(parag.IndentLevel).LeftMargin = indentStep * (parag.IndentLevel)
Next parag

该代码有效,但似乎是怪异的黑魔法。特别是,每次设置特定标尺的边距时,实际上都会设置“所有九个标尺”边距。 但是以某种方式设置了适当的信息。不幸的是,当您在C#中执行相同的操作时,结果会改变。以下代码没有可见效果:

const float kIndentStep = 13.5f;
foreach (PowerPoint.TextRange pg in shp.TextFrame.TextRange.Paragraphs())
{
pg.Parent.Ruler.Levels[pg.IndentLevel].FirstMargin = kIndentStep * (pg.IndentLevel - 1);
pg.Parent.Ruler.LevelS[pg.IndentLevel].LeftMargin = kIndentStep * pg.IndentLevel;
}

1 个答案:

答案 0 :(得分:0)

从C#中自动执行PowerPoint时,这似乎是一个限制/错误。我确认它可以在VBA中使用。

我确实在代码运行后看到了一种效果:每次运行都会更改 first 级别,以便最后,第一个级别具有应该分配给最后一个级别的设置待处理,但其他所有级别均未受到明显影响。我确实看到在代码执行过程中返回的值发生了变化,但是仅此而已。

如果代码仅更改了文本框的一个特定级别,则它可以工作。仅当尝试更改多个级别时,才会出现此问题。

我尝试了各种方法,包括后期绑定(PInvoke)和将更改放入单独的过程中,但结果始终相同。

这是我的最后一次迭代

Microsoft.Office.Interop.PowerPoint.Application pptApp = (Microsoft.Office.Interop.PowerPoint.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Powerpoint.Application"); // new Microsoft.Office.Interop.PowerPoint.Application();
//Change indent level of text
const float kIndentStep = 13.5f;
Microsoft.Office.Interop.PowerPoint.Shape shp = pptApp.ActivePresentation.Slides[2].Shapes[2];
Microsoft.Office.Interop.PowerPoint.TextFrame tf = shp.TextFrame;
object oTf = tf;
int indentLevelLast = 0;
foreach (Microsoft.Office.Interop.PowerPoint.TextRange pg in tf.TextRange.Paragraphs(-1, -1))
{
    int indentLevel = pg.IndentLevel;
    if (indentLevel > indentLevelLast)
    {
        Microsoft.Office.Interop.PowerPoint.RulerLevel rl = tf.Ruler.Levels[indentLevel];
        object oRl = rl;
        System.Diagnostics.Debug.Print(pg.Text + ": " + indentLevel + ", " + rl.FirstMargin.ToString() + ", " + rl.LeftMargin.ToString()) ;
        object fm = oRl.GetType().InvokeMember("FirstMargin", BindingFlags.SetProperty, null, oRl, new object[] {kIndentStep * (indentLevel - 1)});
        //rl.FirstMargin = kIndentStep * (indentLevel - 1);
        object lm = oRl.GetType().InvokeMember("LeftMargin", BindingFlags.SetProperty, null, oRl, new object[] { kIndentStep * (indentLevel) });
        //rl.LeftMargin = kIndentStep * indentLevel;
        indentLevelLast = indentLevel;
        System.Diagnostics.Debug.Print(pg.Text + ": " + indentLevel + ", " + tf.Ruler.Levels[indentLevel].FirstMargin.ToString() + ", " + tf.Ruler.Levels[indentLevel].LeftMargin.ToString()) ;
        rl = null;
    }
}

FWIW问题中提供的代码段都不会编译。 VBA代码段缺少.TextFrame。 C#代码片段不喜欢Parent.Ruler,因此我不得不将其更改为TextFrame.Ruler

相关问题