Copy data type text from excel to word VBA [closed]

0

I am generating a macro that copies data from excel to word, but it marks me an error, I behave the code that I have, hoping they can help me.

Sub copiar_word()
Dim wordapp As Object
Dim x, a As Integer
x = 8
Set wordapp = CreateObject("word.Application")
With wordapp
    .Visible = True
    .Activate
    .Documents.Add
End With
c = ActiveSheet.Range("a" & x).Value
Do While c <> ""
    ActiveSheet.Range("a" & x).Select
    Selection.Copy
    wordapp.Selection.PasteSpecial link:=True
    If ActiveSheet.Range("a" & x + 1).Value = "" Then
        a = x + 1
        Do While ActiveSheet.Range("a" & a).Select = ""
            a = a + 1
        Loop
        ActiveSheet.Range(Cells(x, 2), Cells(a, 2)).Select
        Selection.Copy
        wordapp.Selection.PasteSpecial link:=True
        x = x + a
    Else
        ActiveSheet.Range("b" & x).Select
        Selection.Copy
        wordapp.Selection.PasteSpecial link:=True
        x = x + 1
    End If
Loop
End Sub
    
asked by Hugo 24.04.2017 в 19:53
source

1 answer

1

If the error that marks you is "overflow", it is because your Do While continues indefinitely.

To solve it you have to reassign the value of your variable c before closing your loop

Sub copiar_word()
Dim wordapp As Object
Dim x, a As Integer
x = 8
Set wordapp = CreateObject("word.Application")
With wordapp
    .Visible = True
    .Activate
    .Documents.Add
End With
c = ActiveSheet.Range("a" & x).Value
Do While c <> ""
    ActiveSheet.Range("a" & x).Select
    Selection.Copy
    wordapp.Selection.PasteSpecial link:=True
    If ActiveSheet.Range("a" & x + 1).Value = "" Then
        a = x + 1
        Do While ActiveSheet.Range("a" & a).Select = ""
            a = a + 1
        Loop
        ActiveSheet.Range(Cells(x, 2), Cells(a, 2)).Select
        Selection.Copy
        wordapp.Selection.PasteSpecial link:=True
        x = x + a
    Else
        ActiveSheet.Range("b" & x).Select
        Selection.Copy
        wordapp.Selection.PasteSpecial link:=True
        x = x + 1
    End If
    c = ActiveSheet.Range("a" & x).Value
Loop
End Sub
    
answered by 24.04.2017 в 20:09