Skip to Content
avatar image
-1
Former Member

Adding TEXT watermak in word document through OLE

Hi All ,

Can anybody provide me a code in OLE for inserting text watermark in word document.

I am trying with the below code as per the macro recording but It is not working.

Please help.

CREATE OBJECT gv_wordapp 'WORD.APPLICATION'.
set property of gv_wordapp 'Visible' = 1.

CALL METHOD of
gv_wordapp'Documents' = lv_worddoc.
CALL METHOD of
lv_worddoc'Open'
EXPORTING
#1 = l_filename.
CALL METHOD of
gv_wordapp'ActiveDocument' = lv_wordadoc.


CALL METHOD of
gv_wordapp'ActiveWindow' = w_activewindow.CALL METHOD of
w_activewindow'ActivePane' = w_activepane.CALL METHOD of
w_activepane'View' = w_activeview.
SET PROPERTY OF w_activeview 'SeekView' = '9'. " header view.

Get PROPERTY OF gv_wordapp 'Selection' = w_selection.
get property of w_selection 'HeaderFooter' = w_headfoot.
get property of w_headfoot 'Shapes' = w_shapes.
CALL METHOD of
w_shapes'AddTextEffect' = W_SHAPE

EXPORTING
#1 = 'PowerPlusWaterMarkObject221372499'
#2 = 'IN APPROVAL'
#3 = 'Calibri'
#4 = '44'
#5 = '0'
#6 = '0'
#7 = '0'
#8 = '0'.
CALL METHOD of
w_shape'Select'.

Get PROPERTY OF gv_wordapp 'Selection' = w_selection1.

get property of w_selection1 'ShapeRange' = w_range

set property of w_range 'Name' = 'PowerPlusWaterMarkObject221372499'.

get property of w_range 'TextEffect' = w_text.S

et property of w_text 'NormalizedHeight' = '0'.

GET PROPERTY OF W_range 'Line' = w_line.

set property of w_line 'Visible' = '0'.
get property of w_range 'Fill' = w_fill.

set property of w_fill 'Visible' = '-1'.call method of w_fill 'Solid'.

get property of w_fill 'ForeColor' = W_Colour.

Set property of w_colour 'RGB' = '11776947'.

set property of w_fill 'Transparency' = '0.5'.


set property of w_range 'Rotation' = '315'.

set property of w_range 'LockAspectRatio' = '-1'.

set property of w_range 'Height' = '80'.

set property of w_range 'Width' = '150'.
get property of w_range 'WrapFormat' = w_wrap.

set property of w_wrap 'AllowOverlap' = '3'.

set property of w_wrap 'Side' = '3'.set property of w_wrap 'Type' = '3'.

set property of w_range 'RelativeHorizontalPosition' = '0'.

set property of w_range 'RelativeVerticalPosition' = '0'.

set property of w_range 'Left' = '-999995'.

set property of w_range 'Top' = '-999995'.

SET PROPERTY OF w_activeview 'SeekView' = '0'.
CALL METHOD of
lv_wordadoc'Save'.

CALL METHOD of
gv_wordapp'Quit'.

Regards

Sweta

Add comment
10|10000 characters needed characters exceeded

  • I guess you have recorded the watermark via the Word VBA macro recorder. Could you provide the macro please so that we can help you? (only the part corresponding to the ABAP code of course)

  • Get RSS Feed

