Consulting

Results 1 to 5 of 5

Thread: VBA for word

  1. #1

    VBA for word

    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
    Last edited by kissisvarmas; 09-12-2016 at 02:32 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Ask you colleagues how to use VBA Code Tags in Forums.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Might be better to post your question in the Word forum, not the Excel one.

    There's a lot of very helpful Word experts over there
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    The following should work (note there is no error check for missing documents)

    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.Open(FileName:="C:\Path\Current.docx", AddToRecentfiles:=False)
        Set guid2 = Documents.Open(FileName:="C:\Path\Guidance.docx", AddToRecentfiles:=False)
        vFind = Split(strFind, "/")
        Set oText = guid2.Range
        oText.Text = ""
        For i = LBound(vFind) To UBound(vFind)
            Set orng = guid1.Range
            With orng.Find
                Do While .Execute(FindText:=vFind(i))
                    oText.Collapse 0
                    oText.FormattedText = orng.Sentences(1).FormattedText
                    oText.InsertParagraphAfter
                    orng.Collapse 0
                Loop
            End With
        Next i
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Hi, Thanks for sending the code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •