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 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);
            }
        }
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值