/
sendWord.cs
1115 lines (911 loc) · 39.6 KB
/
sendWord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// sendWord.cs
//
// Copyright (c) 2013 Brent Knowles (http://www.brentknowles.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Review documentation at http://www.yourothermind.com for updated implementation notes, license updates
// or other general information/
//
// Author information available at http://www.brentknowles.com or http://www.amazon.com/Brent-Knowles/e/B0035WW7OW
// Full source code: https://github.com/BrentKnowles/YourOtherMind
//###
using System;
using System.Collections.Generic;
using System.Text;
using Word = NetOffice.WordApi;
//using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using CoreUtilities;
/* Features
*
*************
* DONE
*************
* # = Bullets
* * = bullets (can't get multi-level to work, and when they do, they'll only work if you have a style)
* = Heading 1... Heading 4 =
* '' Italic
* ''' Bold
* ||table||
*
*
*
*
************
* TO DO
************
*
*/
namespace SendTextAway
{
class sendWord : sendBase
{
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
object oFalse = false;
object oTrue = true;
Word._Application oWord;
Word._Document oDoc;
Word.Selection oSelection;
object oStyle = null;
object oHeader1 = null;
object oHeader2 = null;
object oHeader3 = null;
object oHeader4 = null;
object oHeader5 = null;
object oBullet = null;
object oBulletNumber = null;
object oTableStyle = null;
object oTitle = null;
/// <summary>
/// before whatever initial operations are required to open the filestream
/// or whatever (in the case of Word Auto, will require global variables)
/// </summary>
protected override int InitializeDocument(ControlFile _controlFile)
{
//create a word object instead of globals, ie., WOrd(ControlFIle)
base.InitializeDocument(_controlFile);
oWord = new Word.Application();
oWord.Visible = true;
object template = controlFile.Template;
oDoc = oWord.Documents.Add( template, oMissing,
oMissing, oMissing);
oSelection = oWord.Selection;
// load defaults from tempalte
try
{
oHeader1 = controlFile.Heading1;
oHeader2 = controlFile.Heading2; // write getmethods?
oHeader3 = controlFile.Heading3;
oHeader4 = controlFile.Heading4;
oHeader5 = controlFile.Heading5;
oBullet = controlFile.Bullet;
oTitle = controlFile.ChapterTitle;
oStyle = controlFile.BodyText;
oBulletNumber = controlFile.BulletNumber;
oTableStyle = controlFile.TableStyle;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
return -1;
}
// test if style exists before being used
bool bExists = false;
foreach (Word.Style style in oDoc.Styles)
{
if (style.NameLocal == (oStyle).ToString())
{
bExists = true;
}
}
if (false == bExists)
{
NewMessage.Show(oStyle.ToString() + " did not exist");
}
oSelection.Style = oStyle;// (March 2013 - this might be reasonable replacement
// set defaults
// oSelection.set_Style(ref oStyle); //this never seemed to happen 12/12/2010 (FIXED! The name was wrong, trying to come up with a better error check system
Word.Style oSetStyle = (Word.Style)oSelection.Style;
/* DId not work to test
if (oSetStyle.NameLocal != (oStyle).ToString())
{
NewMessage.Show(oStyle.ToString() + " did not exist");
}*/
return 1;
}
/// <summary>
/// overwrite the tab character being written
/// </summary>
protected override void AddTab()
{
InlineWrite("\t");
}
/// <summary>
/// Builds a table row
/// </summary>
/// <param name="sText"></param>
protected override void AddTable(string sText)
{
//March 2013 NetOffice.WordApi.Enums.WdDefaultTableBehavior.wdWord8TableBehavior;
object objDefaultBehaviorWord8 = Word.Enums.WdDefaultTableBehavior.wdWord8TableBehavior;
string[] Cols = sText.Split(new string[1] { "||" }, StringSplitOptions.RemoveEmptyEntries);
Word.Table oTable;
oTable = oDoc.Tables.Add(oSelection.Range, 1, Cols.Length, objDefaultBehaviorWord8, oMissing);
oTable.Style = oTableStyle;
// oTable.Range.ParagraphFormat.SpaceAfter = 10;
// oSelection.SetRange(oTable.Range.End + 20, 1);
//oTable.Borders = Word.WdBorderType.wdBorderBottom;
for (int table = 0; table < Cols.Length; table++)
{
// oTable.Cell(oTable.Rows.Count, table + 1).Range.Text =
Word.Range range = oTable.Cell(oTable.Rows.Count, table + 1).Range;
oSelection.SetRange(range.Start, range.End);
FormatRestOfText(Cols[table]);
}
// edit from the end
// March 2013 Word.Range wrdRng = oDoc.Bookmarks[oEndOfDoc].Range;
Word.Range wrdRng = oDoc.Bookmarks[oEndOfDoc].Range;
oSelection.SetRange(wrdRng.Start, wrdRng.End);
}
/// <summary>
///
/// </summary>
/// <param name="sText"></param>
protected override void AddNumberedBullets(string sText)
{
try
{
// Figured out how to have two chunks of text
//if (oBulletNumber.ToString() != "bulletdefault")
{
string sTabPrefix = "";
string sOldText = sText;
// add tabs
sText = sText.TrimStart('#').Trim();
int nInsertionPoint = oSelection.Start;
string sTextToAdd = sTabPrefix + sText;// +Environment.NewLine;
oSelection.Style = oBulletNumber;
// oSelection.set_Style(ref oBulletNumber);
object defaultlist = Word.Enums.WdDefaultListBehavior.wdWord10ListBehavior;
/*while (oSelection.Range.ListFormat.ListType != Microsoft.Office.Interop.Word.WdListType.wdListSimpleNumbering)
{
oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
}*/
//oSelection.Range.ListFormat.List.
oSelection.Range.ListFormat.ApplyNumberDefault( defaultlist);
oSelection.Range.ListFormat.ApplyNumberDefault( defaultlist);
// oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
// oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
/* Word.ListFormat cleanFormat = null;
if (cleanFormat == null)
{
cleanFormat = oSelection.Range.ListFormat;
}
oSelection.Range. = cleanFormat;
*/
int nCount = 0; bool done = false;
//System.Windows.Forms.MessageBox.Show(oSelection.Range.ListFormat.ListValue.ToString());
oSelection.Range.ListFormat.ListLevelNumber = 1;
while (done == false)
{
// test to see if are an empty string
if (sOldText.Length > nCount + 1)
{
if (nCount > 0 && sOldText[nCount] == '#')
{
// tab blanked the text;
/*
Word.Range oldrange = oSelection.Range;
oSelection.SetRange(nInsertionPoint, nInsertionPoint + sTextToAdd.Length);
oSelection.Range.InsertAlignmentTab((int)Word.WdAlignmentTabAlignment.wdRight, (int)Word.WdAlignmentTabAlignment.wdLeft);
oSelection.SetRange(oldrange.Start, oldrange.End);
*/
oSelection.Range.ListFormat.ListLevelNumber++; // ++crash
// oSelection.Range.ListFormat.ListIndent(); not what we want
//oSelection.TypeText("\t"); // closet but ick
// oSelection.TypeText("\xB"); vertical tab did not work
// oSelection.TypeText();
}
else if (nCount > 0)
{
done = true;
}
}
else
{
done = true;
}
nCount++;
}
// we restart a list if the ListValue is greater than ONE
// and we have reset InAList = false
// This means that we are at #2 of a list that thinks it is continuing
// from another list seperated by text
bool bRestartList = false;
if (oSelection.Range.ListFormat.ListValue > 1 && InAList == false)
{
bRestartList = true;
}
// we sometimes reset the list due to 'issues'
if ((oSelection.Range.ListFormat.ListLevelNumber == 1
&& FixListScan(oSelection.Range.ListFormat.ListString) == true)
|| (bRestartList == true)
)
{
foreach (Word.Paragraph para in oSelection.Range.ListParagraphs)
{
// para.ResetAdvanceTo();
// para.SeparateList();
para.Reset(); // worked for simple list but not big doc
//para.Reset();
}
}
// set that we are in a list
// if (oSelection.Range.ListFormat.ListValue == 1)
{
InAList = true;
}
/* worked but Reset seems easier
* object oDef = Word.WdDefaultListBehavior.wdWord10ListBehavior;
object myTemplateIndex = 1;
Word.ListTemplate myTemplate =
oWord.ListGalleries[Microsoft.Office.Interop.Word.WdListGalleryType.wdNumberGallery].ListTemplates.get_Item(ref myTemplateIndex);
oSelection.Range.ListFormat.ApplyListTemplate(myTemplate, ref oFalse, ref oFalse,
ref oDef );
* */
// System.Windows.Forms.MessageBox.Show("bad list");
/* foreach (Word.Paragraph para in oSelection.Range.ListParagraphs)
{
// para.ResetAdvanceTo();
// para.SeparateList();
para.Reset(); // worked for simple list but not big doc
para.Reset();
}
*/
// oSelection.Range.ListParagraphs[1].SeparateList();
//oSelection.Range.ListFormat.ApplyListTemplate(oSelection.Range.ListParagraphs[1].
// oSelection.Range.SetListLevel(1);
// oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
// oSelection.set_Style(ref oBulletNumber);
// oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
//oSelection.TypeText(sTextToAdd);
FormatRestOfText(sTextToAdd);// + "(" + oSelection.Range.ListFormat.ListValue.ToString());
//indent for each #
// turn off
// oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
// oSelection.Range.ListFormat.ListLevelNumber = 1;
oSelection.Range.ListFormat.ApplyNumberDefault( defaultlist);
oSelection.Style =oStyle;
}
}
catch (Exception ex)
{
UpdateErrors("Failure in AddNumberedBullets + " + sText + " " + ex.ToString());
}
/* else
{
object defaultlist = Word.WdDefaultListBehavior.wdWord10ListBehavior;
oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
oSelection.TypeText(sText + Environment.NewLine);
// oSelection.Select(); did I need this
oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
}*/
/* bool done = false;
int nCount = 0;
//indent for each #
while (done == false)
{
if (sText[nCount] == '#')
{
oSelection.Range.ListFormat.ListIndent();
}
else
{
done = true;
}
nCount++;
}
sText = sText.TrimStart('#').Trim();
object defaultlist = Word.WdDefaultListBehavior.wdWord10ListBehavior;
oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);
oSelection.TypeText(sText + Environment.NewLine);
oSelection.Range.ListFormat.ApplyNumberDefault(ref defaultlist);*/
}
/// <summary>
/// Align text until next alignment command is hit
/// 0 - Center
/// 1 - Left
/// 2 - Right
/// </summary>
/// <param name="nAlignment"></param>
protected override void AlignText(int nAlignment)
{
switch (nAlignment)
{
case 0: oSelection.ParagraphFormat.Alignment = Word.Enums.WdParagraphAlignment.wdAlignParagraphCenter; break;
case 1: oSelection.ParagraphFormat.Alignment = Word.Enums.WdParagraphAlignment.wdAlignParagraphLeft; break;
case 2: oSelection.ParagraphFormat.Alignment = Word.Enums.WdParagraphAlignment.wdAlignParagraphRight; break;
}
}
/// <summary>
///
/// </summary>
/// <param name="sText"></param>
protected override void AddBullets(string sText)
{
/* sText = sText.TrimStart('*').Trim();
object defaultlist = Word.WdDefaultListBehavior.wdWord10ListBehavior;
oSelection.Range.ListFormat.ApplyBulletDefault(ref defaultlist);
oSelection.Range.ListFormat.ListOutdent();
//oSelection.TypeText(FormatRestOfText(sText) + Environment.NewLine);
// oSelection.Range.ListFormat.ListLevelNumber++;
FormatRestOfText(sText);
oSelection.Range.ListFormat.ApplyBulletDefault(ref defaultlist);*/
string sOldText = sText;
// add tabs
sText = sText.TrimStart('*').Trim();
int nInsertionPoint = oSelection.Start;
string sTextToAdd = sText;
oSelection.Style = oBullet;
object defaultlist = Word.Enums.WdDefaultListBehavior.wdWord10ListBehavior;
oSelection.Range.ListFormat.ApplyBulletDefault( defaultlist);
oSelection.Range.ListFormat.ApplyBulletDefault( defaultlist);
int nCount = 0; bool done = false;
oSelection.Range.ListFormat.ListLevelNumber = 1;
while (done == false)
{
// test to see if are an empty string
if (sOldText.Length > nCount + 1)
{
if (nCount > 0 && sOldText[nCount] == '*')
{
oSelection.Range.ListFormat.ListLevelNumber++;
}
else if (nCount > 0)
{
done = true;
}
}
else
{
done = true;
}
nCount++;
}
FormatRestOfText(sTextToAdd);// + "(" + oSelection.Range.ListFormat.ListValue.ToString());
oSelection.Range.ListFormat.ApplyBulletDefault( defaultlist);
oSelection.Style = oStyle;
}
/// <summary>
/// insert a page break, but only if we need it
/// </summary>
protected override void AddPageBreak()
{
Word.Enums.WdInformation info =Word.Enums.WdInformation.wdFirstCharacterLineNumber;
int line = (int)oSelection.get_Information(info);
int col =(int)oSelection.get_Information(Word.Enums.WdInformation.wdFirstCharacterColumnNumber);
// if we are not on the first line and column of a page we insert a page break
if ( !(line == 1 && col == 1) )
{
object pageBreak = Word.Enums.WdBreakType.wdPageBreak;
oSelection.InsertBreak( pageBreak);
}
}
/// <summary>
///
/// </summary>
/// <param name="sText"></param>
protected override string AddTitle(string sText)
{
sText = base.AddTitle(sText);
if (chapter > 1)
{
AddPageBreak();
}
oSelection.Style = oTitle;
oSelection.TypeText(sText + Environment.NewLine);
oSelection.Style = oStyle;
return sText;
}
/// <summary>
/// sID is already lowercase
/// </summary>
/// <param name="sID"></param>
protected override void AddFootnote(string sID)
{
sID = sID.Trim();
if (FootnoteHash.ContainsKey(sID) == true)
{
string sSource = (string) FootnoteHash[sID];
sSource = sSource.Replace("<br>", Environment.NewLine);
// process text lienfeed
object sText = sSource;
object id = (object)sID;
// oSelection.Text = oSelection.Text + " ";
oSelection.Select();
object start = oSelection.Range.End-2;
object end = oSelection.Range.End-1;// oSelection.Range.End - 1;
Word.Range mark = oDoc.Range( start, end);
//oSelection.TypeText("(Note)");
oSelection.Footnotes.Add(mark, oMissing, sText);
object amount = 1;
oSelection.MoveLeft( oMissing, amount, oMissing);
}
else
{
NewMessage.Show(String.Format("{0} footnote not found!", sID));
}
}
/// <summary>
///
/// </summary>
/// <param name="sText"></param>
/// <param name="nLevel">Heading level 1..4</param>
protected override void AddHeader(string sText, int nLevel)
{
object oStyleToUse = null;
switch (nLevel)
{
case 1: oStyleToUse = oHeader1; break;
case 2: oStyleToUse = oHeader2; break;
case 3: oStyleToUse = oHeader3; break;
case 4: oStyleToUse = oHeader4; break;
case 5: oStyleToUse = oHeader5; break;
}
oSelection.Style = oStyleToUse;
oSelection.TypeText(sText + Environment.NewLine);
oSelection.Style = oStyle;
}
/// <summary>
/// Starts a multiline format change such as
/// <example>
/// here
/// </example>
/// </summary>
/// <param name="sFormatType"></param>
/// <returns></returns>
protected override bool AddStartMultiLineFormat(string sFormatType)
{
// starting a format change
object newFormat = sFormatType;
oSelection.Style = newFormat;
return true;
}
/// <summary>
/// resets the formatting of a multi line format to normal format
/// </summary>
/// <returns></returns>
protected override void AddEndMultiLineFormat()
{
oSelection.Style = oStyle;
}
protected override void ChatMode(int onoff)
{
switch (controlFile.ChatMode)
{
case 0: InlineUnderline(onoff); break;
case 1: InlineItalic(onoff); break;
case 2: InlineBold(onoff); break;
}
/* if (1 == onoff)
{
// object newFormat = "Emphasis";
// oSelection.set_Style(ref newFormat);
InlineUnderline(1);
}
else
{
// back to default
// oSelection.set_Style(ref oStyle);
}*/
}
protected override int LineCount()
{
// return oDoc.Sentences.Count;
return oDoc.Paragraphs.Count;
}
/// <summary>
/// searches for the object in text
///
/// May 2012
/// Original intent was just to look for extra brackets in text as
/// error detction mechanism
/// </summary>
/// <param name="searchMe"></param>
protected override void SearchFor(object searchMe)
{
/* NEVER WORKED
object item = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;
object whichItem = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
object forward = false; //cause we are back of it?
object matchAllWord = false;
object missing = System.Reflection.Missing.Value;
oSelection.Range.Document.GoTo(ref item, ref whichItem, ref missing, ref missing);
oSelection.Range.Find.Execute(ref searchMe, ref missing, ref matchAllWord,
ref missing, ref missing, ref missing, ref forward,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
*/
}
protected override void Cleanup()
{
/*12/12/2010 - disabled, lost text between page breaks
if (controlFile.Linespace != -1)
{
// do cleanup
// fix line spacing
NewMessage.Show("Starting Double Space -- DO NOT TOUCH WORD FILE UNTIL DONE");
foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in oDoc.Paragraphs)
{
paragraph.Format.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceExactly;
paragraph.Format.LineSpacing = oWord.LinesToPoints(controlFile.Linespace);
}
NewMessage.Show("DOUBLE SPACE FINISHED");
}*/
//* trying to replace tabbed underlines with no underline... instead created a macro and a reminder to run it
/*
oSelection.Find.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineSingle;
oSelection.Find.Replacement.ClearFormatting();
oSelection.Find.Replacement.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
oSelection.Find.Text = "^t";
oSelection.Find.Replacement.Text = "^t";
oSelection.Find.
*/
// may 2012 trying to count lines
/* foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in oDoc.Paragraphs)
{
// count paragraphs
foreach (
}*/
base.Cleanup();
// update the table of contents if present
if (oDoc.TablesOfContents.Count > 0)
{
oDoc.TablesOfContents[1].Update();
}
}
/// <summary>
/// While processing linline formatting (bold, et cetera), this is used to write out a line of text
/// </summary>
/// <param name="sText"></param>
protected override void InlineWrite(string sText)
{
base.InlineWrite(sText);
oSelection.TypeText(sText);
}
protected override void InlineBold(int nValue)
{
oSelection.Font.Bold = nValue;
}
protected override void InlineItalic(int nValue)
{
oSelection.Font.Italic = nValue;
}
/* /// <summary>
/// get current page number
/// </summary>
/// <param name="nValue"></param>
protected override string PageNumber()
{
// return oSelection.Footnotes[0].ToString();
return oSelection.Footnotes.Count.ToString();
}*/
protected override void InlineStrikeThrough(int nValue)
{
oSelection.Font.StrikeThrough = nValue;
}
/// <summary>
/// nvalue is ignored for underline
/// </summary>
/// <param name="nValue"></param>
protected override void InlineSuper(int nValue)
{
oSelection.Font.Superscript = nValue;
}
/// <summary>
/// nvalue is ignored for underline
/// </summary>
/// <param name="nValue"></param>
protected override void InlineSub(int nValue)
{
oSelection.Font.Subscript = nValue;
}
/// <summary>
/// Adds a bookmark at cursor point
/// </summary>
/// <param name="sBookmark"></param>
protected override void AddBookmark(string sBookmark)
{
sBookmark = sBookmark.Trim();
object Range = oSelection.Range;
oSelection.Bookmarks.Add(sBookmark, Range);
}
int formatCount = 0; // Nov 2012 will increment. If we are already underlining and then we have underlining inside of underlining
// we do not STOP underlining until the block text is finished formatting.
/// <summary>
/// nvalue is ignored for underline
/// </summary>
/// <param name="nValue"></param>
protected override void InlineUnderline(int nValue)
{
if (nValue == 1)
{
if (oSelection.Font.Underline == Word.Enums.WdUnderline.wdUnderlineSingle)
{
NewMessage.Show("We were asked to start underining during a segment of text wherein we were ALREADY underlining. We are ignoring this request. Check for any text issues.");
formatCount++;
}
oSelection.Font.Underline = Word.Enums.WdUnderline.wdUnderlineSingle;
}
else
if (nValue == 0)
{
if (0 == formatCount)
{
oSelection.Font.Underline = Word.Enums.WdUnderline.wdUnderlineNone;
}
else formatCount--;
}
}
/// <summary>
///
/// </summary>
/// <param name="sPathToFile"></param>
protected override void AddLink(string sPathToFile, string sTitle)
{
object oTextToShow = sTitle;
object oLink = sPathToFile;
oSelection.Hyperlinks.Add(oSelection.Range, oLink, oMissing,
oMissing, oTextToShow, oMissing);
// System.Windows.Forms.MessageBox.Show(sPathToFile);
}
/// <summary>
/// adds a picture
/// </summary>
/// <param name="nValue"></param>
protected override void AddPicture(string sPathToFile)
{
base.AddPicture(sPathToFile);
object oWidth = 10;
object oLeft = 1;
object oTop = oSelection.Range.Start;
object oHeight = 100;
// System.Windows.Forms.MessageBox.Show(sPathToFile);
//oDoc.Shapes.AddPicture(sPathToFile, ref oMissing, ref oMissing, ref oLeft, ref oTop, ref oWidth, ref oHeight, ref oMissing);
oSelection.InlineShapes.AddPicture(sPathToFile, oMissing, oTrue, oMissing);
}
/// <summary>
/// Adds a table of contents at location
/// </summary>
protected override void AddTableOfContents()
{
Object oUpperHeadingLevel = "1";
Object oLowerHeadingLevel = "3";
Object oTrue = true;
Object oTOCTableID = "TableOfContents";
oDoc.TablesOfContents.Add(oSelection.Range, oTrue, oUpperHeadingLevel,
oLowerHeadingLevel, oMissing, oTOCTableID, oTrue,
oTrue, oMissing, oTrue, oTrue, oTrue);
}
/// <summary>
/// just an example function
/// </summary>
public void example()
{
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add( oMissing, oMissing,
oMissing, oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add( oMissing);
object styleHeading1 = "Heading 1";
oPara1.Style = styleHeading1;
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
//Insert a paragraph at the end of the document.
Word.Paragraph oPara2;
object oRng = oDoc.Bookmarks[ oEndOfDoc].Range;
oPara2 = oDoc.Content.Paragraphs.Add( oRng);
oPara2.Range.Text = "Heading 2";
oPara2.Format.SpaceAfter = 6;
oPara2.Range.InsertParagraphAfter();
//Insert another paragraph.
Word.Paragraph oPara3;
oRng = oDoc.Bookmarks[ oEndOfDoc].Range;
oPara3 = oDoc.Content.Paragraphs.Add( oRng);
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";
oPara3.Range.Font.Bold = 0;
oPara3.Format.SpaceAfter = 24;
oPara3.Range.InsertParagraphAfter();
//Insert a 3 x 5 table, fill it with data, and make the first row
//bold and italic.
Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks[ oEndOfDoc].Range;
oTable = oDoc.Tables.Add(wrdRng, 3, 5, oMissing, oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
int r, c;
string strText;
for (r = 1; r <= 3; r++)
for (c = 1; c <= 5; c++)
{
strText = "r" + r + "c" + c;
oTable.Cell(r, c).Range.Text = strText;
}
oTable.Rows[1].Range.Font.Bold = 1;
oTable.Rows[1].Range.Font.Italic = 1;
//Add some text after the table.
Word.Paragraph oPara4;
oRng = oDoc.Bookmarks[ oEndOfDoc].Range;
oPara4 = oDoc.Content.Paragraphs.Add( oRng);
oPara4.Range.InsertParagraphBefore();
oPara4.Range.Text = "And here's another table:";
oPara4.Format.SpaceAfter = 24;
oPara4.Range.InsertParagraphAfter();
//Insert a 5 x 2 table, fill it with data, and change the column widths.
wrdRng = oDoc.Bookmarks[ oEndOfDoc].Range;
oTable = oDoc.Tables.Add(wrdRng, 5, 2, oMissing, oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
for (r = 1; r <= 5; r++)
for (c = 1; c <= 2; c++)
{
strText = "r" + r + "c" + c;
oTable.Cell(r, c).Range.Text = strText;
}
oTable.Columns[1].Width = oWord.InchesToPoints(2); //Change width of columns 1 & 2
oTable.Columns[2].Width = oWord.InchesToPoints(3);
//Keep inserting text. When you get to 7 inches from top of the
//document, insert a hard page break.
object oPos;
double dPos = oWord.InchesToPoints(7);
oDoc.Bookmarks[ oEndOfDoc].Range.InsertParagraphAfter();
do
{
wrdRng = oDoc.Bookmarks[ oEndOfDoc].Range;
wrdRng.ParagraphFormat.SpaceAfter = 6;
wrdRng.InsertAfter("A line of text");
wrdRng.InsertParagraphAfter();
oPos = wrdRng.get_Information
(Word.Enums.WdInformation.wdVerticalPositionRelativeToPage);
}
while (dPos >= Convert.ToDouble(oPos));
object oCollapseEnd = Word.Enums.WdCollapseDirection.wdCollapseEnd;
object oPageBreak = Word.Enums.WdBreakType.wdPageBreak;
wrdRng.Collapse( oCollapseEnd);
wrdRng.InsertBreak( oPageBreak);
wrdRng.Collapse( oCollapseEnd);
wrdRng.InsertAfter("We're now on page 2. Here's my chart:");
wrdRng.InsertParagraphAfter();
//Insert a chart.
// Word.InlineShape oShape;
// object oClassType = "MSGraph.Chart.8";
// wrdRng = oDoc.Bookmarks[ oEndOfDoc].Range;
// oShape = wrdRng.InlineShapes.AddOLEObject( oClassType, oMissing,
// oMissing, oMissing, oMissing,
// oMissing, oMissing, oMissing);