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

Word 图片操作

一. 一张一张的处理 Sub 插入图片() Dim myfile As FileDialog Set myfile = Application.FileDialog(msoFileDialo...
  • cay22
  • cay22
  • 2015年08月20日 11:36
  • 963

C#中操作Word(6)—— 如何向Word中插入图片

一、Word对象模型的重叠性分析         本文主要介绍通过书签Bookmark向Word文档中插入图片的方法。在此之前我们先简单讨论下Word对象模型的重叠性。如果你对Word对象模型还不熟悉...

python 中matplotlib 画图导入到word中如何保证分辨率

python画图工具导入word中,尽量使图片不模糊。在把word转换成pdf时,保证图片不失真,pdf放大后图片不模糊。...

C#也能动态生成Word文档并填充数据

 要使用C#操作word,首先要添加引用:       1、添加引用->COM->Microsoft Word 11.0 Object Library       2、在.cs文件中添加using W...
  • mengyao
  • mengyao
  • 2007年09月13日 18:41
  • 9321

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

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

C# 在word文档指定位置插入图片

需求:         需要在word文档中固定位置插入,公章; 方法一:   object filename = filePath; //要打开的文档路径              stri...

AE导出图片并插入到word文档中某一固定位置

按矢量图层中各要素范围批量裁剪图片,并将图像粘贴到word中,将已经存在的图像批量粘贴到已存的word文档中。 void ClipPicToWord() {       ILayer pLaye...

java2Word在Word任意位置插入图片

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

在Word指定位置插入富文本域值(html文本)

遇到此问题,首先想到的就是各种百度。结果度娘了一会并没有发现有用的有效的解决方法,哎,看来还得靠自己啊。      首先整理了下手头上的资源,一是HtmlAgilityPack,专门解析Html...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:word中在指定位置插入图片
举报原因:
原因补充:

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