PDA

View Full Version : [SOLVED:] pelase help the code in VBA



kissisvarmas
09-12-2016, 01:32 AM
Hi,

i am trying to use the below VBA code in a word to extract the whole sentence from the given string into another word document, but the problem is if the given strings are repeated in a single sentence, this code is extracting twice and pasting in other document, so my concern is to paste the sentense only single time, here the examples :
Code:

Sub Guidancefinal()


Const strFind As String = "outlook/guidance/forecast")
Dim guid1 As Document
Dim guid2 As Document
Dim vFind As Variant
Dim orng As Range, oText As Range
Dim i As Long
Set guid1 = Documents("Current.docx")
Set guid2 = Documents("Guidance.docx")
vFind = Split(strFind, "/")
guid2.Activate
Selection.WholeStory
Selection.Delete
For i = LBound(vFind) To UBound(vFind)
guid1.Activate
Set orng = ActiveDocument.Range
With orng.Find
Do While .Execute(FindText:=vFind(i))
Set oText = orng.Sentences(1)
oText.Select
Selection.Copy
guid2.Activate
Selection.EndKey wdStory
Selection.PasteAndFormat wdPasteDefault
orng.Collapse 0


Loop
End With
Next i
lbl_Exit:
Exit Sub


End Sub


case:
current document:
provided earnings guidance for the six months and forecast all the figures for period ended June 30, 2016. And results for reaming PEO’s

Guidance document:

provided earnings guidance for the six months and forecast all the figures for period ended June 30, 2016.
provided earnings guidance for the six months and forecast all the figures for period ended June 30, 2016.

here the issue is the code is extracting twice as the two strings are repeating in a single sentence, so i want to custom my program to extract as single line even we have a multi strings in the document


so please help me please

gmayor
09-12-2016, 06:51 AM
Answered in your other thread in this forum

mana
09-12-2016, 07:26 AM
Sub test()
Const strFind As String = "outlook/guidance/forecast"
Dim guid1 As Document
Dim guid2 As Document
Dim vFind As Variant
Dim dic As Object
Dim s As Variant
Dim i As Long

Set guid1 = Documents("Current.docx")
Set guid2 = Documents("Guidance.docx")
vFind = Split(strFind, "/")

Set dic = CreateObject("scripting.dictionary")

For Each s In guid1.Sentences
For i = LBound(vFind) To UBound(vFind)
If InStr(s.Text, vFind(i)) > 0 Then
dic(s.Text) = Empty
End If
Next
Next


guid2.Range.Text = Join(dic.keys, vbCr)
guid2.Activate

End Sub

gmaxey
09-12-2016, 12:55 PM
I think at the very least you need to change:

dic(s.Text) = Empty to dic(Trim(s.Text) = Empty as a sentence in paragraph is not the same string as a sentence in and at the end of a paragraph.

kissisvarmas
09-12-2016, 06:25 PM
Wow.working million thanks to you


Hi there is a problem in the above code, when the string starring in proper case in a sentense ex: Guidance, Forecast, then the above code is unable to extract, so please modify the code that matches the string in any case.

mana
09-13-2016, 04:27 AM
Thanks!

>dic(Trim(s.Text)

mana
09-13-2016, 04:31 AM
Hi there is a problem in the above code,


If InStr(LCase(s.Text), vFind(i)) > 0 Then

kissisvarmas
09-13-2016, 08:20 PM
Yes working...thanks again for sharing