B4J Question Open Word and replace text


Active Member
Licensed User

I need some help,

I looked at the forum and find the apache poi lib and the work example works fine. (Creating a docx with some lines).

I need to open an existing Word document , and replace some old text with new text . After open word or related program and the user will print the document manually.

I an other language I work with ole object and bookmarks :

ls_content_new ="new Text"
if ole_1.ActiveDocument.Bookmarks.Exists(ls_name) then
end if
How I can do this in B4J . If not possible, only replacing a text would help, because every Bookmark has a text in the document.



Active Member
Licensed User
Great, it works but it replaces the whole line.

How I replace only the keyword, like :

Your name : $V_Name$
must be
Your name : Kohle

Dim m As Map = CreateMap("V_NAME": "Kohle", "V_VORNAME": "Jürgen")
    Dim doc As JavaObject = OpenDocx(File.DirApp, "input.docx")
      Dim paragraphs As List = doc.RunMethod("getParagraphs", Null)
       For Each p As JavaObject In paragraphs
           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
                   End If
           End If
       SaveDocument(doc, File.DirApp , "output.docx")


Active Member
Licensed User
solved it.... Thanks

For Each key As String In m.Keys
    If text.Contains("$" & key & "$") Then
           Dim k As String   
           Dim v As String
           k = "$" & key & "$"
           v = " " & m.Get(key) & " "
           text = text.Replace(k,v    )
           r.RunMethod("setText",Array(text, 0))
     End If


Well-Known Member
Licensed User
Hello community.

i tried this example, but i got this error message:
Waiting for debugger to connect...
Program started.
Fehler in Zeile: 1383 (LoginForm)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:91)
at b4j.example.loginform._opendoc(loginform.java:548)
at b4j.example.loginform._bestehendes_testdokument_befuellen(loginform.java:456)
at b4j.example.loginform._layout_001_button_dokument_click(loginform.java:2241)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$1.run(BA.java:216)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supt thepplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:147)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:177)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:104)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:142)
... 28 more
The error appears in this line:
document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", Array(in))
The document existst on the HDD, does anybody know where the problem is?


Well-Known Member
Licensed User
solved it
It also works ... but I have another problem:
The docx-output-File is generated and it shows only for 1 second the data as expected and after that it shows the variables ($VORNAME$), see Screenshot output.
Screenshot output.png

I have attached the example app, the input- and the output-file.

Does anybody know, why the docx-file is not showing the data?



Well-Known Member
Licensed User
Sorry Erel,
but I thougt if everybody is starting his own thread, the forum will be unorganized ...
... so i will start a new thread.