Word-打字后面的字会被覆盖

可能就是因为你按了键盘上的Insert键,再按一次就好了
阅读更多

C#插入图片后再插入文字后图片会被覆盖

11-01

[b][b][b]最近做实验室的项目遇到个难题,在WORD中插入图片,如果插入图片在WORD中的最后一行,不会有问题,但是如果插入到别的位置,后面再插入的话就会被覆盖掉,不知道怎么回事,下面贴上自己的代码,高分急求,有会的请留言或者加我的QQ:851959715,请主动和我说话。rnusing System;rnusing System.Collections.Generic;rnusing System.Linq;rnusing System.Text;rnusing MSWord = Microsoft.Office.Interop.Word;rnusing System.IO;rnusing System.Reflection;rnrnrnrnnamespace Automatic_CreateWordrnrn class Programrn rn static void Main(string[] args)rn rn object path; //文件路径变量rn string strContent; //文本内容变量rn MSWord.Application wordApp; //Word应用程序变量rn MSWord.Document wordDoc; //Word文档变量rn rn path = @"C:\Automatic_CreatedWord.doc"; //路径rn wordApp = new MSWord.ApplicationClass(); //初始化rn //如果已存在,则删除rn if (File.Exists((string)path))rn rn File.Delete((string)path);rn rn //由于使用的是COM库,因此有许多变量需要用Missing.Value代替rn Object Nothing = Missing.Value;rn wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);rn strContent = "1 范围\n";rn wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";rn wordDoc.Paragraphs.Last.Range.Font.Bold = 1;rn wordDoc.Paragraphs.Last.Range.Font.Size = 15;rn wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;rn rn wordDoc.Paragraphs.Last.Range.Text = strContent;rnrn strContent= " 1.1 标识\n 1.2 系统概述\n 1.3 文档的概述\n";rn wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";rn wordDoc.Paragraphs.Last.Range.Font.Bold = 0;rn wordDoc.Paragraphs.Last.Range.Font.Size = 13;rn wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;rn wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn strContent = "2 引用文档\n";rn wordDoc.Paragraphs.Last.Range.Font.Name = "楷体";rn wordDoc.Paragraphs.Last.Range.Font.Bold = 1;rn wordDoc.Paragraphs.Last.Range.Font.Size = 15;rn wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorGreen;rn wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn strContent = " 引用的文档\n";rn rn wordDoc.Paragraphs.Last.Range.Text = strContent;rnrn strContent = "3 术语应用\n";rn wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorRed;rn wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn strContent = " 术语的应用\n";rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn strContent = "2 程序系统组织结构\n 4.1 程序运行过程\n 程序的运行过程 \n 4.2 程序逻辑\n 插图片有问题\n";rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn strContent="5 组织导航软件详细设计(中断和主函数内部每一个函数都需要\"函数功能描述\")\n";rn wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlack;rn wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn string filename = @"c:\TEST.jpg";rn Object range = wordDoc.Paragraphs.Last.Range;rn Object linkToFile = false; //默认rn Object saveWithDocument = true; //默认rn //使用InlineShapes.AddPicture方法插入图片rn wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);rn object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;rn object count = 30;rn wordApp.Selection.MoveEnd(ref unit, ref count);rn strContent = " 5.1 中断描述\n 中断的描述 \n 5.2 主程序 \n 主的程序\n";rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn strContent = "6 需求可追踪性\n";rn wordDoc.Paragraphs.Last.Range.Text = strContent;rn int lenght = wordDoc.Characters.Count-1;rn object start = lenght;rn object end = lenght;rn //表格起始坐标rn MSWord.Range tableLocation = wordDoc.Range(ref start, ref end);rn //添加Word表格   rn MSWord.Table table = wordDoc.Tables.Add(tableLocation, 5, 5, ref Nothing, ref Nothing);rnrn rn //默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框rn table.Borders.Enable = 1;rn //使用两层循环填充表格的内容rnrn table.Cell(1, 1).Range.Text = "序号";rn table.Cell(1, 2).Range.Text = "概要设计章节";rn table.Cell(1, 3).Range.Text = "概要设计项";rn table.Cell(1, 4).Range.Text = "详细设计章节";rn table.Cell(1, 5).Range.Text = "详细设计项";rn rn object format = MSWord.WdSaveFormat.wdFormatDocument;rn wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);rn //关闭wordDoc文档对象 rn wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);rn //关闭wordApp组件对象 rn wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);rn Console.WriteLine(path + " 创建完毕!");rnrnrnrnrnrn rn rnrn如上面所示,后面插入文字后,图片就会被覆盖。求高人解答[/b][/b][b][/b][/b]

没有更多推荐了,返回首页