B4J Question [SOLVED] Replace Text in a Word-Document-HEADER

Erel

B4X founder
Staff member
Licensed User
Longtime User
Updated code that replaces text in the headers and document body:
B4X:
Dim m As Map = CreateMap("HEADER": "asdasd")

Dim doc As JavaObject = OpenDocx("C:\Users\H\Downloads\TEST1.docx", "")
Dim AllParagrpahs As List
AllParagrpahs.Initialize
Dim headers As List = doc.RunMethod("getHeaderList", Null)
For Each header As JavaObject In headers
   AllParagrpahs.AddAll(header.RunMethod("getParagraphs", Null))
Next
AllParagrpahs.AddAll(doc.RunMethod("getParagraphs", Null))
For Each p As JavaObject In AllParagrpahs
   Dim runs As List = p.RunMethod("getRuns", Null)
   If runs.IsInitialized Then
       For Each r As JavaObject In runs
           Dim text As String = r.RunMethod("getText", Array(0))
           If text <> Null Then
               For Each key As String In m.Keys
                   If text.Contains("$"&key&"$") Then
                       r.RunMethod("setText", Array(""&m.Get(key)&"", 0))
                   End If
               
               Next
               End If
       Next
   End If
Next
SaveDocument(doc, "C:\Users\H\Downloads\TEST2.docx", "")
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Thank you Erel!
You are a genius!
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
... there is a new problem:
When i want to print out this WORD-document, there is the message:
Error! Reference source not found!
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
It has nothing to do with the header text, it is always the same to all text.
Just open the attached file "output.docx".

The first time everything looks good, but ...
I am not able to print it
 

Attachments

  • output.zip
    3.9 KB · Views: 188
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Here it is ...
 

Attachments

  • input.zip
    4 KB · Views: 187
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…