-
-
Notifications
You must be signed in to change notification settings - Fork 654
/
Copy pathwinword.cpp
1462 lines (1391 loc) · 61.6 KB
/
winword.cpp
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
/*
This file is a part of the NVDA project.
URL: http://www.nvda-project.org/
Copyright 2006-2025 NVDA contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2.0, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This license can be found at:
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
#define WIN32_LEAN_AND_MEAN
#include <sstream>
#include <vector>
#include <comdef.h>
#include <windows.h>
#include <oleacc.h>
#include <common/xml.h>
#include <common/log.h>
#include <optional>
#include "nvdaHelperRemote.h"
#include <remote/nvdaInProcUtils.h>
#include <remote/WinWord/Constants.h>
#include <remote/WinWord/Fields.h>
#include "winword.h"
using namespace std;
// See https://github.com/nvaccess/nvda/wiki/Using-COM-with-NVDA-and-Microsoft-Word
constexpr int formatConfig_reportFontName = 0x1;
constexpr int formatConfig_reportFontSize = 0x2;
constexpr int formatConfig_reportFontAttributes = 0x4;
constexpr int formatConfig_reportColor = 0x8;
constexpr int formatConfig_reportAlignment = 0x10;
constexpr int formatConfig_reportStyle = 0x20;
constexpr int formatConfig_reportSpellingErrors = 0x40;
constexpr int formatConfig_reportPage = 0x80;
constexpr int formatConfig_reportLineNumber = 0x100;
constexpr int formatConfig_reportTables = 0x200;
constexpr int formatConfig_reportLists = 0x400;
constexpr int formatConfig_reportLinks = 0x800;
constexpr int formatConfig_reportComments = 0x1000;
constexpr int formatConfig_reportHeadings = 0x2000;
constexpr int formatConfig_reportLanguage = 0x4000;
constexpr int formatConfig_reportRevisions = 0x8000;
constexpr int formatConfig_reportParagraphIndentation = 0x10000;
constexpr int formatConfig_includeLayoutTables = 0x20000;
constexpr int formatConfig_reportLineSpacing = 0x40000;
constexpr int formatConfig_reportSuperscriptsAndSubscripts = 0x80000;
constexpr int formatConfig_reportGraphics = 0x100000;
constexpr int formatConfig_reportHighlightColor = 0x200000;
constexpr int formatConfig_fontFlags =(formatConfig_reportFontName|formatConfig_reportFontSize|formatConfig_reportFontAttributes|formatConfig_reportColor|formatConfig_reportSuperscriptsAndSubscripts);
constexpr int formatConfig_initialFormatFlags =(formatConfig_reportPage|formatConfig_reportLineNumber|formatConfig_reportTables|formatConfig_reportHeadings|formatConfig_includeLayoutTables);
constexpr wchar_t PAGE_BREAK_VALUE = L'\x0c';
constexpr wchar_t COLUMN_BREAK_VALUE = L'\x0e';
// A class that disables MS Word screen updating while it is alive.
class ScreenUpdatingDisabler {
private:
IDispatchPtr pDispatchApplication {nullptr};
public:
ScreenUpdatingDisabler(IDispatch* arg_pDispatchApplication) {
if(!arg_pDispatchApplication) {
LOG_ERROR(L"Null application given");
return;
}
HRESULT res=_com_dispatch_raw_propput(arg_pDispatchApplication,wdDISPID_APPLICATION_SCREENUPDATING,VT_BOOL,false);
if(res!=S_OK) {
LOG_WARNING(L"application.screenUpdating false failed with code "<<res);
return;
}
pDispatchApplication=arg_pDispatchApplication;
}
~ScreenUpdatingDisabler() {
if(!pDispatchApplication) return;
HRESULT res=_com_dispatch_raw_propput(pDispatchApplication,wdDISPID_APPLICATION_SCREENUPDATING,VT_BOOL,true);
if(res!=S_OK) {
LOG_WARNING(L"application.screenUpdating true failed with code "<<res);
}
}
};
UINT wm_winword_expandToLine=0;
typedef struct {
int offset;
int lineStart;
int lineEnd;
} winword_expandToLine_args;
void winword_expandToLine_helper(HWND hwnd, winword_expandToLine_args* args) {
//Fetch all needed objects
IDispatchPtr pDispatchWindow=NULL;
if(AccessibleObjectFromWindow(hwnd,OBJID_NATIVEOM,IID_IDispatch,(void**)&pDispatchWindow)!=S_OK||!pDispatchWindow) {
LOG_DEBUGWARNING(L"AccessibleObjectFromWindow failed");
return;
}
IDispatchPtr pDispatchApplication=nullptr;
if(_com_dispatch_raw_propget(pDispatchWindow,wdDISPID_WINDOW_APPLICATION,VT_DISPATCH,&pDispatchApplication)!=S_OK||!pDispatchApplication) {
LOG_DEBUGWARNING(L"window.application failed");
return;
}
// Disable screen updating until the end of this scope
ScreenUpdatingDisabler sud{pDispatchApplication};
IDispatchPtr pDispatchSelection=nullptr;
if(_com_dispatch_raw_propget(pDispatchWindow,wdDISPID_WINDOW_SELECTION,VT_DISPATCH,&pDispatchSelection)!=S_OK||!pDispatchSelection) {
LOG_DEBUGWARNING(L"application.selection failed");
return;
}
BOOL startWasActive=false;
if(_com_dispatch_raw_propget(pDispatchSelection,wdDISPID_SELECTION_STARTISACTIVE,VT_BOOL,&startWasActive)!=S_OK) {
LOG_DEBUGWARNING(L"selection.StartIsActive failed");
}
IDispatch* pDispatchOldSelRange=NULL;
if(_com_dispatch_raw_propget(pDispatchSelection,wdDISPID_SELECTION_RANGE,VT_DISPATCH,&pDispatchOldSelRange)!=S_OK||!pDispatchOldSelRange) {
LOG_DEBUGWARNING(L"selection.range failed");
return;
}
//Move the selection to the given range
_com_dispatch_raw_method(pDispatchSelection,wdDISPID_SELECTION_SETRANGE,DISPATCH_METHOD,VT_EMPTY,NULL,L"\x0003\x0003",args->offset,args->offset);
//Expand the selection to the line
// #3421: Expand and or extending selection cannot be used due to MS Word bugs on the last line in a table cell, or the first/last line of a table of contents, selecting would select the entire object.
// Therefore do it in two steps
bool lineError=false;
if(_com_dispatch_raw_method(pDispatchSelection,wdDISPID_SELECTION_STARTOF,DISPATCH_METHOD,VT_EMPTY,NULL,L"\x0003\x0003",wdLine,0)!=S_OK) {
lineError=true;
} else {
_com_dispatch_raw_propget(pDispatchSelection,wdDISPID_RANGE_START,VT_I4,&(args->lineStart));
if(_com_dispatch_raw_method(pDispatchSelection,wdDISPID_SELECTION_ENDOF,DISPATCH_METHOD,VT_EMPTY,NULL,L"\x0003\x0003",wdLine,0)!=S_OK) {
lineError=true;
} else {
_com_dispatch_raw_propget(pDispatchSelection,wdDISPID_RANGE_END,VT_I4,&(args->lineEnd));
}
// the endOf method has a bug where IPAtEndOfLine gets stuck as true on wrapped lines
// So reset the selection to the start of the document to force it to False
_com_dispatch_raw_method(pDispatchSelection,wdDISPID_SELECTION_SETRANGE,DISPATCH_METHOD,VT_EMPTY,NULL,L"\x0003\x0003",0,0);
}
// Fall back to the older expand if there was an error getting line bounds
if(lineError) {
_com_dispatch_raw_method(pDispatchSelection,wdDISPID_SELECTION_SETRANGE,DISPATCH_METHOD,VT_EMPTY,NULL,L"\x0003\x0003",args->offset,args->offset);
_com_dispatch_raw_method(pDispatchSelection,wdDISPID_RANGE_EXPAND,DISPATCH_METHOD,VT_EMPTY,NULL,L"\x0003",wdLine);
_com_dispatch_raw_propget(pDispatchSelection,wdDISPID_RANGE_START,VT_I4,&(args->lineStart));
_com_dispatch_raw_propget(pDispatchSelection,wdDISPID_RANGE_END,VT_I4,&(args->lineEnd));
}
if(args->lineStart>=args->lineEnd) {
args->lineStart=args->offset;
args->lineEnd=args->offset+1;
}
//Move the selection back to its original location
_com_dispatch_raw_method(pDispatchOldSelRange,wdDISPID_RANGE_SELECT,DISPATCH_METHOD,VT_EMPTY,NULL,NULL);
//Restore the old selection direction
_com_dispatch_raw_propput(pDispatchSelection,wdDISPID_SELECTION_STARTISACTIVE,VT_BOOL,startWasActive);
}
BOOL generateFormFieldXML(IDispatch* pDispatchRange, IDispatchPtr pDispatchRangeExpandedToParagraph, wostringstream& XMLStream, int& chunkEnd) {
IDispatchPtr pDispatchRange2=pDispatchRangeExpandedToParagraph;
BOOL foundFormField=false;
IDispatchPtr pDispatchFormFields=NULL;
_com_dispatch_raw_propget(pDispatchRange2,wdDISPID_RANGE_FORMFIELDS,VT_DISPATCH,&pDispatchFormFields);
if(pDispatchFormFields) for(int count=1;!foundFormField&&count<100;++count) {
IDispatchPtr pDispatchFormField=NULL;
if(_com_dispatch_raw_method(pDispatchFormFields,wdDISPID_FORMFIELDS_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchFormField,L"\x0003",count)!=S_OK||!pDispatchFormField) {
break;
}
IDispatchPtr pDispatchFormFieldRange=NULL;
if(_com_dispatch_raw_propget(pDispatchFormField,wdDISPID_FORMFIELD_RANGE,VT_DISPATCH,&pDispatchFormFieldRange)!=S_OK||!pDispatchFormFieldRange) {
break;
}
if(_com_dispatch_raw_method(pDispatchRange,wdDISPID_RANGE_INRANGE,DISPATCH_METHOD,VT_BOOL,&foundFormField,L"\x0009",pDispatchFormFieldRange)!=S_OK||!foundFormField) {
continue;
}
long fieldType=-1;
_com_dispatch_raw_propget(pDispatchFormField,wdDISPID_FORMFIELD_TYPE,VT_I4,&fieldType);
BSTR fieldResult=NULL;
_com_dispatch_raw_propget(pDispatchFormField,wdDISPID_FORMFIELD_RESULT,VT_BSTR,&fieldResult);
BSTR fieldStatusText=NULL;
_com_dispatch_raw_propget(pDispatchFormField,wdDISPID_FORMFIELD_STATUSTEXT,VT_BSTR,&fieldStatusText);
XMLStream<<L"<control wdFieldType=\""<<fieldType<<L"\" wdFieldResult=\""<<(fieldResult?fieldResult:L"")<<L"\" wdFieldStatusText=\""<<(fieldStatusText?fieldStatusText:L"")<<L"\">";
if(fieldResult) SysFreeString(fieldResult);
if(fieldStatusText) SysFreeString(fieldStatusText);
_com_dispatch_raw_propget(pDispatchFormFieldRange,wdDISPID_RANGE_END,VT_I4,&chunkEnd);
_com_dispatch_raw_propput(pDispatchRange,wdDISPID_RANGE_END,VT_I4,chunkEnd);
break;
}
if(foundFormField) return true;
IDispatchPtr pDispatchContentControls=NULL;
_com_dispatch_raw_propget(pDispatchRange2,wdDISPID_RANGE_CONTENTCONTROLS,VT_DISPATCH,&pDispatchContentControls);
if(pDispatchContentControls)for(int count=1;!foundFormField&&count<100;++count) {
IDispatchPtr pDispatchContentControl=NULL;
if(_com_dispatch_raw_method(pDispatchContentControls,wdDISPID_CONTENTCONTROLS_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchContentControl,L"\x0003",count)!=S_OK||!pDispatchContentControl) {
break;
}
IDispatchPtr pDispatchContentControlRange=NULL;
if(_com_dispatch_raw_propget(pDispatchContentControl,wdDISPID_CONTENTCONTROL_RANGE,VT_DISPATCH,&pDispatchContentControlRange)!=S_OK||!pDispatchContentControlRange) {
break;
}
if(_com_dispatch_raw_method(pDispatchRange,wdDISPID_RANGE_INRANGE,DISPATCH_METHOD,VT_BOOL,&foundFormField,L"\x0009",pDispatchContentControlRange)!=S_OK||!foundFormField) {
continue;
}
long fieldType=-1;
_com_dispatch_raw_propget(pDispatchContentControl,wdDISPID_CONTENTCONTROL_TYPE,VT_I4,&fieldType);
BOOL fieldChecked=false;
_com_dispatch_raw_propget(pDispatchContentControl,wdDISPID_CONTENTCONTROL_CHECKED,VT_BOOL,&fieldChecked);
BSTR fieldTitle=NULL;
_com_dispatch_raw_propget(pDispatchContentControl,wdDISPID_CONTENTCONTROL_TITLE,VT_BSTR,&fieldTitle);
XMLStream<<L"<control wdContentControlType=\""<<fieldType<<L"\" wdContentControlChecked=\""<<fieldChecked<<L"\" wdContentControlTitle=\""<<(fieldTitle?fieldTitle:L"")<<L"\">";
if(fieldTitle) SysFreeString(fieldTitle);
_com_dispatch_raw_propget(pDispatchContentControlRange,wdDISPID_RANGE_END,VT_I4,&chunkEnd);
_com_dispatch_raw_propput(pDispatchRange,wdDISPID_RANGE_END,VT_I4,chunkEnd);
break;
}
return foundFormField;
}
bool collectSpellingErrorOffsets(IDispatchPtr pDispatchRange, vector<pair<long,long>>& errorVector) {
IDispatchPtr pDispatchApplication=NULL;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_APPLICATION ,VT_DISPATCH,&pDispatchApplication)!=S_OK||!pDispatchApplication) {
return false;
}
BOOL isSandbox = false;
// Don't go on if this is sandboxed as collecting spelling errors crashes word
_com_dispatch_raw_propget(pDispatchApplication,wdDISPID_APPLICATION_ISSANDBOX ,VT_BOOL,&isSandbox);
if(isSandbox ) {
return false;
}
IDispatchPtr pDispatchSpellingErrors=NULL;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_SPELLINGERRORS,VT_DISPATCH,&pDispatchSpellingErrors)!=S_OK||!pDispatchSpellingErrors) {
return false;
}
long iVal=0;
_com_dispatch_raw_propget(pDispatchSpellingErrors,wdDISPID_SPELLINGERRORS_COUNT,VT_I4,&iVal);
for(int i=1;i<=iVal;++i) {
IDispatchPtr pDispatchErrorRange=NULL;
if(_com_dispatch_raw_method(pDispatchSpellingErrors,wdDISPID_SPELLINGERRORS_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchErrorRange,L"\x0003",i)!=S_OK||!pDispatchErrorRange) {
return false;
}
long start=0;
if(_com_dispatch_raw_propget(pDispatchErrorRange,wdDISPID_RANGE_START,VT_I4,&start)!=S_OK) {
return false;
}
long end=0;
if(_com_dispatch_raw_propget(pDispatchErrorRange,wdDISPID_RANGE_END,VT_I4,&end)!=S_OK) {
return false;
}
errorVector.push_back(make_pair(start,end));
}
return !errorVector.empty();
}
// #6033: This must not be a static variable inside the function
// because that causes crashes on Windows XP (Visual Studio bug 1941836).
vector<wstring> headingStyleNames;
int getHeadingLevelFromParagraph(IDispatch* pDispatchParagraph) {
IDispatchPtr pDispatchStyle=NULL;
// fetch the localized style name for the given paragraph
if(_com_dispatch_raw_propget(pDispatchParagraph,wdDISPID_PARAGRAPH_STYLE,VT_DISPATCH,&pDispatchStyle)!=S_OK||!pDispatchStyle) {
return 0;
}
BSTR nameLocal=NULL;
_com_dispatch_raw_propget(pDispatchStyle,wdDISPID_STYLE_NAMELOCAL,VT_BSTR,&nameLocal);
if(!nameLocal) {
return 0;
}
// If not fetched already, fetch all builtin heading style localized names (1 through 9).
if(headingStyleNames.empty()) {
IDispatchPtr pDispatchDocument=NULL;
IDispatchPtr pDispatchStyles=NULL;
if(_com_dispatch_raw_propget(pDispatchStyle,wdDISPID_STYLE_PARENT,VT_DISPATCH,&pDispatchDocument)==S_OK&&pDispatchDocument&&_com_dispatch_raw_propget(pDispatchDocument,wdDISPID_DOCUMENT_STYLES,VT_DISPATCH,&pDispatchStyles)==S_OK&&pDispatchStyles) {
for(int i=-2;i>=-10;--i) {
IDispatchPtr pDispatchBuiltinStyle=NULL;
_com_dispatch_raw_method(pDispatchStyles,wdDISPID_STYLES_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchBuiltinStyle,L"\x0003",i);
if(pDispatchBuiltinStyle) {
BSTR builtinNameLocal=NULL;
_com_dispatch_raw_propget(pDispatchBuiltinStyle,wdDISPID_STYLE_NAMELOCAL,VT_BSTR,&builtinNameLocal);
if(!builtinNameLocal) continue;
headingStyleNames.push_back(builtinNameLocal);
SysFreeString(builtinNameLocal);
}
}
}
}
int level=0;
int count=1;
// See if the style name matches one of the builtin heading styles
for(auto i=headingStyleNames.cbegin();i!=headingStyleNames.cend();++i) {
if(i->compare(nameLocal)==0) {
level=count;
break;
}
count+=1;
}
SysFreeString(nameLocal);
return level;
}
int generateHeadingXML(IDispatch* pDispatchParagraph, IDispatch* pDispatchParagraphRange, int startOffset, int endOffset, wostringstream& XMLStream) {
int headingLevel=getHeadingLevelFromParagraph(pDispatchParagraph);
if(!headingLevel) return 0;
XMLStream<<L"<control role=\"heading\" level=\""<<headingLevel<<L"\" ";
// Expose the collapsed state of the heading
BOOL isCollapsed=false;
_com_dispatch_raw_propget(pDispatchParagraph, wdDISPID_PARAGRAPH_COLLAPSED_STATE, VT_BOOL, &isCollapsed);
XMLStream<<L"collapsedState=\""<<(isCollapsed ? L"true" : L"false")<<L"\" ";
if(pDispatchParagraphRange) {
long iVal=0;
if(_com_dispatch_raw_propget(pDispatchParagraphRange,wdDISPID_RANGE_START,VT_I4,&iVal)==S_OK&&iVal>=startOffset) {
XMLStream<<L"_startOfNode=\"1\" ";
}
if(_com_dispatch_raw_propget(pDispatchParagraphRange,wdDISPID_RANGE_END,VT_I4,&iVal)==S_OK&&iVal<=endOffset) {
XMLStream<<L"_endOfNode=\"1\" ";
}
}
XMLStream<<L">";
return 1;
}
IDispatchPtr CreateExpandedDuplicate(IDispatch* pDispatchRange, const int expandTo) {
IDispatchPtr pDispatchRangeDup = nullptr;
auto res = _com_dispatch_raw_propget( pDispatchRange, wdDISPID_RANGE_DUPLICATE, VT_DISPATCH, &pDispatchRangeDup);
if( res != S_OK || !pDispatchRangeDup ) {
LOG_DEBUGWARNING(L"error duplicating the range.");
}
else {
res = _com_dispatch_raw_method( pDispatchRangeDup, wdDISPID_RANGE_EXPAND,DISPATCH_METHOD,VT_EMPTY,NULL,L"\x0003", expandTo);
if( res != S_OK || !pDispatchRangeDup ) {
LOG_DEBUGWARNING(L"error expanding the range");
}
}
return pDispatchRangeDup;
}
bool collectCommentOffsets(IDispatchPtr pDispatchRange, vector<pair<long,long>>& commentVector) {
IDispatchPtr pDispatchComments=NULL;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_COMMENTS,VT_DISPATCH,&pDispatchComments)!=S_OK||!pDispatchComments) {
return false;
}
long iVal=0;
_com_dispatch_raw_propget(pDispatchComments,wdDISPID_COMMENTS_COUNT,VT_I4,&iVal);
for(int i=1;i<=iVal;++i) {
IDispatchPtr pDispatchComment=NULL;
if(_com_dispatch_raw_method(pDispatchComments,wdDISPID_COMMENTS_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchComment,L"\x0003",i)!=S_OK||!pDispatchComment) {
return false;
}
IDispatchPtr pDispatchCommentScope=NULL;
if(_com_dispatch_raw_propget(pDispatchComment,wdDISPID_COMMENT_SCOPE,VT_DISPATCH,&pDispatchCommentScope)!=S_OK||!pDispatchCommentScope) {
return false;
}
long start=0;
if(_com_dispatch_raw_propget(pDispatchCommentScope,wdDISPID_RANGE_START,VT_I4,&start)!=S_OK) {
return false;
}
long end=0;
if(_com_dispatch_raw_propget(pDispatchCommentScope,wdDISPID_RANGE_END,VT_I4,&end)!=S_OK) {
return false;
}
commentVector.push_back(make_pair(start,end));
}
return !commentVector.empty();
}
bool collectRevisionOffsets(IDispatchPtr pDispatchRange, vector<tuple<long,long,long>>& revisionsVector) {
IDispatchPtr pDispatchRevisions=nullptr;
HRESULT res=_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_REVISIONS,VT_DISPATCH,&pDispatchRevisions);
if(res!=S_OK||!pDispatchRevisions) {
LOG_DEBUGWARNING(L"range.revisions failed");
return false;
}
long iVal=0;
_com_dispatch_raw_propget(pDispatchRevisions,wdDISPID_REVISIONS_COUNT,VT_I4,&iVal);
for(int i=1;i<=iVal;++i) {
IDispatchPtr pDispatchRevision=nullptr;
res=_com_dispatch_raw_method(pDispatchRevisions,wdDISPID_REVISIONS_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchRevision,L"\x0003",i);
if(res!=S_OK||!pDispatchRevision) {
LOG_DEBUGWARNING(L"revisions.item "<<i<<L" failed");
continue;
}
long revisionType=0;
res=_com_dispatch_raw_propget(pDispatchRevision,wdDISPID_REVISION_TYPE,VT_I4,&revisionType);
if(res!=S_OK) {
LOG_DEBUGWARNING(L"revision.type failed");
continue;
}
IDispatchPtr pDispatchRevisionScope=nullptr;
_com_dispatch_raw_propget(pDispatchRevision,wdDISPID_REVISION_RANGE,VT_DISPATCH,&pDispatchRevisionScope);
if(res!=S_OK||!pDispatchRevisionScope) {
LOG_DEBUGWARNING(L"revision.range failed");
continue;
}
long start=0;
res=_com_dispatch_raw_propget(pDispatchRevisionScope,wdDISPID_RANGE_START,VT_I4,&start);
if(res!=S_OK) {
LOG_DEBUGWARNING(L"range.start failed");
continue;
}
long end=0;
res=_com_dispatch_raw_propget(pDispatchRevisionScope,wdDISPID_RANGE_END,VT_I4,&end);
if(res!=S_OK) {
LOG_DEBUGWARNING(L"range.end failed");
continue;
}
revisionsVector.push_back({start,end,revisionType});
}
return !revisionsVector.empty();
}
bool fetchTableInfo(IDispatch* pDispatchTable, bool includeLayoutTables, int* rowCount, int* columnCount, int* nestingLevel) {
IDispatchPtr pDispatchRows=NULL;
IDispatchPtr pDispatchColumns=NULL;
IDispatchPtr pDispatchBorders=NULL;
if(!includeLayoutTables&&_com_dispatch_raw_propget(pDispatchTable,wdDISPID_TABLE_BORDERS,VT_DISPATCH,&pDispatchBorders)==S_OK&&pDispatchBorders) {
BOOL isEnabled=true;
if(_com_dispatch_raw_propget(pDispatchBorders,wdDISPID_BORDERS_ENABLE,VT_BOOL,&isEnabled)==S_OK&&!isEnabled) {
return false;
}
}
if(_com_dispatch_raw_propget(pDispatchTable,wdDISPID_TABLE_ROWS,VT_DISPATCH,&pDispatchRows)==S_OK&&pDispatchRows) {
_com_dispatch_raw_propget(pDispatchRows,wdDISPID_ROWS_COUNT,VT_I4,rowCount);
}
if(_com_dispatch_raw_propget(pDispatchTable,wdDISPID_TABLE_COLUMNS,VT_DISPATCH,&pDispatchColumns)==S_OK&&pDispatchColumns) {
_com_dispatch_raw_propget(pDispatchColumns,wdDISPID_COLUMNS_COUNT,VT_I4,columnCount);
}
_com_dispatch_raw_propget(pDispatchTable,wdDISPID_TABLE_NESTINGLEVEL,VT_I4,nestingLevel);
return true;
}
int generateTableXML(IDispatch* pDispatchRange, bool includeLayoutTables, int startOffset, int endOffset, wostringstream& XMLStream) {
int numTags=0;
int iVal=0;
IDispatchPtr pDispatchTables=NULL;
IDispatchPtr pDispatchTable=NULL;
bool inTableCell=false;
int rowCount=0;
int columnCount=0;
int nestingLevel=0;
int rowNumber=0;
int columnNumber=0;
bool startOfCell=false;
bool endOfCell=false;
if(
_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_TABLES,VT_DISPATCH,&pDispatchTables)!=S_OK||!pDispatchTables\
||_com_dispatch_raw_method(pDispatchTables,wdDISPID_TABLES_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchTable,L"\x0003",1)!=S_OK||!pDispatchTable\
||!fetchTableInfo(pDispatchTable,includeLayoutTables,&rowCount,&columnCount,&nestingLevel)\
) {
return 0;
}
IDispatchPtr pDispatchCells=NULL;
IDispatchPtr pDispatchCell=NULL;
if(
_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_CELLS,VT_DISPATCH,&pDispatchCells)==S_OK&&pDispatchCells\
&&_com_dispatch_raw_method(pDispatchCells,wdDISPID_CELLS_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchCell,L"\x0003",1)==S_OK&&pDispatchCell\
) {
_com_dispatch_raw_propget(pDispatchCell,wdDISPID_CELL_ROWINDEX,VT_I4,&rowNumber);
_com_dispatch_raw_propget(pDispatchCell,wdDISPID_CELL_COLUMNINDEX,VT_I4,&columnNumber);
IDispatchPtr pDispatchCellRange=NULL;
if(_com_dispatch_raw_propget(pDispatchCell,wdDISPID_CELL_RANGE,VT_DISPATCH,&pDispatchCellRange)==S_OK&&pDispatchCellRange) {
if(_com_dispatch_raw_propget(pDispatchCellRange,wdDISPID_RANGE_START,VT_I4,&iVal)==S_OK&&iVal>=startOffset) {
startOfCell=true;
}
if(_com_dispatch_raw_propget(pDispatchCellRange,wdDISPID_RANGE_END,VT_I4,&iVal)==S_OK&&iVal<=endOffset) {
endOfCell=true;
}
}
inTableCell=true;
} else {
if((_com_dispatch_raw_method(pDispatchRange,wdDISPID_RANGE_INFORMATION,DISPATCH_PROPERTYGET,VT_I4,&rowNumber,L"\x0003",wdStartOfRangeRowNumber)==S_OK)&&rowNumber>0) {
inTableCell=true;
}
if((_com_dispatch_raw_method(pDispatchRange,wdDISPID_RANGE_INFORMATION,DISPATCH_PROPERTYGET,VT_I4,&columnNumber,L"\x0003",wdStartOfRangeColumnNumber)==S_OK)&&columnNumber>0) {
inTableCell=true;
}
}
if(!inTableCell) return numTags;
numTags+=2;
XMLStream<<L"<control role=\"table\" table-id=\"1\" table-rowcount=\""<<rowCount<<L"\" table-columncount=\""<<columnCount<<L"\" level=\""<<nestingLevel<<L"\" ";
wstring altTextStr=L"";
BSTR altText=NULL;
if(_com_dispatch_raw_propget(pDispatchTable,wdDISPID_TABLE_TITLE,VT_BSTR,&altText)==S_OK&&altText) {
for(int i=0;altText[i]!='\0';++i) {
appendCharToXML(altText[i],altTextStr,true);
}
SysFreeString(altText);
}
if(!altTextStr.empty()) {
XMLStream<<L"alwaysReportName=\"1\" name=\""<<altTextStr<<L"\" ";
altTextStr=L"";
}
altText=NULL;
if(_com_dispatch_raw_propget(pDispatchTable,wdDISPID_TABLE_DESCR,VT_BSTR,&altText)==S_OK&&altText) {
for(int i=0;altText[i]!='\0';++i) {
appendCharToXML(altText[i],altTextStr,true);
}
XMLStream<<L"longdescription=\""<<altTextStr<<L"\" ";
SysFreeString(altText);
}
IDispatchPtr pDispatchTableRange=NULL;
if(_com_dispatch_raw_propget(pDispatchTable,wdDISPID_TABLE_RANGE,VT_DISPATCH,&pDispatchTableRange)==S_OK&&pDispatchTableRange) {
if(_com_dispatch_raw_propget(pDispatchTableRange,wdDISPID_RANGE_START,VT_I4,&iVal)==S_OK&&iVal>=startOffset) {
XMLStream<<L"_startOfNode=\"1\" ";
}
if(_com_dispatch_raw_propget(pDispatchTableRange,wdDISPID_RANGE_END,VT_I4,&iVal)==S_OK&&iVal<=endOffset) {
XMLStream<<L"_endOfNode=\"1\" ";
}
}
XMLStream<<L">";
XMLStream<<L"<control role=\"tableCell\" table-id=\"1\" ";
XMLStream<<L"table-rownumber=\""<<rowNumber<<L"\" ";
XMLStream<<L"table-columnnumber=\""<<columnNumber<<L"\" ";
if(startOfCell) {
XMLStream<<L"_startOfNode=\"1\" ";
}
if(endOfCell) {
XMLStream<<L"_endOfNode=\"1\" ";
}
XMLStream<<L">";
return numTags;
}
void generateXMLAttribsForFormatting(IDispatch* pDispatchRange, int startOffset, int endOffset, int formatConfig, wostringstream& formatAttribsStream) {
int iVal=0;
if((formatConfig&formatConfig_reportPage)&&(_com_dispatch_raw_method(pDispatchRange,wdDISPID_RANGE_INFORMATION,DISPATCH_PROPERTYGET,VT_I4,&iVal,L"\x0003",wdActiveEndAdjustedPageNumber)==S_OK)&&iVal>0) {
formatAttribsStream<<L"page-number=\""<<iVal<<L"\" ";
}
if((formatConfig&formatConfig_reportLineNumber)&&(_com_dispatch_raw_method(pDispatchRange,wdDISPID_RANGE_INFORMATION,DISPATCH_PROPERTYGET,VT_I4,&iVal,L"\x0003",wdFirstCharacterLineNumber)==S_OK)) {
formatAttribsStream<<L"line-number=\""<<iVal<<L"\" ";
}
if (
(formatConfig & formatConfig_reportColor)
|| (formatConfig & formatConfig_reportAlignment)
|| (formatConfig & formatConfig_reportParagraphIndentation)
|| (formatConfig & formatConfig_reportLineSpacing)
) {
IDispatchPtr pDispatchParagraphFormat=NULL;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_PARAGRAPHFORMAT,VT_DISPATCH,&pDispatchParagraphFormat)==S_OK&&pDispatchParagraphFormat) {
if (formatConfig & formatConfig_reportColor) {
IDispatchPtr pDispatchParagraphFormatShading = NULL;
if (
_com_dispatch_raw_propget(pDispatchParagraphFormat, wdDISPID_PARAGRAPHFORMAT_SHADING, VT_DISPATCH, &pDispatchParagraphFormatShading) == S_OK
&& pDispatchParagraphFormatShading
) {
int bgColor = 0;
if (
_com_dispatch_raw_propget(pDispatchParagraphFormatShading, wdDISPID_SHADING_BACKGROUNDPATTERNCOLOR, VT_I4, &bgColor) == S_OK
) {
formatAttribsStream << L"background-color=\"" << bgColor << L"\" ";
}
}
}
if(formatConfig&formatConfig_reportAlignment) {
if(_com_dispatch_raw_propget(pDispatchParagraphFormat,wdDISPID_PARAGRAPHFORMAT_ALIGNMENT,VT_I4,&iVal)==S_OK) {
switch(iVal) {
case wdAlignParagraphLeft:
formatAttribsStream<<L"text-align=\"left\" ";
break;
case wdAlignParagraphCenter:
formatAttribsStream<<L"text-align=\"center\" ";
break;
case wdAlignParagraphRight:
formatAttribsStream<<L"text-align=\"right\" ";
break;
case wdAlignParagraphJustify:
formatAttribsStream<<L"text-align=\"justify\" ";
break;
case wdAlignParagraphDistribute:
formatAttribsStream<<L"text-align=\"distribute\" ";
break;
}
}
}
float fVal=0.0;
if(formatConfig&formatConfig_reportParagraphIndentation) {
if(_com_dispatch_raw_propget(pDispatchParagraphFormat,wdDISPID_PARAGRAPHFORMAT_RIGHTINDENT,VT_R4,&fVal)==S_OK) {
formatAttribsStream<<L"right-indent=\"" << fVal <<L"\" ";
}
float firstLineIndent=0;
if(_com_dispatch_raw_propget(pDispatchParagraphFormat,wdDISPID_PARAGRAPHFORMAT_FIRSTLINEINDENT,VT_R4,&firstLineIndent)==S_OK) {
if(firstLineIndent<0) {
formatAttribsStream<<L"hanging-indent=\"" << (0-firstLineIndent) <<L"\" ";
} else {
formatAttribsStream<<L"first-line-indent=\"" << firstLineIndent <<L"\" ";
}
}
if(_com_dispatch_raw_propget(pDispatchParagraphFormat,wdDISPID_PARAGRAPHFORMAT_LEFTINDENT,VT_R4,&fVal)==S_OK) {
if(firstLineIndent<0) fVal+=firstLineIndent;
formatAttribsStream<<L"left-indent=\"" << fVal <<L"\" ";
}
}
if(formatConfig&formatConfig_reportLineSpacing) {
if(_com_dispatch_raw_propget(pDispatchParagraphFormat,wdDISPID_PARAGRAPHFORMAT_LINESPACINGRULE,VT_I4,&iVal)==S_OK) {
formatAttribsStream<<L"wdLineSpacingRule=\"" << iVal <<L"\" ";
}
if(_com_dispatch_raw_propget(pDispatchParagraphFormat,wdDISPID_PARAGRAPHFORMAT_LINESPACING,VT_R4,&fVal)==S_OK) {
formatAttribsStream<<L"wdLineSpacing=\"" << fVal <<L"\" ";
}
}
}
}
if(formatConfig&formatConfig_reportLists) {
IDispatchPtr pDispatchListFormat=NULL;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_LISTFORMAT,VT_DISPATCH,&pDispatchListFormat)==S_OK&&pDispatchListFormat) {
BSTR listString=NULL;
if(_com_dispatch_raw_propget(pDispatchListFormat,wdDISPID_LISTFORMAT_LISTSTRING,VT_BSTR,&listString)==S_OK&&listString) {
if(SysStringLen(listString)>0) {
IDispatchPtr pDispatchParagraphs=NULL;
IDispatchPtr pDispatchParagraph=NULL;
IDispatchPtr pDispatchParagraphRange=NULL;
if(
_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_PARAGRAPHS,VT_DISPATCH,&pDispatchParagraphs)==S_OK&&pDispatchParagraphs\
&&_com_dispatch_raw_method(pDispatchParagraphs,wdDISPID_PARAGRAPHS_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchParagraph,L"\x0003",1)==S_OK&&pDispatchParagraph\
&&_com_dispatch_raw_propget(pDispatchParagraph,wdDISPID_PARAGRAPH_RANGE,VT_DISPATCH,&pDispatchParagraphRange)==S_OK&&pDispatchParagraphRange\
&&_com_dispatch_raw_propget(pDispatchParagraphRange,wdDISPID_RANGE_START,VT_I4,&iVal)==S_OK&&iVal==startOffset\
) {
wstring tempText;
for(int i=0;listString[i]!=L'\0';++i) {
appendCharToXML(listString[i],tempText,true);
}
formatAttribsStream<<L"line-prefix=\""<<tempText<<L"\" ";
}
}
SysFreeString(listString);
}
}
}
if(formatConfig&formatConfig_reportStyle) {
IDispatchPtr pDispatchStyle=NULL;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_STYLE,VT_DISPATCH,&pDispatchStyle)==S_OK&&pDispatchStyle) {
BSTR nameLocal=NULL;
_com_dispatch_raw_propget(pDispatchStyle,wdDISPID_STYLE_NAMELOCAL,VT_BSTR,&nameLocal);
if(nameLocal) {
formatAttribsStream<<L"style=\""<<nameLocal<<L"\" ";
SysFreeString(nameLocal);
}
}
}
if(formatConfig&formatConfig_fontFlags) {
IDispatchPtr pDispatchFont=NULL;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_FONT,VT_DISPATCH,&pDispatchFont)==S_OK&&pDispatchFont) {
BSTR fontName=NULL;
if((formatConfig&formatConfig_reportFontName)&&(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_NAME,VT_BSTR,&fontName)==S_OK)&&fontName) {
formatAttribsStream<<L"font-name=\""<<fontName<<L"\" ";
SysFreeString(fontName);
}
float fVal=0.0;
if((formatConfig&formatConfig_reportFontSize)&&(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_SIZE,VT_R4,&fVal)==S_OK)) {
formatAttribsStream<<L"font-size=\""<<fVal<<L"\" ";
}
if((formatConfig&formatConfig_reportColor)&&(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_COLOR,VT_I4,&iVal)==S_OK)) {
formatAttribsStream<<L"color=\""<<iVal<<L"\" ";
}
if(formatConfig&formatConfig_reportFontAttributes) {
if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_BOLD,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"bold=\"1\" ";
}
if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_ITALIC,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"italic=\"1\" ";
}
if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_UNDERLINE,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"underline=\"1\" ";
}
if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_STRIKETHROUGH,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"strikethrough=\"1\" ";
} else if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_DOUBLESTRIKETHROUGH,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"strikethrough=\"double\" ";
}
if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_HIDDEN,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"hidden=\"1\" ";
}
}
if(formatConfig&formatConfig_reportSuperscriptsAndSubscripts) {
if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_SUPERSCRIPT,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"text-position=\"super\" ";
} else if(_com_dispatch_raw_propget(pDispatchFont,wdDISPID_FONT_SUBSCRIPT,VT_I4,&iVal)==S_OK&&iVal) {
formatAttribsStream<<L"text-position=\"sub\" ";
}
}
}
}
int hlColorIndex = 0;
if (
(formatConfig & formatConfig_reportHighlightColor)
&& (_com_dispatch_raw_propget(pDispatchRange, wdDISPID_RANGE_HIGHLIGHTCOLORINDEX, VT_I4, &hlColorIndex) == S_OK)
&& (hlColorIndex > 0)
) {
formatAttribsStream << L"highlight-color-index=\"" << hlColorIndex << L"\" ";
}
if (formatConfig&formatConfig_reportLanguage) {
int languageId = 0;
if (_com_dispatch_raw_propget(pDispatchRange, wdDISPID_RANGE_LANGUAGEID, VT_I4, &languageId)==S_OK) {
if (languageId != wdLanguageNone && languageId != wdNoProofing && languageId != wdLanguageUnknown) {
formatAttribsStream<<L"wdLanguageId=\""<<languageId<<L"\" ";
}
}
}
}
inline int getInlineShapesCount(IDispatch* pDispatchRange) {
IDispatchPtr pDispatchShapes=NULL;
int count=0;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_INLINESHAPES,VT_DISPATCH,&pDispatchShapes)!=S_OK||!pDispatchShapes) {
return 0;
}
if(_com_dispatch_raw_propget(pDispatchShapes,wdDISPID_INLINESHAPES_COUNT,VT_I4,&count)!=S_OK||count<=0) {
return 0;
}
return count;
}
/**
* Generates an opening tag for the first inline shape in this range if one exists.
* If the function is successful, the total number of inline shapes for this range is returned allowing the caller to then perhaps move the range forward a character and try again.
*/
inline int generateInlineShapeXML(IDispatch* pDispatchRange, int offset, wostringstream& XMLStream) {
IDispatchPtr pDispatchShapes=NULL;
IDispatchPtr pDispatchShape=NULL;
IDispatchPtr pDispatchChart=NULL;
IDispatchPtr pDispatchChartTitle=NULL;
int count=0;
int shapeType=0;
BSTR altText=NULL;
BOOL shapeHasChart=false;
BOOL chartHasTitle=false;
if(_com_dispatch_raw_propget(pDispatchRange,wdDISPID_RANGE_INLINESHAPES,VT_DISPATCH,&pDispatchShapes)!=S_OK||!pDispatchShapes) {
return 0;
}
if(_com_dispatch_raw_propget(pDispatchShapes,wdDISPID_INLINESHAPES_COUNT,VT_I4,&count)!=S_OK||count<=0) {
return 0;
}
if(_com_dispatch_raw_method(pDispatchShapes,wdDISPID_INLINESHAPES_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchShape,L"\x0003",1)!=S_OK||!pDispatchShape) {
return 0;
}
if(_com_dispatch_raw_propget(pDispatchShape,wdDISPID_INLINESHAPE_TYPE,VT_I4,&shapeType)!=S_OK) {
return 0;
}
wstring altTextStr=L"";
if(_com_dispatch_raw_propget(pDispatchShape,wdDISPID_INLINESHAPE_ALTERNATIVETEXT,VT_BSTR,&altText)==S_OK&&altText) {
for(int i=0;altText[i]!='\0';++i) {
appendCharToXML(altText[i],altTextStr,true);
}
SysFreeString(altText);
}
altText=NULL;
if(altTextStr.empty()&&_com_dispatch_raw_propget(pDispatchShape,wdDISPID_INLINESHAPE_TITLE,VT_BSTR,&altText)==S_OK&&altText) {
for(int i=0;altText[i]!='\0';++i) {
appendCharToXML(altText[i],altTextStr,true);
}
SysFreeString(altText);
}
altText=NULL;
XMLStream<<L"<control _startOfNode=\"1\" ";
auto roleText = L"object";
if(shapeType==wdInlineShapePicture||shapeType==wdInlineShapeLinkedPicture) {
roleText=L"graphic";
} else if(shapeType==wdInlineShapeChart) {
roleText=L"chart";
}
XMLStream<<L"role=\""<<roleText<<L"\" ";
XMLStream<<L"content=\""<<altTextStr<<L"\"";
if(shapeType==wdInlineShapeEmbeddedOLEObject) {
XMLStream<<L" shapeoffset=\""<<offset<<L"\"";
IDispatchPtr pOLEFormat=NULL;
if(_com_dispatch_raw_propget(pDispatchShape,wdDISPID_INLINESHAPE_OLEFORMAT,VT_DISPATCH,&pOLEFormat)==S_OK) {
BSTR progId=NULL;
if(_com_dispatch_raw_propget(pOLEFormat,wdDISPID_OLEFORMAT_PROGID,VT_BSTR,&progId)==S_OK&&progId) {
XMLStream<<L" progid=\""<<progId<<"\"";
SysFreeString(progId);
}
}
}
XMLStream<<L">";
return count;
}
inline bool generateFootnoteEndnoteXML(IDispatch* pDispatchRange, wostringstream& XMLStream, bool footnote) {
IDispatchPtr pDispatchNotes=NULL;
IDispatchPtr pDispatchNote=NULL;
int count=0;
int index=0;
if(_com_dispatch_raw_propget(pDispatchRange,(footnote?wdDISPID_RANGE_FOOTNOTES:wdDISPID_RANGE_ENDNOTES),VT_DISPATCH,&pDispatchNotes)!=S_OK||!pDispatchNotes) {
return false;
}
if(_com_dispatch_raw_propget(pDispatchNotes,wdDISPID_FOOTNOTES_COUNT,VT_I4,&count)!=S_OK||count<=0) {
return false;
}
if(_com_dispatch_raw_method(pDispatchNotes,wdDISPID_FOOTNOTES_ITEM,DISPATCH_METHOD,VT_DISPATCH,&pDispatchNote,L"\x0003",1)!=S_OK||!pDispatchNote) {
return false;
}
if(_com_dispatch_raw_propget(pDispatchNote,wdDISPID_FOOTNOTE_INDEX,VT_I4,&index)!=S_OK) {
return false;
}
XMLStream<<L"<control _startOfNode=\"1\" role=\""<<(footnote?L"footnote":L"endnote")<<L"\" value=\""<<index<<L"\">";
return true;
}
std::optional<int> getSectionBreakType(IDispatchPtr pDispatchRange ) {
// The following case should handle where we have the page break character ('0x0c') shown with '|p|'
// first section|p|
// second section.
// range.Sections[1].pageSetup.SectionStart tells you how the section started, so we need to know
// the next sections start type to report what kind of break this is. To do this we need to expand
// the range, get the section start type, remove the page break character, and insert an attribute
// for the break type.
IDispatchPtr pDispatchRangeDup = nullptr;
auto res = _com_dispatch_raw_propget( pDispatchRange, wdDISPID_RANGE_DUPLICATE, VT_DISPATCH, &pDispatchRangeDup);
if( res != S_OK || !pDispatchRangeDup ) {
LOG_DEBUGWARNING(L"error duplicating the range.");
return {};
}
// we assume that we are 1 character away from the next section, this should be the value for PAGE_BREAK_VALUE ("0x0c")
const int unitsToMove = 1;
int unitsMoved=-1;
res = _com_dispatch_raw_method(pDispatchRangeDup,wdDISPID_RANGE_MOVEEND,DISPATCH_METHOD,VT_I4,&unitsMoved,L"\x0003\x0003",wdCharacter,unitsToMove);
if( res !=S_OK || unitsMoved<=0 || !pDispatchRangeDup) {
LOG_DEBUGWARNING(L"error moving the end of the range");
return {};
}
IDispatchPtr pDispatchSections = nullptr;
res = _com_dispatch_raw_propget( pDispatchRangeDup, wdDISPID_RANGE_SECTIONS, VT_DISPATCH, &pDispatchSections);
if( res != S_OK || !pDispatchSections ) {
LOG_DEBUGWARNING(L"error getting sections from range");
return {};
}
int count = -1;
res = _com_dispatch_raw_propget( pDispatchSections, wdDISPID_SECTIONS_COUNT, VT_I4, &count);
if( res != S_OK || count != 2 ) {
LOG_DEBUGWARNING(L"error getting section count. There should be exactly 2 sections, count: " << count);
return {};
}
// we make the assumption that the second section will always be the one we want. We also assume that the section
// count was 1 before expanding the range.
const int sectionToGet = 2;
IDispatchPtr pDispatchItem = nullptr;
res = _com_dispatch_raw_method( pDispatchSections, wdDISPID_SECTIONS_ITEM, DISPATCH_METHOD, VT_DISPATCH, &pDispatchItem, L"\x0003", sectionToGet);
if( res != S_OK || !pDispatchItem){
LOG_DEBUGWARNING(L"error getting section item");
return {};
}
IDispatchPtr pDispatchPageSetup = nullptr;
res = _com_dispatch_raw_propget( pDispatchItem, wdDISPID_SECTION_PAGESETUP, VT_DISPATCH, &pDispatchPageSetup);
if( res != S_OK || !pDispatchPageSetup){
LOG_DEBUGWARNING(L"error getting pageSetup");
return {};
}
int type = -1;
res = _com_dispatch_raw_propget( pDispatchPageSetup, wdDISPID_PAGESETUP_SECTIONSTART, VT_I4, &type);
if( res != S_OK || type < 0){
LOG_DEBUGWARNING(L"error getting section start");
return {};
}
LOG_DEBUG(L"Got section break type: " << type);
return type;
}
std::optional<float>
getStartOfRangeDistanceFromEdgeOfDocument(IDispatchPtr pDispatchRange) {
float rangePos = -1.0f;
auto res = _com_dispatch_raw_method( pDispatchRange, wdDISPID_RANGE_INFORMATION,
DISPATCH_PROPERTYGET, VT_R4, &rangePos, L"\x0003", wdHorizontalPositionRelativeToPage);
if( S_OK != res && rangePos < 0) {
LOG_ERROR(L"error getting wdHorizontalPositionRelativeToPage."
<< " res: " << res
<< " rangePos: " << rangePos);
return {};
}
return rangePos;
}
std::optional< std::pair<float, float> >
calculatePreAndPostColumnOffsets(IDispatchPtr pDispatchPageSetup) {
float leftMargin = -1.0f;
auto res = _com_dispatch_raw_propget( pDispatchPageSetup, wdDISPID_PAGESETUP_LEFTMARGIN,
VT_R4, &leftMargin);
if( res != S_OK || leftMargin <0){
LOG_ERROR(L"error getting leftMargin. res: "<< res
<< " leftMargin: " << leftMargin);
return {};
}
float rightMargin = -1.0f;
// right margin necessary to validate that the full width of the document has been
// taken into account.
res = _com_dispatch_raw_propget( pDispatchPageSetup, wdDISPID_PAGESETUP_RIGHTMARGIN,
VT_R4, &rightMargin);
if( res != S_OK || rightMargin <0){
LOG_ERROR(L"error getting rightMargin. res: "<< res
<< " rightMargin: " << rightMargin);
return {};
}
float gutter = -1.0f;
res = _com_dispatch_raw_propget( pDispatchPageSetup, wdDISPID_PAGESETUP_GUTTER,
VT_R4, &gutter);
if( res != S_OK || gutter <0){
LOG_ERROR(L"error getting gutter. res: "<< res
<< " gutter: " << gutter);
return {};
}
int gutterPos = -1;
res = _com_dispatch_raw_propget( pDispatchPageSetup, wdDISPID_PAGESETUP_GUTTERPOS,
VT_R4, &gutterPos);
if( res != S_OK || gutterPos <0){
LOG_ERROR(L"error getting gutterPos. res: "<< res
<< " gutterPos: " << gutterPos);
return {};
}
int mirrorMargins = wdUndefined;
res = _com_dispatch_raw_propget( pDispatchPageSetup, wdDISPID_PAGESETUP_MIRRORMARGINS,
VT_R4, &mirrorMargins);
if( res != S_OK || mirrorMargins == wdUndefined){
LOG_ERROR(L"error getting mirrorMargins. res: "<< res
<< " mirrorMargins: " << mirrorMargins);
return {};
}
// We do not handle mirror margins being set since the gutter alternates between the
// left and the right making it unclear which side to add it to.
if( mirrorMargins != FALSE) {
LOG_DEBUGWARNING(L"Unable to calculate the start and \
end margins due to mirror margins being enabled.");
return {};
}
switch (gutterPos) {
case wdGutterPosLeft:
return std::make_pair(gutter + leftMargin, rightMargin);
case wdGutterPosRight:
return std::make_pair(leftMargin, rightMargin + gutter);
default:
return std::make_pair(leftMargin, rightMargin);
}
}
void detectAndGenerateColumnFormatXML(IDispatchPtr pDispatchRange, wostringstream& xmlStream) {
// 1. Get the count of columns, its important we do this first so that in the event that
// calculating the column we are in fails, we are still able to report the overall count
IDispatchPtr pDispatchPageSetup = nullptr;
auto res = _com_dispatch_raw_propget( pDispatchRange, wdDISPID_RANGE_PAGESETUP,
VT_DISPATCH, &pDispatchPageSetup);
if( res != S_OK || !pDispatchPageSetup){
LOG_ERROR(L"error getting pageSetup. res: "<< res);
return;
}
// Get columns collection
IDispatchPtr pDispatchTextColumns = nullptr;
res = _com_dispatch_raw_propget( pDispatchPageSetup, wdDISPID_PAGESETUP_TEXTCOLUMNS,
VT_DISPATCH, &pDispatchTextColumns);
if( res != S_OK || !pDispatchTextColumns){
LOG_ERROR(L"error getting textColumns. res: "<< res);
return;
}
// Count of columns
int count = -1;
res = _com_dispatch_raw_propget( pDispatchTextColumns, wdDISPID_TEXTCOLUMNS_COUNT,
VT_I4, &count);
if( res != S_OK || count < 0){
LOG_DEBUG(L"Unable to get textColumn count. We may be in a 'comment'. res: "<< res
<< " count: " << count);
return;
}
xmlStream <<L"text-column-count=\"" << count << "\" ";
// Optimization: If there is only one column, then we can only be in that column.
if(count<=1) {
if(count==1) {
xmlStream <<L"text-column-number=\"" << 1 << "\" ";
}
return;
}
// 2. Get the start position (IN POINTS) of the range. This is relative to the start
// of the document.
auto rangeStart = getStartOfRangeDistanceFromEdgeOfDocument(pDispatchRange);
if(!rangeStart) {
return;
}
// 3. Get the offsets that come before and after the columns, this is a combination
// of left margin, gutter and right margin.
auto prePostColumnOffsets = calculatePreAndPostColumnOffsets(pDispatchPageSetup);
if(!prePostColumnOffsets){
return;
}
// 4. Iterate through the columns, look for the final column where the range start