Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SAP OLE- Word doc text highlighting

jatin_singhal
Participant
0 Kudos

Hi,

How can I fulfill below requirement.

screenshot-20190212-224325

I am using below code. What change can I do in this to achieve above requirement.

screenshot-20190212-230025.png

It's urgent. Thanks in advance.

14 REPLIES 14

Sandra_Rossi
Active Contributor

Can't you paste the code normally instead of image? (and use button CODE to display it correctly)

Sandra_Rossi
Active Contributor
0 Kudos

Your piece of code looks correct based on the Word macro recorder, except the value 65535. The valid range goes from 0 (highlight off) to 18 (classic blue). Yellow is 7.

Here is the VBA code obtained via the Word macro recorder:

Selection.Range.HighlightColorIndex = wdYellow

Object browser for color constant values:

0 Kudos

Hi Sandra thank you very much. I'll try and will let u know. Actually yesterday night I asked this question using my phone. I'll post code and requirement properly today

0 Kudos

Hi Sandra can u give 2 lines of code for this

Let me rewrite textually the code you have posted in your initial screenshot:

GET PROPERTY OF g_selection 'Range' = g_wrange.
SET PROPERTY OF g_wrange 'HighlightColorIndex' = '65535'.

Why not using 7 (yellow) instead of 65535 ?

Maybe you have an issue with the initialization of g_selection?

0 Kudos

Hi Sandra, seems i can achieve it. Using above two lines whole of the data is highlighted in yellow. Entire page is highlighted in yellow.

So have to change something in above two lines. Great, if you can suggest.

0 Kudos

So, as I said, you have an issue with the initialization of g_selection, so just select the right portion of text... But I can't help if you don't post the concerned code !

0 Kudos

I think g_selection is working, but whole range of code gets colored with yellow color .

and also the entire below it also gets colored. I only want XXXXX to be colored with yellow.

g_selection initialization

GET PROPERTY OF g_application 'Selection' = g_selection.

GET PROPERTY OF g_application 'Selection' = g_selection1. "For Table
GET PROPERTY OF g_selection 'Font' = g_font.
GET PROPERTY OF g_selection 'ParagraphFormat' = g_parformat.

Now i am using below code to achieve

* Set Bold
IF p_boldx NE space.
SET PROPERTY OF g_font 'Bold' = p_bold.
SET PROPERTY OF g_font 'BackgroundPatternColor' = 7.
SET PROPERTY OF g_font 'HighlightColorIndex' = 7.
ENDIF.

GET PROPERTY OF g_selection g_font = g_wrange.
SET PROPERTY OF g_wrange 'HighlightColorIndex' = 7.

0 Kudos

The selection is not working well because it doesn't highlight the right portion. How did you select the portion of text with the OLE method SELECT ? Or you may work without the selection, just get the RANGE object corresponding to the text you want to highlight.

0 Kudos

Hi Sandra,

As per ur comments I used below code but no help, can you modify the code that can help.

GET PROPERTY OF g_font 'Selection' = g_selection.
GET PROPERTY OF g_selection 'Range' = g_wrange.
SET PROPERTY OF g_wrange 'HighlightColorIndex' = 7.

In initialization

GET PROPERTY OF g_application 'Selection' = g_selection.

***********************************************************************

Whole code of form

* Set Bold
IF p_boldx NE space.
SET PROPERTY OF g_font 'Bold' = p_bold.
SET PROPERTY OF g_font 'BackgroundPatternColor' = 7.
SET PROPERTY OF g_font 'HighlightColorIndex' = 7.
ENDIF.


IF p_ulinex NE space.
SET PROPERTY OF g_font 'Underline' = p_uline.
ENDIF.

* Set Alignment
IF p_alignx NE space.
SET PROPERTY OF g_parformat 'Alignment' = p_align. " 0-Left 1-Centered 2-Right
ENDIF.

* Value
CALL METHOD OF
g_selection
'TypeText'

EXPORTING
#1 = p_text.

0 Kudos

Hi Sandra,

How to get RANGE object corresponding to the text you want to highlight.

I am using text as p_text and font g_font

I used below to get RANGE corresponding to my text, but no help


CALL METHOD OF
g_font
'RANGE' = g_WRANGE.

* GET PROPERTY OF g_selection 'Range' = g_wrange.
SET PROPERTY OF g_wrange 'HighlightColorIndex' = 7.

0 Kudos

You should learn the OLE model of Word first. The best way is to first write your code with MS Word VBA and when it works, rewrite it into ABAP (see this OLE equivalence VBA versus ABAP - it's for Excel but the same equivalence logic works for Word). It's easy with the VBA macro recorder, from the debugger and from the VBA language reference documentation. You may also get some help from Microsoft Word forums. It must be clear to you that your question is not related to ABAP but to MS Word OLE.

A MS Word range object can be obtained by various means. Here you show that you get the range property from "g_font" but the only thing I can say is that it's not a font object. Based on your code, the "g_font" object has a Selection property, then "g_font" might be an Excel (Application) or Window object.

I recommend you to search examples for VBA and HighlightColorIndex, then try these examples and adapt them so that to achieve your goal with the right properties and methods, then translate into ABAP.

0 Kudos

Ok will try. Thanks for suggestions 🙂

jatin_singhal
Participant
0 Kudos

HI Sandra,

In my word document created using SAP OLE currently the output is

Expected output is

I want the Yellow highlighting in the text.

For this I have used below code but its not helping.

*******************************************************************

* Set Bold
IF p_boldx NE space.
SET PROPERTY OF g_font 'Bold' = p_bold.
* SET PROPERTY OF g_font 'BackgroundPatternColor' = '65535'.
* SET PROPERTY OF g_font 'HighlightColorIndex' = '65535'.
ENDIF.
* SET PROPERTY OF g_font 'Subscript' = '1'.
* SET PROPERTY OF g_font 'DiacriticColor' = '65535'.

GET PROPERTY OF g_font 'Interior' = g_color1.
SET PROPERTY OF g_color1 'ColorIndex' = '6'.
* Set Underline
IF p_ulinex NE space.
SET PROPERTY OF g_font 'Underline' = p_uline.
ENDIF.

* Set Alignment
IF p_alignx NE space.
SET PROPERTY OF g_parformat 'Alignment' = p_align. " 0-Left 1-Centered 2-Right
ENDIF.

* Value
CALL METHOD OF
g_selection
'TypeText'

EXPORTING
#1 = p_text.

********************************************************************