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

word中在指定位置插入图片相关推荐

  1. PDF中在指定位置插入图片

    public static void pdfInsertPhoto()throws Exception{//pdf路径File file = new File("D:\\file\\888. ...

  2. java word添加图片_Java 替换word文档文字并指定位置插入图片

    先说下 需要的依赖包 org.apache.poi poi-excelant 3.12 org.apache.poi poi-scratchpad 3.12 org.apache.poi poi-oo ...

  3. 用 Python 自动生成 Word 文档并在指定位置插入图片

    用Python生成Word文档 在指定位置插入图片 首先当然需要第三方库啦(▽) 天才第一步,第三方的库O(∩_∩)O哈哈~ 参照转发的是这位大佬的博客:https://blog.csdn.net/c ...

  4. 记录使用Docx4j向word文档的指定位置插入图片遇到的坑

    使用Docx4j向指定位置插入图片 pom.xml <dependency><groupId>org.docx4j</groupId><artifactId& ...

  5. java pdf添加图片_java实现在pdf模板的指定位置插入图片

    本文实例为大家分享了java在pdf模板的指定位置插入图片的具体代码,供大家参考,具体内容如下 java操作pdf有个非常好用的库itextpdf,maven: com.itextpdf itextp ...

  6. java pdf域插入img_java实现在pdf模板的指定位置插入图片

    本文实例为大家分享了java在pdf模板的指定位置插入图片的具体代码,供大家参考,具体内容如下 java操作pdf有个非常好用的库itextpdf,maven: com.itextpdf itextp ...

  7. java pdf 插入图片_java实现在pdf模板的指定位置插入图片

    本文实例为大家分享了java在pdf模板的指定位置插入图片的具体代码,供大家参考,具体内容如下 java操作pdf有个非常好用的库itextpdf,maven: com.itextpdf itextp ...

  8. python 在表格指定位置插入图片

    在表格指定位置插入图片 import xlsxwriter file = '数据' + '.xlsx' # 保存表格名 pic_path = 'image/1.png' # 图片路径 workbook ...

  9. python替换指定位置word图片_用Python在word的指定位置插入图片(使用Python-docx包)...

    1 2 首先,本实例是采用java语言的,需要进行xml操作,至于poi,docx4j这些,因为感觉不是文档不太好,要嘛就是操作不方便,而且英文就算了,主要是注释很少,让人云里雾里的.所以,嘿嘿,直接 ...

最新文章

  1. MongoDB学习笔记(入门)
  2. Cocos2d-x 截屏功能集成
  3. 从搜索引擎到社交网络的艰难转变
  4. fastbin attack攻击中关于 malloc__hook
  5. 不断注册账号能挤满对方服务器吗,为什么不能注册啊
  6. 2009年SOA七大预测:SOA借力云计算
  7. 领域驱动设计,让程序员心中有码(五)
  8. java 任务池_多线程的应用-异步任务线程池的简单实现
  9. ajax清除session,跳出iframe框架页面后跳转页面
  10. javaShop JAVA版多用户B2B2C商城源码(PC +H5+小程序+APP)
  11. 计算机病毒的入侵路径,[浅谈VBS脚本病毒入侵计算机的途径与防治] 计算机病毒是指...
  12. 4月9本最新程序员专业书:Go语言、深度学习、量子计算等与您相约
  13. win10打印机无法打印(怎样使用打印机打印)
  14. 洛谷刷题C语言:第一次,第二次,成交!、Bessie‘s Secret Pasture S、金币、Bookshelf B、东南西北
  15. 中文语音评测之thchs30+gop_speechocean762实现
  16. 【微信公众号开发】四、公众号按钮设置及自己的微信按钮编辑器
  17. STM32单片机控制A1333角度传感器磁编码器
  18. 睿智的目标检测20——利用mAP计算目标检测精确度
  19. MDN和IMSI码详细解释
  20. 联想移动员工内幕消息,资深评论

热门文章

  1. Line推出新语音群聊功能 最多支持200人
  2. 学习OpenCV:滤镜系列(8)——素描
  3. python docx修改word文档格式
  4. 二级路由当作交换机,与一级路由同一个局域网
  5. ipad iphone开发_如何在iPhone或iPad上随机播放或重复播放歌曲
  6. 基于Hough变换的人眼虹膜定位
  7. SIMBOSS:物联网业务如何应用领域驱动设计?
  8. 怎么python画好几朵玫瑰花_使用Python画玫瑰花
  9. matlab编程999玫瑰花,网上收到的用matlab画玫瑰花的代码怎么不行啊,报告错误,求大神...
  10. 数据库的应用之(智慧城市)