关闭

word中在指定位置插入图片

标签: objectexceptionmicrosoftapplicationlibrarystring
7447人阅读 评论(2) 收藏 举报

当然得在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);
            }
        }
    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:107625次
    • 积分:1380
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:4篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论