我们学校要求做一个c#的小程序,用c#连接word,在指定标签处添加文字后,我想在文字中间添加一个图片,但不知道怎么加,希望各位高手帮帮忙。这个是我找了好长时间的,但不是我想要的,日期就要到了,麻烦各位高手尽快啊。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Collections.Specialized;namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:\\Users\\dell3\\Desktop"); //创建文件所在目录
string name = "7.doc";
object filename = "C:\\Users\\dell3\\Desktop\\" + name; //文件保存路径
//创建Word文档
_Application WordApp = new ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(); //WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; //设置对齐方式
string text1 = richTextBox1.Text;
WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验目的\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = text1 + "\n";
//WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
WordDoc.Paragraphs.Last.Range.Bold = 2; //加粗
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验环境\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
WordDoc.Paragraphs.Last.Range.Text = richTextBox2.Text.ToString() + "\n";
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验原理\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Bold = 0;
//this.richTextBox3.Focus();
int a=this.richTextBox1.SelectionStart; //定义该插入的图片是否为外部链接
object linkToFile = false; //默认
//定义要插入的图片是否随Word文档一起保存
object saveWithDocument = true;
object range = WordDoc.Paragraphs.Last.Range;
if (Clipboard.ContainsFileDropList())
{
StringCollection sc = Clipboard.GetFileDropList();
for (int i = 0; i < sc.Count; i++)
{
string fileName = sc[i];
richTextBox1.Text = fileName; Image img = Image.FromFile(fileName);
Clipboard.Clear();
Bitmap bmp = new Bitmap(img);
Clipboard.SetImage(bmp);
richTextBox3.Paste();
// WordDoc.InlineShapes.AddPicture(fileName);
//object range = WordDoc.Paragraphs.Last.Range;
//Object range = WordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
// Object linkToFile = false; //默认
//定义要插入的图片是否随Word文档一起保存
//Object saveWithDocument = true; //默认
//object Anchor = this.richTextBox3.SelectionStart;
// object bkObj = "book";
// string bk;
//if (WordApp.ActiveDocument.Books.Exists(bk) == true)
// {
// WordApp.ActiveDocument.Books.get_Item(ref bkObj).Select();
// object oRng = WordDoc.Books.get_Item(ref bkObj).Range;
// object Anchor = WordDoc.Application.Selection.Range;
// WordDoc.InlineShapes.AddPicture(fileName, ref linkToFile, ref saveWithDocument, ref oRng);
// }
}
}
WordDoc.Paragraphs.Last.Range.Text = richTextBox3.Text.ToString() + "\n";
//MessageBox.Show("1"); //object Anchor = WordDoc.Application.Selection.Range; //string path = @"C:\Users\dell3\Desktop\新建文件夹 (18)\登陆界面\0.jpg";
//Clipboard.Clear();
//Bitmap bmp=new Bitmap(path);
//Clipboard.SetImage(bmp);
//richTextBox3.Paste();
//Clipboard.Clear();
//WordDoc.InlineShapes.AddPicture(path); //G_str_path = string.Format( //计算文件保存路径
//@"{0}{1}", G_FolderBrowserDialog.SelectedPath,+ ".doc");
// WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(richTextBox3.Paste());
//= richTextBox3.Paste + "\n";
//WordDoc.Paragraphs.Last.Range = richTextBox3.Focus()+ "\n";
//移动焦点并换行
//object count = 14;
object WdLine = WdUnits.wdLine;//换一行;
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验内容与要求\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Text = richTextBox4.Text.ToString() + "\n";
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验过程及结果分析\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Text = richTextBox5.Text.ToString() + "\n";
//WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
//WordDoc.Paragraphs.Last.Range.Text = "应收获书\n";
//WordApp.Selection.TypeParagraph(); //插入段落
//WordDoc.Paragraphs.Last.Range.Text = "应收获确认书\n";
WordDoc.SaveAs(filename); //保存文件
WordApp.Quit(); //结束程序 }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Collections.Specialized;namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:\\Users\\dell3\\Desktop"); //创建文件所在目录
string name = "7.doc";
object filename = "C:\\Users\\dell3\\Desktop\\" + name; //文件保存路径
//创建Word文档
_Application WordApp = new ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(); //WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; //设置对齐方式
string text1 = richTextBox1.Text;
WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验目的\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = text1 + "\n";
//WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
WordDoc.Paragraphs.Last.Range.Bold = 2; //加粗
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验环境\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
WordDoc.Paragraphs.Last.Range.Text = richTextBox2.Text.ToString() + "\n";
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验原理\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Bold = 0;
//this.richTextBox3.Focus();
int a=this.richTextBox1.SelectionStart; //定义该插入的图片是否为外部链接
object linkToFile = false; //默认
//定义要插入的图片是否随Word文档一起保存
object saveWithDocument = true;
object range = WordDoc.Paragraphs.Last.Range;
if (Clipboard.ContainsFileDropList())
{
StringCollection sc = Clipboard.GetFileDropList();
for (int i = 0; i < sc.Count; i++)
{
string fileName = sc[i];
richTextBox1.Text = fileName; Image img = Image.FromFile(fileName);
Clipboard.Clear();
Bitmap bmp = new Bitmap(img);
Clipboard.SetImage(bmp);
richTextBox3.Paste();
// WordDoc.InlineShapes.AddPicture(fileName);
//object range = WordDoc.Paragraphs.Last.Range;
//Object range = WordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
// Object linkToFile = false; //默认
//定义要插入的图片是否随Word文档一起保存
//Object saveWithDocument = true; //默认
//object Anchor = this.richTextBox3.SelectionStart;
// object bkObj = "book";
// string bk;
//if (WordApp.ActiveDocument.Books.Exists(bk) == true)
// {
// WordApp.ActiveDocument.Books.get_Item(ref bkObj).Select();
// object oRng = WordDoc.Books.get_Item(ref bkObj).Range;
// object Anchor = WordDoc.Application.Selection.Range;
// WordDoc.InlineShapes.AddPicture(fileName, ref linkToFile, ref saveWithDocument, ref oRng);
// }
}
}
WordDoc.Paragraphs.Last.Range.Text = richTextBox3.Text.ToString() + "\n";
//MessageBox.Show("1"); //object Anchor = WordDoc.Application.Selection.Range; //string path = @"C:\Users\dell3\Desktop\新建文件夹 (18)\登陆界面\0.jpg";
//Clipboard.Clear();
//Bitmap bmp=new Bitmap(path);
//Clipboard.SetImage(bmp);
//richTextBox3.Paste();
//Clipboard.Clear();
//WordDoc.InlineShapes.AddPicture(path); //G_str_path = string.Format( //计算文件保存路径
//@"{0}{1}", G_FolderBrowserDialog.SelectedPath,+ ".doc");
// WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(richTextBox3.Paste());
//= richTextBox3.Paste + "\n";
//WordDoc.Paragraphs.Last.Range = richTextBox3.Focus()+ "\n";
//移动焦点并换行
//object count = 14;
object WdLine = WdUnits.wdLine;//换一行;
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验内容与要求\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Text = richTextBox4.Text.ToString() + "\n";
WordDoc.Paragraphs.Last.Range.Bold = 2;
WordDoc.Paragraphs.Last.Range.Font.Size = 20; //设置字号大小
WordDoc.Paragraphs.Last.Range.Text = "实验过程及结果分析\n";
WordApp.Selection.TypeParagraph(); //插入段落
WordDoc.Paragraphs.Last.Range.Font.Size = 13; //设置字号大小
WordDoc.Paragraphs.Last.Range.Bold = 0;
WordDoc.Paragraphs.Last.Range.Text = richTextBox5.Text.ToString() + "\n";
//WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
//WordDoc.Paragraphs.Last.Range.Text = "应收获书\n";
//WordApp.Selection.TypeParagraph(); //插入段落
//WordDoc.Paragraphs.Last.Range.Text = "应收获确认书\n";
WordDoc.SaveAs(filename); //保存文件
WordApp.Quit(); //结束程序 }
}
}
http://www.cnblogs.com/starstarfire/archive/2006/05/24/408422.html
//插入图片
public void InsertPicture(string book, string picturePath, float width, float height, ref Word2007.Document wordDoc)
{
if (!File.Exists(picturePath)) return;
object miss = System.Reflection.Missing.Value;
object oStart = book;
Object linkToFile = false; //图片是否为外部链接 Object saveWithDocument = true; //图片是否随文档一起保存 object range = wordDoc.Books.get_Item(ref oStart).Range;//图片插入位置
Word2007.InlineShape picShape = wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);
picShape.Width = width;//设置图片宽度
picShape.Height = height;//设置图片高度
}
其实我知道把图片插入标签的方法,但是向下面创建的对象不能在函数声明,不然提示我“字段初始值设定项无法引用非静态字段、方法或属性”。
_Application WordApp = new ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add();如果不在这个插入函数内声明的话就会提示我上下文中不存在WordDoc,如果在每个函数内都创建这么一个对象那就不是在一个文档里了,保存不下来信息,我该怎么才能让每个函数都能使用同一个对象。
还有一个问题,我是用载入模拟板的方式创建的word文档(其实实际的代码不是这个,这个只是用来研究怎么才能把图片插入到文字中间),每个richtextbox都被标签接收,我怎么才能在我黏贴图片的时候创建一个标签用来插入这个图片啊。
打完收工。
这个光标位置指的是我的richtextbox里的光标位子,还是我创建的word文档之中的,我以前试过插入光标位置,但是都是插入在word文档的最开头,难道是我设置错误了。