word中在指定位置插入图片

原创 2007年09月14日 20:33:00

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

office插件开发shapes.addpicture插入图片保存原始大小

// 就是最后两个参数(红色显示)传-1即可,在excel2007中验证通过,其他未验证。 CComPtr pShape = pShapes->AddPicture(bstrPicName, mso...
  • Alsmile
  • Alsmile
  • 2014年02月28日 11:12
  • 4645

word操作类

public class WordUtility { private _Application wordApp = null; private _Docume...
  • jiushidt
  • jiushidt
  • 2015年11月17日 09:15
  • 1043

DocX类操作WORD文档——把图片插入到书签指定位置

  • 2016年07月25日 09:19
  • 173KB
  • 下载

word中在指定位置插入图片

当然得在word中建立一些书签了,作用就是定位.再就是要有Microsoft Word 11.0 Object引用 这是段是精华 //添加应用 using Word=Microsoft.Office....
  • u9999
  • u9999
  • 2007年09月14日 20:33
  • 8306

java2Word在Word任意位置插入图片

  • 2015年07月13日 21:30
  • 37KB
  • 下载

黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作

黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作       最近在做考试系统,说是要将试卷导出到word文档中,好方便教师打印,其实考试系统这个已经是别人做烂的系统...
  • anlun
  • anlun
  • 2015年02月02日 09:08
  • 822

使用VC向word中插入图片

 发表于:2008-11-23 14:13:2913楼 得分:0 C/C++ code#pragma   warning(disable:4786)          #import   "C:/Pr...
  • zzz822163
  • zzz822163
  • 2008年11月23日 14:16
  • 3565

vc操作word在指定位置插入图片

  • 2013年03月25日 09:29
  • 7.36MB
  • 下载

java2Word在Word文档指定位置插入图片或者文字

由于项目需要,苦苦对Java操作Word进行了一番研究,包括当下比较流行的java2Word、jacob、POI、itext等,只能说各有千秋,但是没有一个是完全适合我的,没有办法,只能去各种尝试,在...
  • baochanghong
  • baochanghong
  • 2016年08月11日 13:07
  • 4352

java2Word在Word文档指定位置插入图片或者文字

由于项目需要,苦苦对Java操作Word进行了一番研究,包括当下比较流行的java2Word、jacob、POI、itext等,只能说各有千秋,但是没有一个是完全适合我的,没有办法,只能去各种尝试,在...
  • abelzyp
  • abelzyp
  • 2015年07月13日 21:23
  • 3592
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:word中在指定位置插入图片
举报原因:
原因补充:

(最多只允许输入30个字)