6 Answers

  • avatar image
    Former Member
    Mar 08, 2017 at 05:14 AM

    As per macro recording in add text method we need to pass the watermarkobject name but when I give the watermark object name its not displaying anything. If I pass some value like ' Msotexteffect 1 , Msotexteffect10 then its displaying but not in the form of watermark.

    Add comment
    10|10000 characters needed characters exceeded

    • Let me help you, by answering to my question i.e. the code of the macro. I had to spend time to get it, that's ridiculous as you could have given it to us (you also see that I don't have the same code as yours, so please continue the discussion with the code below as basis of discussion):

      ActiveDocument.Sections(1).Range.Select
      ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
      Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject, _
      "WATERMARKTEXT", "Calibri", 1, False, False, 0, 0).Select
      Selection.ShapeRange.name = "PowerPlusWaterMarkObject"
      Selection.ShapeRange.TextEffect.NormalizedHeight = False
      Selection.ShapeRange.Line.Visible = False
      Selection.ShapeRange.Fill.Visible = True
      Selection.ShapeRange.Fill.Solid
      Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
      Selection.ShapeRange.Fill.Transparency = 0.5
      Selection.ShapeRange.Rotation = 315
      Selection.ShapeRange.LockAspectRatio = True
      Selection.ShapeRange.Height = CentimetersToPoints(3.98)
      Selection.ShapeRange.Width = CentimetersToPoints(18.58)
      Selection.ShapeRange.WrapFormat.AllowOverlap = True
      Selection.ShapeRange.WrapFormat.Side = wdWrapNone
      Selection.ShapeRange.WrapFormat.Type = 3
      Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
      Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
      Selection.ShapeRange.Left = wdShapeCenter
      Selection.ShapeRange.Top = wdShapeCenter
      ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

      So, your question is about the value of "PowerPlusWaterMarkObject" (the first parameter of AddTextEffect). Strangely, the VBA debugger says it's of value "Empty" and type is "Variant/Empty".

      Function AddTextEffect(_
      PresetTextEffect As MsoPresetTextEffect, _
      Text As String, _
      FontName As String, _
      FontSize As Single, _
      FontBold As MsoTriState, _
      FontItalic As MsoTriState, _
      Left As Single, _
      Top As Single, _
      [Anchor]) _
      As Shape

      I next see that MsoPresetTextEffect is of type "Enum". By typing code "dummy =

      MsoPresetTextEffect." the suggestion proposes the list of possible values, which are msoTextEffect1, etc., but nowhere I find PowerPlusWaterMarkObject. Arbitrarily, I try msoTextEffect1 (the Object Explorer tells us it's "Const msoTextEffect1 = 0", so in ABAP it will be "= 0". And that works. So "Empty" seems to be understood as zero here.

      Now your last question: the watermark appears as a "WordArt" instead of a watermark. This is because both are technically the same, but for being a watermark, the WordArt object needs to be attached to the Header part of the page. If it doesn't work, you have missed something in one of the three first lines:

      ActiveDocument.Sections(1).Range.Select
      ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
      Selection.HeaderFooter.Shapes.AddTextEffect
  • avatar image
    Former Member
    Mar 08, 2017 at 05:09 AM
    -1

    Can you let me know what should I pass in addtext method?

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 07, 2017 at 09:55 PM

    Be careful to pass the integer values without the quotes, and same for booleans! (= 0 for false, = 1 for true)

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 08, 2017 at 10:25 AM

    Just to answer your very first question.

    CALL METHOD of w_shapes 'AddTextEffect' = W_SHAPE
    EXPORTING
    #1 = 0 " use 0 instead of 'PowerPlusWaterMarkObject221372499' ; PresetTextEffect
    #2 = 'IN APPROVAL' " Text
    #3 = 'Calibri' " FontName
    #4 = 44 " FontSize
    #5 = 0 " FontBold
    #6 = 0 " FontItalic
    #7 = 0 " Left
    #8 = 0. " Top

    See also my detailed answer comment for details.

    For other questions, post them as new questions (see forum rules)
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 08, 2017 at 06:42 PM

    ActiveDocument.Sections(1).Range.Select
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.HeaderFooter.Shapes.AddTextEffect( _
    PowerPlusWaterMarkObject247385324, "ASAP", "Calibri", 72, False, False, 0 _
    , 0).Select
    Selection.ShapeRange.Name = "PowerPlusWaterMarkObject247385324"
    Selection.ShapeRange.TextEffect.NormalizedHeight = False
    Selection.ShapeRange.Line.Visible = False
    Selection.ShapeRange.Fill.Visible = True
    Selection.ShapeRange.Fill.Solid
    Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
    Selection.ShapeRange.Fill.Transparency = 0.5
    Selection.ShapeRange.Rotation = 315
    Selection.ShapeRange.LockAspectRatio = True
    Selection.ShapeRange.Height = CentimetersToPoints(3.1)
    Selection.ShapeRange.Width = CentimetersToPoints(5.45)
    Selection.ShapeRange.WrapFormat.AllowOverlap = True
    Selection.ShapeRange.WrapFormat.Side = wdWrapNone
    Selection.ShapeRange.WrapFormat.Type = 3
    Selection.ShapeRange.RelativeHorizontalPosition = _
    wdRelativeVerticalPositionMargin
    Selection.ShapeRange.RelativeVerticalPosition = _
    wdRelativeVerticalPositionMargin
    Selection.ShapeRange.Left = wdShapeCenter
    Selection.ShapeRange.Top = wdShapeCenter
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 19, 2018 at 11:08 AM

    Hi,

    You can use this code

      DATA: gv_word_app       TYPE ole2_object,
            gv_documents      TYPE ole2_object, "ActiveDocument Handle
            gv_document       TYPE ole2_object, "ActiveDocument Handle
            gv_activedocument TYPE ole2_object, "ActiveDocument Handle
            gv_range          TYPE ole2_object, "Range Handle
            gv_activewindow   TYPE ole2_object, "ActiveWindow Handle
            gv_view           TYPE ole2_object, "View Handle
            gv_selection      TYPE ole2_object, "Selection Handle
            gv_HeaderFooter   TYPE ole2_object, "HeaderFooter Handle
            gv_Shapes         TYPE ole2_object,
            gv_Shape          TYPE ole2_object,
    
    
      CREATE OBJECT gv_word_app 'WORD.APPLICATION'.
      SET PROPERTY OF gv_word_app 'Visible' = '1'.
      GET PROPERTY OF gv_word_app 'Documents' = gv_documents.
      CALL METHOD OF gv_documents 'Add' = gv_document.
      CALL METHOD OF gv_document 'Activate'.
      GET PROPERTY OF gv_word_app 'ActiveDocument' = gv_activedocument.
      GET PROPERTY OF gv_activedocument 'ActiveWindow' = gv_activewindow.
      GET PROPERTY OF gv_activewindow 'View' = gv_view.
      SET PROPERTY OF gv_view 'SeekView' = '1'.
      GET PROPERTY OF gv_word_app 'Selection' = gv_selection.
      GET PROPERTY OF gv_selection 'HeaderFooter' = gv_HeaderFooter.
      GET PROPERTY OF gv_HeaderFooter 'Shapes' = gv_Shapes.
      call METHOD of gv_Shapes 'AddTextEffect' = gv_Shape
      EXPORTING
        #1 = '0'
        #2 = 'TESTING'
        #3 = 'Times New Roman'
        #4 = '44'
        #5 = '0'
        #6 = '0'
        #7 = '0'
        #8 = '0'.
    Add comment
    10|10000 characters needed characters exceeded