做对WORD文档读取时会经常对定位有问题,在msdn中有一个WordApp的小程序,本文就以它为例
准备:初学者对找WORD控件有很大问题,要在对office 2003中添加/更改对Net的支持
然后加入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行修改试验
1using System;
2using System.Diagnostics;
3using System.Runtime.InteropServices;
4using System.Reflection;
5using System.Collections;
6using System.Threading;
7using Microsoft.Office.Interop.Word;
8
9namespace Microsoft.Samples.Interop.WordApp
10{
11 class WordAppMain
12 {
13 static object missing = Missing.Value;
14 static object missing2 = Missing.Value;
15 static object missing3 = Missing.Value;
16 static object missing4 = Missing.Value;
17 static object missing5 = Missing.Value;
18 static object missing6 = Missing.Value;
19 static object missing7 = Missing.Value;
20 static object missing8 = Missing.Value;
21 static object missing9 = Missing.Value;
22 static object missing10 = Missing.Value;
23 static object missing11 = Missing.Value;
24 static object missing12 = Missing.Value;
25 static object missing13 = Missing.Value;
26
27
28 static int Main()
29 {
30
31 WordAppMain myWord = new WordAppMain();
32 int return_Result = 0;
33
34 // Create a word object that we can manipulate
35 Application Word_App = null;
36 Document Word_doc=null;
37 try
38 {
39 Word_App = new Application();
40 Word_doc=new Document();
41 }
42 catch(Exception e)
43 {
44 Console.WriteLine("Can't create a word document " + e.ToString());
45 return_Result = 1;
46 goto Exit;
47 }
48
49 AutoCorrect autocorrect = Word_App.AutoCorrect;
50 AutoCorrectEntries autoEntries = autocorrect.Entries;
51
52 string theEnd= "\nThe End";
53 autoEntries.Add("Inntroduction", "Introduction");
54
55 Documents Docs = Word_App.Documents;
56 if (Docs == null)
57 {
58 Console.WriteLine("Docs is null");
59 }
60 else
61 {
62 Console.WriteLine("Docs exists:" + Docs.Count);
63 }
64
65 Word_App.Visible=true;
66 _Document my_Doc= (_Document) Word_doc;
67 Word_doc=Docs.Add(ref missing, ref missing, ref missing, ref missing);
68
69 object start = 0;
70 object end = 0;
71 Range range = Word_doc.Range(ref missing,ref missing);
72
73 // add text to the doc -- this contains some deliberate misspellings so that we can correct them in a short while
74 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 ";
75
76 // Wait so the starting state can be admired
77 Thread.Sleep(2000);
78
79 // Format the title
80 Font fc= new Font();
81 try
82 {
83 Console.WriteLine("Formatting the title");
84 start = 0; end = 40;
85 range=Word_doc.Range(ref start, ref end);
86 range.Font.Size=24;
87 range.Font.Bold=1;
88 range.Font.Color=WdColor.wdColorGray30;
89 start = 40; end = 54;
90 range=Word_doc.Range(ref start, ref end);
91 range.Font.Size=14;
92
93
94 }
95 catch(Exception e)
96 {
97 Console.WriteLine(" Font exception:{0}", e.ToString());
98 }
99
100
101 // Wait so the new formatting can be appreciated
102 Thread.Sleep(3000);
103
104 autocorrect.ReplaceTextFromSpellingChecker=true;
105 // Fix inntroduction
106 string obj = "Introduction";
107 AutoCorrectEntry errEntry= autoEntries.Add("Inntroduction", obj);
108
109 Words myWords=Word_doc.Words;
110 Range errRange= myWords[7];
111 errEntry.Apply(errRange);
112
113 // Add a caption to the window and get it back
114 Window myWindow = Word_App.ActiveWindow;
115 myWindow.Caption = "Managed Word execution from C# ";
116 string gotCaption = myWindow.Caption;
117 if (gotCaption.Equals("Managed Word execution from C# "))
118 {
119 Console.WriteLine("Caption assigned and got back");
120 return_Result = 1;
121 }
122 Thread.Sleep(2000);
123
124 // define the selection object, find and replace text
125 Selection mySelection = myWindow.Selection;
126
127
128 try
129 {
130 start = 1; end =3;
131 range=Word_doc.Range(ref start, ref end);
132 Console.WriteLine("The color of .NET is being changed");
133
134 range.Font.Bold=16;
135 range.Font.Color=WdColor.wdColorLavender;
136
137 }
138 catch(Exception e)
139 {
140 Console.WriteLine(" Font exception:{0}", e.ToString());
141 }
142 Thread.Sleep(2000);
143
144 // underline the selected text
145 range=Word_doc.Range(ref start,ref end);
146 range.Underline=(WdUnderline.wdUnderlineDouble);
147
148 // add hyperlink and follow the hyperlink
149 Hyperlinks my_Hyperlinks = Word_doc.Hyperlinks;
150
151 // Make the range past the end of all document text
152 mySelection.Start = 9999;
153 mySelection.End = 9999;
154 range = mySelection.Range;
155
156 // Add a hyperlink
157 string myAddress = "http://go.microsoft.com/fwlink/?linkid=3269&clcid=0x409";
158 object obj_Address = myAddress;
159 Console.WriteLine("Adding hyperlink to the document");
160 Hyperlink my_Hyperlink1= my_Hyperlinks._Add(range, ref obj_Address, ref missing);
161 Word_App.ActiveWindow.Selection.InsertAfter("\n");
162
163 Thread.Sleep(5000);
164
165 // Open a window to Hyperlink
166 Process ie = Process.Start("iexplore.exe", my_Hyperlink1.Address);
167
168 // Wait for a short spell to allow the page to be examined
169 Thread.Sleep(10000);
170
171 // close the browser first
172 Console.WriteLine("Removing browser window");
173 ie.Kill();
174
175 // Display "The End"
176 Word_App.ActiveWindow.Selection.InsertAfter(theEnd);
177 Word_App.ActiveWindow.Selection.Start = 0;
178 Word_App.ActiveWindow.Selection.End = 0;
179 Word_App.Activate();
180 Thread.Sleep(5000);
181
182 // Close Microsoft Word
183 object myBool = WdSaveOptions.wdDoNotSaveChanges;
184 Word_App.ActiveWindow.Close(ref myBool,ref missing);
185 Exit:
186 return return_Result;
187 }
188 }
189}
190