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: 

Ms word Integration with SAP using OLE methods for Header sections and footer sections (Word.basic)

hemanth_kumar46
Explorer
0 Kudos

Hi all ,

I am replacing the MS office Word Data (Place holders) with SAP data using OLE , its working fine ....

But here i am facing Problem with Header and footer .

in my word document i have different Header sections and footer sections .

I am using Call method of gs_word 'ViewHeader'.

Call Method of gs_word 'Editreplace'

methods for repainting data .

But data is not replacing if the header section will change , kindly let me know any method is there for finding the different header sections and footer sections.

Urgent ....

Regards .

Hemanth

6 REPLIES 6

Sandra_Rossi
Active Contributor
0 Kudos

Don't know how to find it by yourself? Just start the Word macro recorder (developer tabe & alt+F11), you'll get the properties and methods.

hemanth_kumar46
Explorer
0 Kudos

Hi Sandra ,

I created Macro , How do I write the "With Selection.Find" command in my ABAP code?

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "VOID@"
        .Replacement.Text = "Text"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sandra_Rossi
Active Contributor
0 Kudos

For VBA questions, you have a VBA reference guide available online. with something means that all lines starting with a period/point will be interpreted as starting with something. By the way your recording is only about replacing text in the current "pane", but not how to go from the main pane to the header and footer panes. The recorder generates this with my old Word version:

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="header text"<br>

By the way, constant names must be replaced with constant values. Use F2 in the VBA editor to get them (wdReplaceAll -> 2, wdSeekCurrentPageHeader -> 9,etc.)

Sandra_Rossi
Active Contributor
0 Kudos

By the way, there's also a Word generator -> Generate DOCX file in ABAP

hemanth_kumar46
Explorer

Hi Sandra ,

Here i created macro for Find replace .

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "#VOID$1"
        .Replacement.Text = "Hemanth"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

For above Macro i developed ABAP code as below.


CALL METHOD OF word 'ActiveWindow' = w_activewindow.
CALL METHOD OF w_activewindow 'ActivePane' = w_activepane.
CALL METHOD OF w_activepane 'View' = w_activeview.
*--Getting applications handle
GET PROPERTY OF actdoc 'application' = application .
GET PROPERTY OF application 'Selection' = selection.
* GET PROPERTY OF selection 'InlineShapes' = inlineshapes.
SET PROPERTY OF w_activeview 'SeekView' = '9'.
* find
GET PROPERTY OF selection 'find' = find.
* find text
SET PROPERTY OF find 'Text' = '#VOID$1'.
* find text forward
SET PROPERTY OF find 'Forward' = abap_true.
* Clear formatting
CALL METHOD OF find 'ClearFormatting'.
GET PROPERTY OF find 'Replacement' = oreplacement.
SET PROPERTY OF oreplacement 'Text' = 'Hemanth'.
CALL METHOD OF oreplacement 'ClearFormatting'.
* Replacing
CALL METHOD OF find 'Execute'
  EXPORTING
    #01 = '#VOID$1'
    #02 = abap_false
    #03 = abap_false
    #04 = abap_false
    #05 = abap_false
    #06 = abap_false
    #07 = abap_true
    #08 = 1
    #09 = abap_false
    #10 = gv_charg
    #11 = 2.

I am trying to replacing the header sections .

in my file i have different header sections.

only first header section (3 pages) is replacing with sap data , remaining is not replacing , kindly correct my code if any changes required.

Sandra_Rossi
Active Contributor
0 Kudos

If you find the GoTo command in MS Word, then you can use the macro recorder (Ctrl+G in my Word English version). So, to reach the first section:

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=1

To reach the next section (not via the recorder, deduced from "Which" possible values):

Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext

Note: based on the Word Object Browser, I agree that "Replace" is the #11 th parameter:

Function Execute([FindText], [MatchCase], [MatchWholeWord], [MatchWildcards], [MatchSoundsLike], [MatchAllWordForms], [Forward], [Wrap], [Format], [ReplaceWith], [Replace], [MatchKashida], [MatchDiacritics], [MatchAlefHamza], [MatchControl]) As Boolean

A limit with OLE in SAP is that all "previous" parameters need to be passed (1 to 10 in your case). So it's okay that you added the 10 first parameters.

But now, your ABAP code has lots of redundancies, as you can see (VBA equivalence):

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection
        .Find.Text = "#VOID$1"
        .Replacement.Text = "Hemanth"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
Selection.Find.Execute _
      FindText:="#VOID$1", _
      MatchCase:=False, _
      MatchWholeWord:=False, _
      MatchWildcards:=False, _
      MatchSoundsLike:=False, _
      MatchAllWordForms:=False, _
      Forward:=True, _
      Wrap:=wdFindContinue, _
      Format:=False, _
      ReplaceWith:="Hemanth", _
      Replace:=wdReplaceAll

All the "With" group can be removed, just make sure it still works by editing the macro and run it:

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Execute _
      FindText:="#VOID$1", _
      MatchCase:=False, _
      MatchWholeWord:=False, _
      MatchWildcards:=False, _
      MatchSoundsLike:=False, _
      MatchAllWordForms:=False, _
      Forward:=True, _
      Wrap:=wdFindContinue, _
      Format:=False, _
      ReplaceWith:="Hemanth", _
      Replace:=wdReplaceAll