Macro Visual basic code in word to combine documents paragraph by paragraph

Marta 1 Reputation point
2022-12-05T19:50:55.19+00:00

Dear community,

I am trying to perform a macro to combine two documents (1) and (2) and obtain as a result (3).

267280-image.png

267306-image.png

267371-image.png

Thanks for your help

Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-12-06T01:46:06.687+00:00

    Hi @Marta ,
    You can refer to the following code.

            Dim f1() As String = File.ReadAllLines("C:\Users\Administrator\Desktop\1.txt")  
            Dim f2() As String = File.ReadAllLines("C:\Users\Administrator\Desktop\2.txt")  
            Using Sw As StreamWriter = New StreamWriter("C:\Users\Administrator\Desktop\3.txt", True)  
                Dim a As Integer = 0  
                For i = 0 To f1.Length - 1  
      
                    If f1(i) Is "" OrElse i = f1.Length - 1 Then  
      
                        For j = a To i  
                            If f2(j) IsNot "" Then  
                                Sw.WriteLine(f2(j))  
                            End If  
                        Next  
                        a = i  
                    Else  
                        Sw.WriteLine(f1(i))  
                    End If  
                Next  
            End Using  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.