1
votes

How to convert string to temporary range so that we can get functionality of range object to work with string. For example I am doing something like this.

Edit

I have in word table cell 2

With ActiveDocument.Range
    For Each Tbl In .Tables
        With Tbl
            For i = 1 To .Rows.Count
                With .Cell(i, 2).Range
                If .Hyperlinks.Count > 0 Then
                    HttpReq.Open "GET", .Hyperlinks(1).Name, False
                    HttpReq.send
                    oHtml.body.innerHTML = HttpReq.responseText
                    StrTxt = oHtml.body.innerText
                    'missing lne something like set rng.Text = StrTxt
                    'this part is also not working, but I think I will be able to do it if I get above part working
                        If rng.Find.Execute(findText:="Abstract:") Then
                            Set Rng1 = rng( _
                            Start:=docnew.Paragraphs(1).Range.Start, _
                            End:=docnew.Paragraphs(2).Range.End)
                            MsgBox Rng1.Text
                                With Tbl.Cell(i, 3).Range
                                .FormattedText = Rng1.FormattedText
                                End With
                        End If
                End if
            Next
        End with
    Next
End With

This macro is not working so I can't say weather this is right or wrong to do by this mehtod. It's just an Idea. Is it possible to dim a temp rng? Is there any better option available to my problem?

1
Can you EDIT your question and provide a brief explanation about why you're getting the string from an html source? Are you trying to use Word as a "tool" for working with the HTML? Or is there more to this macro that has to do with Word?Cindy Meister
Edited. Please note that I want to extract abstract text from the HTML. the HTML is dynamic so elements extraction is not working. I am going casual by doing this as this would Definitely give me abstract every time.Rahul
Yes, the question is clearer. Now: do you want this text in a specific place, or just "temporary"? Would it help if you could check whether "Abstract" is in the string before deciding to write to a range? You can use the InStr function for that: If instr(strtxt, "Abstract") <> 0Cindy Meister
Problem is that I don't have any knowledge how the text will behave as string. The thing is that I don't have marker to end if I use instr. While converting the htmlinnertext to word range I will be sure that the paragraph which contain word abstract and the next paragraphs is the desired text.Rahul
@CindyMeister, Does this means that, No you can't convert the htmlinnertext to range?. You have to parse it as a plain text.Rahul

1 Answers

0
votes

I know that this is stub but, this is the code that worked for me.

StrTxt = oHtml.body.innerText
                            'MsgBox StrTxt
                            Set docnew = Documents.Add
                                With docnew
                                    .Range.Text = StrTxt
                                    Set drange = ActiveDocument.Range( _
                                        Start:=ActiveDocument.Paragraphs(1).Range.Start, _
                                        End:=ActiveDocument.Paragraphs(150).Range.End)
                                        drange.Delete
                                        Set Rng1 = ActiveDocument.Range
                                            If Rng1.Find.Execute(findText:="Abstract") Then
                                                Set rng2 = ActiveDocument.Range(Rng1.End, ActiveDocument.Range.End)
                                                If rng2.Find.Execute(findText:=".") Then
                                                    strTheText = ActiveDocument.Range(Rng1.End, rng2.Start).Text
                                                    strTheText = Replace(strTheText, ":  ", ":")
                                                    'MsgBox strTheText
                                                End If
                                            End If
                                              .Close (Word.WdSaveOptions.wdDoNotSaveChanges)
                                End With
                            Set docnew = Nothing
                            Application.ScreenUpdating = False
                        End If
                            With Tbl.Cell(i, 3).Range
                            .InsertAfter vbCr & "Abstract" & strTheText & "."
                            End With