起因
有些PPT文件,视频没有打包在文件中,而是引用了本地的文件。在复制PPT时,有时会遗漏了视频文件。之前我经常给同事处理这个问题,就写了这个代码,用于检查PPT中的视频。代码比较简单 就是就2个遍历。
先循环 slides,foreach (PPT.Slide slide in slides)
再循环 shapes foreach (PPT.Shape shape in slide.Shapes)
小细节 关闭Microsoft Office 安全选项
有时 会弹出 Microsoft Office 安全选项的窗体,可以用以下代码进行关闭。
try
{
System.Threading.Thread.Sleep(100);
IntPtr h = IntPtr.Zero;
h = FindWindow(null, "Microsoft Office 安全选项");
if (h != IntPtr.Zero)
{
ForceWindowIntoForeground(h);//置顶
CloseWindow(h);
}
else
{
}
}
catch (Exception)
{
}
代码
private void check_video(string ppt_filepath){if (ObjPression == null)return;string dir = System.IO.Path.GetDirectoryName(ppt_filepath);try{ObjPression.UpdateLinks();}catch { }try{ PPT.Slides slides = ObjPression.Slides;int c = slides.Count;int i = 0;foreach (PPT.Slide slide in slides){try{ foreach (PPT.Shape shape in slide.Shapes){try{ if (shape.LinkFormat != null){PPT.LinkFormat link = shape.LinkFormat;string filename = link.SourceFullName;string fn = System.IO.Path.Combine(dir, System.IO.Path.GetFileName(filename));if (System.IO.File.Exists(fn)){link.SourceFullName = fn;}else{warning_txt.AppendLine("没有找到"+ filename);}shape.LinkFormat.Update();}}catch (Exception e){// warning_txt.AppendLine(e.Message);}}}catch{}i = i + 1;RunProgress("正在检查视频音频(第"+(i+1).ToString()+"页)", i, c); }}catch (Exception e ){ }if (will_save_checked_file){try{ObjPression.SaveAs(checked_file_name);}catch (Exception){}}}