基于C#,vsto,开发 类似 active(ocx控件)的word 辅助工具
实现的功能是插了一个图片~
画圈的是插入的 UserControlForm,背景是图片。
能正确保存。
使用 word2007,能正常。
用word2010 双击打开 ,却不能正常加载。
但是。如果先打开 word2010,再使用 下图的功能
却正常。
求 解决方案~
不是很清楚怎么解决,但是建议你打开:文件夹选项\文件类型\DOCX(应该是这个后缀的文件吧)\高级\Open的命令行word2007和2011有什么不一样没,然后再进一步找原因。
希望能有帮助.
这是我封装的在PPT中插入元素的cs代码,没做整理,供参考:
using System;
using System.IO;
using Infrastructure;
using Infrastructure.FileHelper;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using VarEnum;
using Shape = Microsoft.Office.Interop.PowerPoint.Shape;
using Word = Microsoft.Office.Interop.Word;
namespace PowerPointAddIn.Vstor
{
/// <summary>
/// 幻灯片扩展_插入元素
/// </summary>
public static class InsertElement
{
static Word.Application wordApp;
static Word.Document wordDoc;
static Word.ContentControl contentControl;
//public enum AddPictureType
//{
// 常规, 有题可推, 未初始化
//}
///// <summary>
///// 添加图片
///// </summary>
///// <param name="slide"></param>
//public static void AddPicture(Slide slide, string filePath)
//{
//}
/// <summary>
/// 添加媒体
/// </summary>
public static void AddMedia(Slide slide, string filePath)
{
if (!File.Exists(filePath))
return;
try
{
Shape pic = slide.Shapes.AddMediaObject2(filePath, MsoTriState.msoFalse, MsoTriState.msoTrue);
}
catch
{
string ext = Path.GetExtension(filePath).Replace(".", string.Empty);
throw new Exception("暂不支持此[" + ext + "]格式添加到PPT");
}
}
/// <summary>
/// 添加http
/// </summary>
public static void AddHttp(Slide slide, string filePath)
{
if (!File.Exists(filePath))
return;
Shape pic = slide.Shapes.AddOLEObject(FileName: filePath);
}
#region 富文本
/// <summary>
/// 插入富文本 (使用word剪切板实现 有很大风险 暂时弃用)
/// </summary>
public static async void InsertRich(this Slide slide, string html)
{
//插入富文本控件
try
{
wordApp = wordApp ?? new Word.Application();
wordDoc = wordDoc ?? wordApp.Documents.Add();
contentControl = contentControl ?? wordDoc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText);
string path = await HtmlHelper.Save2TempFile(html);
contentControl.Range.InsertFile(path);
contentControl.Range.Copy();
ThisAddIn.application.ActiveWindow.View.PasteSpecial();
}
finally
{
contentControl.Range.Delete();
}
}
#endregion
#region 图片
/// <summary>
/// 插入图片
/// </summary>
/// <param name="slide"></param>
/// <param name="filePath"></param>
/// <param name="idName">这张图片的唯一名字</param>
public static void AddPicture(this Slide slide, string filePath, string name = "", int height = -1, int width = -1, int top = 0, int left = 0)
{
//没有设置宽高使用图片原始大小插入
Shape pic;
if (height > 0 || width > 0)
pic = slide.Shapes.AddPicture(filePath, MsoTriState.msoFalse, MsoTriState.msoTrue, left, top, width, height);
else
pic = slide.Shapes.AddPicture(filePath, MsoTriState.msoFalse, MsoTriState.msoTrue, left, top);
pic.Name = name;
pic.Left = left;
pic.Top = top;
pic.Width = width;
pic.Height = height;
}
/// <summary>
/// 移除形状 包含(图片)
/// </summary>
/// <param name="slide"></param>
/// <param name="name"></param>
public static void DeletePicture(this Slide slide, string name)
{
try
{
var Shape = slide.Shapes.Range(name);
Shape.Delete();
}
catch { }
}
#endregion
}
}
正文完