对word文档的读取定位小问题

做对WORD文档读取时会经常对定位有问题,在msdn中有一个WordApp的小程序,本文就以它为例
准备:初学者对找WORD控件有很大问题,要在对office 2003中添加/更改对Net的支持
WordsupportNet.JPG
然后加入using Microsoft.Office.Interop.Word;
这样就可以引用word功能了
现在是 定位问题,要对某段字符进行改变
    start = 37; end =40;
    range=Word_doc.Range(ref start, ref end);
     range.Font.Bold=16;
    range.Font.Color=WdColor.wdColorLavender;
意思是将第37和40个字符 之间的字符修改bold 和color
注意: 换行键算一个字符,空格不算
下面全部给出源代码,大家可以在第130行修改试验
  1 None.gif using  System;
  2 None.gif using  System.Diagnostics;
  3 None.gif using  System.Runtime.InteropServices;
  4 None.gif using  System.Reflection;
  5 None.gif using  System.Collections;
  6 None.gif using  System.Threading;
  7 None.gif using  Microsoft.Office.Interop.Word;
  8 None.gif
  9 None.gif namespace  Microsoft.Samples.Interop.WordApp
 10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 11InBlock.gif    class WordAppMain
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        static object missing = Missing.Value;
 14InBlock.gif        static object missing2 = Missing.Value;
 15InBlock.gif        static object missing3 = Missing.Value;
 16InBlock.gif        static object missing4 = Missing.Value;
 17InBlock.gif        static object missing5 = Missing.Value;
 18InBlock.gif        static object missing6 = Missing.Value;
 19InBlock.gif        static object missing7 = Missing.Value;
 20InBlock.gif        static object missing8 = Missing.Value;
 21InBlock.gif        static object missing9 = Missing.Value;
 22InBlock.gif        static object missing10 = Missing.Value;
 23InBlock.gif        static object missing11 = Missing.Value;
 24InBlock.gif        static object missing12 = Missing.Value;
 25InBlock.gif        static object missing13 = Missing.Value;
 26InBlock.gif
 27InBlock.gif
 28InBlock.gif        static int Main()
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 30InBlock.gif            
 31InBlock.gif            WordAppMain myWord = new WordAppMain();
 32InBlock.gif            int return_Result = 0;
 33InBlock.gif            
 34InBlock.gif            // Create a word object that we can manipulate
 35InBlock.gif            Application Word_App = null;
 36InBlock.gif            Document Word_doc=null;
 37InBlock.gif            try
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 39InBlock.gif                Word_App = new Application();
 40InBlock.gif                Word_doc=new Document();
 41ExpandedSubBlockEnd.gif            }

 42InBlock.gif            catch(Exception e)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 44InBlock.gif                Console.WriteLine("Can't create a word document " + e.ToString());
 45InBlock.gif                return_Result = 1;
 46InBlock.gif                goto Exit;
 47ExpandedSubBlockEnd.gif            }

 48InBlock.gif
 49InBlock.gif            AutoCorrect autocorrect = Word_App.AutoCorrect;
 50InBlock.gif            AutoCorrectEntries autoEntries = autocorrect.Entries; 
 51InBlock.gif
 52InBlock.gif            string theEnd= "\nThe End";
 53InBlock.gif            autoEntries.Add("Inntroduction""Introduction");
 54InBlock.gif
 55InBlock.gif            Documents Docs = Word_App.Documents;
 56InBlock.gif            if (Docs == null)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 58InBlock.gif                Console.WriteLine("Docs is null");
 59ExpandedSubBlockEnd.gif            }

 60InBlock.gif            else
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{    
 62InBlock.gif                Console.WriteLine("Docs exists:" + Docs.Count);
 63ExpandedSubBlockEnd.gif            }

 64InBlock.gif
 65InBlock.gif            Word_App.Visible=true;
 66InBlock.gif            _Document my_Doc= (_Document) Word_doc;
 67InBlock.gif            Word_doc=Docs.Add(ref missing, ref missing, ref missing, ref missing);
 68InBlock.gif
 69InBlock.gif            object start = 0;
 70InBlock.gif            object end = 0;
 71InBlock.gif            Range range = Word_doc.Range(ref missing,ref missing);    
 72InBlock.gif            
 73InBlock.gif            // add text to the doc -- this contains some deliberate misspellings so that we can correct them in a short while
 74InBlock.gif            range.Text="M icrosoftWordInteroperability Sample\n\n\nInntroduction:\n\n\nMicrosoft.NETwillallowthe creation of truly distributed XML Web services. These services will integrate and collaborate with a range of complementary services to work for customers in ways that today's internet companies can only dream of. Microsoft .NET will drive the Next Generation Internet and will shift the focus from individual Web sites or devices connected to the Internet, to constellations of computers, devices, and services that work together to deliver broader, richer solutions.\nFor more info go to:\n   ";
 75InBlock.gif
 76InBlock.gif            // Wait so the starting state can be admired
 77InBlock.gif            Thread.Sleep(2000);
 78InBlock.gif
 79InBlock.gif            // Format the title
 80InBlock.gif            Font fc= new Font();
 81InBlock.gif            try
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                Console.WriteLine("Formatting the title");
 84InBlock.gif                start = 0; end = 40;
 85InBlock.gif                range=Word_doc.Range(ref start, ref end);
 86InBlock.gif                range.Font.Size=24;
 87InBlock.gif                range.Font.Bold=1
 88InBlock.gif                range.Font.Color=WdColor.wdColorGray30;
 89InBlock.gif                start = 40; end = 54;
 90InBlock.gif                range=Word_doc.Range(ref start, ref end);
 91InBlock.gif                range.Font.Size=14;
 92InBlock.gif                
 93InBlock.gif
 94ExpandedSubBlockEnd.gif            }

 95InBlock.gif            catch(Exception e)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                Console.WriteLine(" Font exception:{0}", e.ToString());
 98ExpandedSubBlockEnd.gif            }

 99InBlock.gif
