您的位置:首页 > 其它

word中在指定位置插入图片

2007-09-14 20:33 761 查看
当然得在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 System.ComponentModel;
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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: