Consulting

Results 1 to 5 of 5

Thread: Word-VBA that combines 2 sentences that have references

  1. #1

    Word-VBA that combines 2 sentences that have references

    HELLO. I have a word doc that has sentences with references. It looks like this:

    Se1c1 The boy went
    Se1c2 to school.
    Se1c3 Then he saw a rabbit.
    Se2c1 he ran after it
    Se2c2 but he couldn’t catch it

    -The reference Se1c3 stands for: section Se, chapter 1, sentence 3


    The target is to copy to clipboard several sentences that has this structure:

    Se1c1-3 The boy went to school. Then he saw a rabbit.


    THE QUESTION IS: Can someone suggest a vba code that when it runs, it copies to clipboard the sentences that i have selected, But with the target structure? (only to copy, not to replace)


    Notes

    -I use word 2016
    -I will only select consecutive sentences before running the macro.
    -In the word document, every sentence is written as a paragraph.
    -In the reference (Se1c1), the Section Letters (Se) may be formed from 2 or 3 letters like: Neg1c1 (3 letters)


    THANKS A LOT

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://stackoverflow.com/questions/...s-2-references
    Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    My post on the other site was unclear and unsuccessful. I deleted it to post again, but i couldn't post again immediately. So i posted here.
    But I like that policy that teaches people not to be selfish. Wise is the one who learns this.

  4. #4
    Based on your examples the following should work

    Sub Macro1()
    'Graham Mayor - https://www.gmayor.com - Last updated - 03 May 2020
    Dim iStart As Integer, iEnd As Integer
    Dim iSec As Integer, i As Integer, x As Integer
    Dim sTest As String, sResult As String
    Dim dResult As DataObject
    Dim sSection As String
    Dim oPara As Paragraph
    Dim oRng As Range, oSec As Range
        sTest = InputBox("Enter section number", "Section Number")
        If IsNumeric(sTest) Then
            iSec = CInt(sTest)
        Else
            Beep
            MsgBox "Not a number"
            Exit Sub
        End If
        sTest = InputBox("Enter start number", "Start Number")
        If IsNumeric(sTest) Then
            iStart = CInt(sTest)
        Else
            Beep
            MsgBox "Not a number"
            Exit Sub
        End If
        sTest = InputBox("Enter end number", "End Number")
        If IsNumeric(sTest) Then
            iEnd = CInt(sTest)
        Else
            Beep
            MsgBox "Not a number"
            Exit Sub
        End If
        sResult = "": sSection = ""
        For i = 1 To Selection.Paragraphs.Count
            Set oRng = Selection.Paragraphs(i).Range
            oRng.MoveStartUntil "0123456789"
            x = Len(CStr(iSec)) + Len(CStr(iStart)) + Len(CStr(iEnd)) + 1
            oRng.MoveEndWhile "0123456789"
            If Val(oRng.Text) = iSec Then
                If sSection = "" Then
                    Set oSec = oRng.Paragraphs(1).Range
                    oSec.Collapse 1
                    oSec.MoveEndUntil "0123456789"
                    oSec.End = oSec.End + 1 + Len(CStr(iSec))
                    sSection = oSec.Text
                End If
                oRng.End = oRng.Paragraphs(1).Range.End - 1
                oRng.Start = oRng.Start + x
                sResult = sResult & oRng.Text & Chr(32)
            End If
        Next i
        sResult = sSection & CStr(iStart) & "-" & CStr(iEnd) & Chr(32) & sResult
        Set dResult = New DataObject
        dResult.SetText sResult
        dResult.PutInClipboard
    
        MsgBox sResult & vbCr & vbCr & "Copied to clipboard"
    lbl_Exit:
        Set oRng = Nothing
        Set oSec = Nothing
        Set dResult = Nothing
        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
    Yes it worked! Thank you very much!!

    But i faced a couple of problems:

    The first one: in the case below, some letters from the sentence copied are disappearing.
    Se1c10 The boy went (2 digit number after the c)
    Se1c11 to school.
    Se1c12 Then he saw a rabbit.
    The sentence copied to clipboard is: Se1c11-13 he boy went o school. en he saw a rabbit.

    The second one is the input-boxes. Can we Not to use them? (in this case it will be done automatically because the macro will be run a lot.)


    -----

    Sorry , but I have another question: Is it possible to add another part to the macro where it will do the same thing but for nonconsecutive sentences?(If the selected is consecutive, it will run the first part, if not it will run The new Part)
    for example:

    Se1c10 The boy went
    Se1c11 to school.
    Se1c12 Then he saw a rabbit.

    If i select sentence 1 and 3 (holding Ctrl), the result will be:
    Se1c10&12 The boy went Then he saw a rabbit.

    Thank you again!!!

Tags for this Thread

Posting Permissions

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