100InBlock.gif
101InBlock.gif            // Wait so the new formatting can be appreciated
102InBlock.gif            Thread.Sleep(3000);
103InBlock.gif
104InBlock.gif            autocorrect.ReplaceTextFromSpellingChecker=true;
105InBlock.gif            // Fix inntroduction
106InBlock.gif            string obj = "Introduction";
107InBlock.gif            AutoCorrectEntry errEntry= autoEntries.Add("Inntroduction", obj);
108InBlock.gif
109InBlock.gif            Words myWords=Word_doc.Words;
110InBlock.gif            Range errRange= myWords[7];
111InBlock.gif            errEntry.Apply(errRange);
112InBlock.gif
113InBlock.gif            // Add a caption to the window and get it back             
114InBlock.gif            Window myWindow = Word_App.ActiveWindow;
115InBlock.gif            myWindow.Caption = "Managed Word execution from C# ";
116InBlock.gif            string gotCaption = myWindow.Caption;
117InBlock.gif            if (gotCaption.Equals("Managed Word execution from C# "))
118ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
119InBlock.gif                Console.WriteLine("Caption assigned and got back");
120InBlock.gif                return_Result = 1;
121ExpandedSubBlockEnd.gif            }

122InBlock.gif            Thread.Sleep(2000);
123InBlock.gif
124InBlock.gif            // define the selection object, find and  replace text
125InBlock.gif            Selection mySelection = myWindow.Selection;
126InBlock.gif            
127InBlock.gif
128InBlock.gif            try
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif                start = 1; end =3;
131InBlock.gif                range=Word_doc.Range(ref start, ref end);
132InBlock.gif                Console.WriteLine("The color of .NET is being changed");
133InBlock.gif                
134InBlock.gif                range.Font.Bold=16;
135InBlock.gif                range.Font.Color=WdColor.wdColorLavender;
136InBlock.gif
137ExpandedSubBlockEnd.gif            }

138InBlock.gif            catch(Exception e)
139ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
140InBlock.gif                Console.WriteLine(" Font exception:{0}", e.ToString());
141ExpandedSubBlockEnd.gif            }

142InBlock.gif            Thread.Sleep(2000);
143InBlock.gif            
144InBlock.gif            // underline the selected text
145InBlock.gif             range=Word_doc.Range(ref start,ref end);
146InBlock.gif            range.Underline=(WdUnderline.wdUnderlineDouble);
147InBlock.gif
148InBlock.gif            // add hyperlink and follow the hyperlink
149InBlock.gif            Hyperlinks my_Hyperlinks = Word_doc.Hyperlinks;
150InBlock.gif
151InBlock.gif            // Make the range past the end of all document text
152InBlock.gif            mySelection.Start = 9999;
153InBlock.gif            mySelection.End   = 9999;
154InBlock.gif            range = mySelection.Range;
155InBlock.gif
156InBlock.gif            // Add a hyperlink
157InBlock.gif            string myAddress = "http://go.microsoft.com/fwlink/?linkid=3269&clcid=0x409";
158InBlock.gif            object obj_Address = myAddress;
159InBlock.gif            Console.WriteLine("Adding hyperlink to the document");
160InBlock.gif            Hyperlink my_Hyperlink1= my_Hyperlinks._Add(range, ref obj_Address, ref missing);              
161InBlock.gif            Word_App.ActiveWindow.Selection.InsertAfter("\n");
162InBlock.gif
163InBlock.gif            Thread.Sleep(5000);
164InBlock.gif
165InBlock.gif            // Open a window to Hyperlink
166InBlock.gif            Process ie = Process.Start("iexplore.exe", my_Hyperlink1.Address);            
167InBlock.gif
168InBlock.gif            // Wait for a short spell to allow the page to be examined
169InBlock.gif            Thread.Sleep(10000);
170InBlock.gif            
171InBlock.gif            // close the browser first
172InBlock.gif            Console.WriteLine("Removing browser window");
173InBlock.gif            ie.Kill();
174InBlock.gif
175InBlock.gif            // Display "The End"
176InBlock.gif            Word_App.ActiveWindow.Selection.InsertAfter(theEnd);
177InBlock.gif            Word_App.ActiveWindow.Selection.Start = 0;
178InBlock.gif            Word_App.ActiveWindow.Selection.End = 0;
179InBlock.gif            Word_App.Activate();
180InBlock.gif            Thread.Sleep(5000);
181InBlock.gif
182InBlock.gif            // Close Microsoft Word
183InBlock.gif            object myBool = WdSaveOptions.wdDoNotSaveChanges;
184InBlock.gif            Word_App.ActiveWindow.Close(ref myBool,ref missing);
185InBlock.gif        Exit:
186InBlock.gif            return return_Result;
187ExpandedSubBlockEnd.gif        }

188ExpandedSubBlockEnd.gif    }

189ExpandedBlockEnd.gif}

190 None.gif

转载于:https://www.cnblogs.com/Aldebaran/archive/2005/12/01/288341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值