B4J Question Open Word and replace text

Discussion in 'B4J Questions' started by kohle, Apr 10, 2019.

  1. kohle

    kohle 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.

  2. Erel

    Erel Administrator Staff Member Licensed User

  3. kohle

    kohle 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
    "setText"Array(" " & m.Get(key) & " "0))
    End If
    End If
    End If
    File.DirApp , "output.docx")
  4. kohle

    kohle 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    )
    "setText",Array(text, 0))
    End If
    Harris likes this.
  5. JOTHA

    JOTHA Well-Known Member Licensed User

    Hello community.

    i tried this example, but i got this error message:
    The error appears in this line:
    The document existst on the HDD, does anybody know where the problem is?
  6. JOTHA

    JOTHA Well-Known Member Licensed User

    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?

    Attached Files:

  7. Erel

    Erel Administrator Staff Member Licensed User

    You should never post a new question in an existing thread. Always start your own thread. You can post a link to this thread is they are similar.
  8. JOTHA

    JOTHA 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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice