Macro to Copy/Paste list items from one Word document to another does not continue list.

keaden

New Member
Joined
Sep 29, 2010
Messages
26
Hello again all,
I'm having some trouble with a macro I've been writing to populate a template letter with user selected articles formatted and stored in another document.


I have the Userform set up and it runs pretty well, it opens an instance of word, copies the proper text and pastes it into the letter and the users comments and closed the articles document when finished, leaving the letter to be saved by the user.


the problem is I'm having difficulty with the preformatted list items copy and pasting properly.


I want the general structure of the document to be as follows


1). Article1 info


Observations
User input text from related userform textbox


Actions
User input text from related userform textbox


2). Article2 info


Observations
User input text from related userform textbox


Actions
User input text from related userform textbox


3). Article3 info


Observations
User input text from related userform textbox


Actions
User input text from related userform textbox


etc


However, when I run the macro I get this output


1). Article1 info (numbered list item)


Observations
User input text from related userform textbox


Actions
User input text from related userform textbox


Article2 info (no numbered list item)


Observations
User input text from related userform textbox


Actions
User input text from related userform textbox


the numbering stops


I was wondering if anyone could point me in the right direction in this or come across this problem before?
Any help would be appreciated.


The code I have is as follows;


Code:
Private Sub CommandButton4_Click()
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim i As Integer
    Dim x As Integer
    Dim y As Integer
    Dim Txt2 As String
    Dim Txt3 As String
    Dim Txt2Rplce As String
    Dim Txt3Rplce As String
    Dim DocA As Object
    Dim DocB As Object


    x = iAddedCount 'loop integer from previous sub
    y = 1


    If Me.Letter1.Value = False And Me.Letter2.Value = False And Me.Letter3.Value = False And Me.Letter4.Value = False Then ' will not run the macro unless a leter type is selected
        MsgBox "You Must Choose a Letter Type"
    Exit Sub
    End If
    
        If Me.Letter1.Value = True Then


                Set objWord = New Word.Application 'Opens new instance or word
                objWord.DisplayAlerts = False
                Set DocB = objWord.Documents.Open(ActiveWorkbook.Path & "\Forms\Articles.docx", ReadOnly:=True ) ' opens prepared document containing formatted list as read only
                objWord.Visible = True
                Set DocA = objWord.Documents.Open(ActiveWorkbook.Path & "\Letters\Letter1.docx") ' opens prepared template document 
                objWord.Visible = True
                objWord.Selection.GoTo What:=wdGoToBookmark, Name:="InsertPoint1" ' goes to inserted named bookmark in template letter


            
        End If
        


        For Each theCombo In UserForm1.MultiPage1.Pages(1).MultiPage2.Pages(5).Frame14.Controls


                If x > 0 Then
                    
                    Txt1 = Me("Combo" & y).Text
        
                    Txt2 = Me("Text1" & y).Text


                    Txt3 = Me("Text2" & y).Text


                    Txt1Rplce = Replace(Txt1, vbLf, Chr(11))


                    Txt2Rplce = Replace(Txt2, vbLf, "")


                    Txt3Rplce = Replace(Txt3, vbLf, "")
                                  
                    
                If Me("Combo" & y).Text = "Select Article" Then ' if userform dropdown selection contains predefined text
                    


                    DocB.Bookmarks("Article3General").Range.Copy     'copy specified article bookmarked in articles document
                    DocA.Bookmarks("InsertPoint1").Select        'select bookmark in letter template
                    objWord.Selection.Paste                'Paste list numbered list item into letter
                    objWord.Selection.Range.ListFormat.RemoveNumbers    'remove numberformatting 
                    
            objWord.Selection.TypeParagraph            'insert line
                   
                
                objWord.Selection = "Observations:" & vbCr & Txt2Rplce 'adds heading and copies and pastes text from first textbox to selection in word document
                    With objWord.Selection
                        .Font.Name = "Arial"
                        .Font.Size = 11
                        .Font.Bold = True
                        .ParagraphFormat.LeftIndent = 10 'working
                        .ParagraphFormat.RightIndent = 10
                        .MoveRight
                        .TypeParagraph
                        .TypeParagraph
                    End With


                objWord.Selection = "Action(s):" & vbCr & Txt3Rplce 'adds heading and copies and pastes text from second textbox to selection in word document
                    With objWord.Selection
                        .Font.Name = "Arial"
                        .Font.Size = 11
                        .Font.Bold = True
                        .ParagraphFormat.LeftIndent = 10 'working
                        .ParagraphFormat.RightIndent = 10
                        .MoveRight
                        .TypeParagraph
                        .TypeParagraph
                    End With
                End If


End If
                    y = y + 1
                    x = x - 1 ' counts down loop integer
                    
       Next theCombo


    objWord.Documents(ActiveWorkbook.Path & "\Forms\Articles.docx").Close (Word.WdSaveOptions.wdDoNotSaveChanges) 'closes down Articles document when all list items are copy pasted into letter document


End Sub


Thanks in advance for any help or tips.
Cheers
K
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top