如何用C#编写,向Word的指定位置插入图片,图片版式要求是嵌入型
我的代码如下,可是图片的格式和位置怎么也不对,我想让图片插在数字974后面,另外版式也是不对,,枚举里wdWrapInline和wdWrapNone怎么都是"文字之上"呢?
请问各位大哥,分不多.
private void button2_Click(object sender, EventArgs e)
{
try
{
killWordProcess();//结束WORD进程
Object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:\\罐笼计算"); //创建文件所在目录
string name = "罐笼计算文档" + DateTime.Now.ToShortDateString() + ".doc";//18:28
object filename = "C:\\罐笼计算\\" + name; //文件保存路径
//创建Word文档
Word._Application WordApp = new Word.ApplicationClass();
Word._Document WordDoc = new Word.DocumentClass();
WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//出现提示信息,因为前面必须加_
WordDoc.Activate();
Word.Paragraph oPara1;
oPara1 = WordDoc.Content.Paragraphs.Add(ref Nothing);
oPara1.Range.Text = "1.5吨双层四车四绳罐笼计算方法";
//oPara1.Range.Font.Bold = 5;
oPara1.Range.Font.Size = 12;
oPara1.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
oPara1.Range.InsertParagraphAfter();
Word.Paragraph oPara2;
oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2= WordDoc.Content.Paragraphs.Add(ref oRng);
oPara2.Range.Text = "荷重:MG1.7-9B 1.5米固定式矿车单重974kg,4辆矿车总重974";//图片插在这个位置(图片是个乘号)
//oPara2.Range.Font.Bold = 5;
oPara2.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
//oPara2.Range.InsertParagraphAfter();
/**/
//插入图片
string FileName = "F:\\罐笼\\Image\\乘号.JPG";//图片所在路径
object LinkToFile = true;
object SaveWithDocument = true;
//Object abc=WordApp.Selection.Range;
oRng = WordDoc.Paragraphs[2].Range;
//Word.Range wrdpic = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;//WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;//WordDoc.Range(ref WordDoc.Paragraphs[10].Range.End, ref WordDoc.Paragraphs[10].Range.End);//注意Range的用法
WordDoc.Words.Last.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref oRng )
//WordApp.Selection.Bookmarks.get_Item(ref oEndOfDoc).Range.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref oRng);
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 10f;//图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 10f;//图片高度
//设置图片版式
Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = Word.WdWrapType.wdWrapInline;//.wdWrapNone;//.wdWrapSquare;.wdWrapTopBottom;//.wdWrapTight;//.wdWrapThrough;//
//文件保存
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
MessageBox.Show(name + "文档生成成功,以保存到C:'\'罐笼计算'\'下");
}
catch
{
MessageBox.Show("文件导出异常!");
}
}