在我的vsto插件中,我有一些简单的计时器代码
private void MainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!dialogopen & Application.Documents.Count > 0)
{
var doc = Application.ActiveDocument;
Word.InlineShapes shps;
Word.Paragraphs pars;
try
{
pars = doc.Paragraphs;
}
catch (Exception)
{
return;
}
var pars2 = pars.Cast<Word.Paragraph>().ToList();
foreach (var obj in pars2)
{
if (obj.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText )//PROBLEM HERE
{
};
}
}
}一旦到达检查外列的线,即使我不做任何事情,活动纪念碑中的选择也会丢失。
当然,用户不能使用一个插件不断取消他的选择.
谷歌什么都没给我
谢谢
EDIT1
我试着创建一个static函数来检查样式,但是没有帮助。这是密码
static public class Helpers
{
static public Word.Paragraph checkPars(List<Word.Paragraph> pars)
{
return pars.FirstOrDefault();//(x) => x.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText);
}
}正如您所看到的,我必须删除实际的检查,因为它会导致光标闪烁并丢失选择。
请告知
发布于 2017-04-03 07:18:45
我们使用Application.ScreenUpdating = true;,这将保留对Paragraph中除Range属性以外的所有属性的选择。
然后,我们试图通过反射访问范围,这就是解决方案。
Range rng = (Range)typeof(Paragraph).GetProperty("Range").GetValue(comObj);发布于 2017-03-13 07:18:00
我们试图消除对ActiveDocument的质疑,认为这可能产生了导致问题的副作用,但事实并非如此。
然后,我们确认选择没有“丢失”,而屏幕更新是唯一的问题,所以我们尝试用Application.ScreenRefresh恢复UI,虽然它确实工作了,但是每次计时器启动时,它都会导致屏幕闪烁,这还不够好。
最后,知道这只是一个UI问题,我尝试简单地关闭Application.ScreenUpdating.
在ThisAddin中
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Timer timer = new Timer(2000);
timer.Elapsed += (s, t) =>
{
var scrnUpdating = Application.ScreenUpdating;
Application.ScreenUpdating = false;
MainTimer.onElapsed(Application, t);
if (scrnUpdating)
Application.ScreenUpdating = true;
};
timer.Start();
}在另一个类库中(注意它是静态的,我仍然认为这是最好的方法).
public static class MainTimer
{
public static void onElapsed (Word.Application state, System.Timers.ElapsedEventArgs e)
{
if (state.Documents.Count > 0)
{
var doc = state.ActiveDocument;
Word.InlineShapes shps;
Word.Paragraphs pars;
try
{
pars = doc.Paragraphs;
}
catch (Exception)
{
return;
}
var pars2 = pars.Cast<Word.Paragraph>()
.Where(p => p.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText)
.Select(p => p) // do stuff with the selected parragraphs...
.ToList();
}
}
}这对我来说很管用。所选内容保持不变,并显示在UI中,不闪烁。
我还从您的代码中消除了一些过早的枚举:您没有在.ToList()之前使用foreach。
https://stackoverflow.com/questions/42506356
复制相似问题