B4J Question Open Word and replace text

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

  1. kohle

    kohle Active Member Licensed User

    Hi,

    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 :

    Code:
    ole_1.connecttoobject("",'word.application')
    ole_1.Application.documents.open("test.docx",false,true)
    ls_name=
    "M_ANREDE"
    ls_content_new =
    "new Text"
    if ole_1.ActiveDocument.Bookmarks.Exists(ls_name) then
        ole_1.selection.goto(
    true,0,0,ls_name)
        ole_1.selection.typetext(ls_content_new)
    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.



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

    Code:
    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
                           
    Next
                  
                       
    End If
                   
    Next
               
    End If
           
    Next
           SaveDocument(doc, 
    File.DirApp , "output.docx")
     
  4. kohle

    kohle Active Member Licensed User

    solved it.... Thanks

    Code:
    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
     
    Next
     
    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:
    Code:
    document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument"Array(in))
    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.
     
Loading...
  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