900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > word中在指定位置插入图片

word中在指定位置插入图片

时间:2020-05-05 22:52:30

相关推荐

word中在指定位置插入图片

当然得在word中建立一些书签了,作用就是定位.再就是要有Microsoft Word 11.0 Object引用

这是段是精华

//添加应用 using Word=Microsoft.Office.Interop.Word;

// Microsoft Word 11.0 Object Library

//添加应用 using Word=Microsoft.Office.Interop.Word;

// Microsoft Word 11.0 Object Library

public void insert(string fileName, string ImageName)

{

try

{

object missing = System.Reflection.Missing.Value;

//建立程序对象

Word._Application app = new Word.ApplicationClass();

//打开文档

object file = fileName;

Word._Document doc = app.Documents.Open(ref file, ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing);

//查找标签

if (doc.Bookmarks.Count > 1)

{

int i = doc.Bookmarks.Count;

Word.Bookmark bmk;

object item;

object start;

object end;

while (i > 0)

{

item = i;

bmk = doc.Bookmarks.get_Item(ref item);

start = bmk.Start;

end = bmk.End;

//用图片替换标签

doc.Range(ref start, ref end).InlineShapes.AddPicture(ImageName, ref missing, ref missing, ref missing);

i--;

}

//保存

doc.Save();

//退出

app.Quit(ref missing, ref missing, ref missing);

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

//这下面是我的所有代码

using System;

using System.Collections.Generic;

using ponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;

namespace WordImage

{

public partial class Form1 : Form

{

private Word._Application app = new Word.ApplicationClass();

private object missing = System.Reflection.Missing.Value;

private Word.Document doc ;

public Form1()

{

InitializeComponent();

app.Visible = false;

}

private void btnOpenFile_Click(object sender, EventArgs e)

{

if (openFileDialog.ShowDialog() == DialogResult.OK)

{

textBoxFile.Text = openFileDialog.FileName;

}

}

private void btnOpemImage_Click(object sender, EventArgs e)

{

if (openFileDialog.ShowDialog() == DialogResult.OK)

{

textBoxImage.Text = openFileDialog.FileName;

}

}

private void btnOpen_Click(object sender, EventArgs e)

{

if (textBoxFile.Text != string.Empty)

{

try

{

object filename = textBoxFile.Text;

doc = app.Documents.Open(ref filename, ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing,

ref missing);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

private void btnWrite_Click(object sender, EventArgs e)

{

if (textBoxFile.Text != string.Empty)

{

try

{

object filename = textBoxImage.Text;

object start =0;

object end = 0;

object reftrue = true;

object reffalse = false;

//doc.Range(ref start, ref end).Text = "123";

doc.Shapes.AddPicture(textBoxImage.Text, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

//doc.Range(ref start, ref end).InlineShapes.AddPicture(textBoxImage.Text,ref reffalse,ref reftrue,ref missing);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

private void btnSave_Click(object sender, EventArgs e)

{

try

{

doc.Save();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void btnExit_Click(object sender, EventArgs e)

{

try

{

app.Quit(ref missing, ref missing, ref missing);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void btnReplace_Click(object sender, EventArgs e)

{

try

{

object item = 1;

object reftrue = true;

object reffalse = false;

Word.Bookmark bmk = doc.Bookmarks.get_Item(ref item );

object rmk = bmk;

object start = bmk.Start;

object end = bmk.End;

//doc.Shapes.AddPicture(textBoxImage.Text, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref rmk);

doc.Range(ref start, ref end).InlineShapes.AddPicture(textBoxImage.Text, ref reffalse, ref reftrue, ref missing);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。