cancel
Showing results for 
Search instead for 
Did you mean: 

MS Word custom VBO to copy paragraphs between two headings

Udaya_SankarC
Level 2

HI Team, I have scenario where I have to copy paragraphs between two headings from word document and paste it into another word document under specific section. I have created a VBA code in word document which works in Word VBA and when i modify according to our BP code stage. I am getting various error message and difficult to complete the action. Please help me resolve the action.

 Here is the code which i was using in BP Code and getting the error.

 
Dim oDoc As Object = GetDocument(handle,document_name)
Dim w As Object = oDoc.Application
Dim srchRng As Range = Nothing
Dim srchIntroRng As Object = Nothing
Dim oRng As Object = Nothing
Dim oRng2 As Object = Nothing
Dim oDocRng As Object = Nothing
Dim Rng As Object = Nothing
Dim styltext as string = ""
Dim styl as style = Nothing
Dim lStart,lPara as long
Dim checkout as string = ""
Dim paras As Paragraphs
Dim para As Paragraph
'Dim tempRange as Range = Nothing
 
lStart=0
 
Try
 
srchRng = oDoc.Content
 
For Each fld In oDoc.Fields
    If fld.Type = 13 Then
        fld.Select
        w.Selection.Collapse
        Rng = w.Selection.Range 'capture place to re-insert TOC later
        fld.Cut
    End If
Next
 
srchRng = oDoc.Content
 
For lPara = lStart To oDoc.Paragraphs.Count
Numbervalue=lPara+2
checkout="im n"
checkout = w.Paragraphs(1).Range.Text
If oRng2.FormattedText=StartHeading Then
oRng.Start = oRng2.Start - 1
'bEndFound = True
Exit For
End If
Next para
 
 
Numbervalue=oRng.Start
'Numbervalue=oDoc.Paragraphs.Count
 
For lPara = oRng.Start+1 To oDoc.Paragraphs.Count
'Numbervalue=lPara
 
styl=oRng2.Style
 
styltext=styl.NameLocal
If styltext = "Heading 1" and oRng2.Text="Introduction" Then
oRng.End = oRng2.Start - 1
'bEndFound = True
Exit For
End If
Next lPara
 
 
OutputData=oRng.FormattedText
 
 
        
lbl_Exit:
    
oDoc = Nothing
oRng = Nothing
oDocRng = Nothing
srchRng=Nothing
 
Catch ex As Exception
Dim trace = New Diagnostics.StackTrace(ex, True)
        Dim line As String = Strings.Right(trace.ToString, 5)
        OutputData=checkout
        ErrorMessage="Error Line Number: " & line & ex.Message 
 
'ErrorMessage=ex.Message
Finally
w = Nothing
srchRng = Nothing
Rng = Nothing
ORng = Nothing
 
End try

1 REPLY 1

Denis__Dennehy
Level 15

Hello.  Have you looked at the logic in Blue Prism's own Word VBO and how the code looks in that,  how it launches/attaches/etc.  Whenever I ever wanted to create a new Word or Excel action I would take a copy of the existing BP VBO,  remove all the actions I did not need (leaving all the launch/connect/attach/close/etc ones) and then create my action using the same syntax format they used and keeping the same connect/launch actions.
All the create object / GetDocument logic already exists in the existing VBO.
Blue Prism Digital Exchange