B4J Question Open Word and replace text

kohle

Active Member
Licensed User
Longtime 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 :

B4X:
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.
 

kohle

Active Member
Licensed User
Longtime 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

B4X:
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")
 
Upvote 0

kohle

Active Member
Licensed User
Longtime User
solved it.... Thanks

B4X:
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
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello community.

i tried this example, but i got this error message:
The error appears in this line:
B4X:
document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", Array(in))
The document existst on the HDD, does anybody know where the problem is?
 
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime 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.

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

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

Attachments

  • PoiRescue_Map.zip
    5.4 KB · Views: 225
  • input.zip
    4 KB · Views: 221
  • output.zip
    3.9 KB · Views: 206
Upvote 0

JOTHA

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…