0
votes

This is my third day of using VBA. I am currently working a 3 month contract for this company and have figured out how to automatically transfer data from excel to a new word document (a process they have wanted for years). I also found out how to open an existing word document from excel.

The thing is, I haven't worked out how to transfer data from excel to an existing word document.

Below is the code I am trying to write for the above reason. Can anyone point out where I'm going wrong? Any and all answers would be greatly appreciated :)

Sub CopyRangeToWord()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Set wordApp = GetObject("C:\Users\CoffeeFuelsMeNow\Documents\Ladedadeda\Testplate.dotx")

Set objDoc = objWord.Documents
objWord.Visible = True
Range("A1:B10").Copy
 With objDoc.Paragraphs(objDoc.Paragraphs.Count).Range
       'All formatting goes here
    .Paste
    .Font.Name = "broadway"
    .Font.Color = wdColorBlue
    .Font.Bold = True
    .Font.Italic = True
     .Font.Allcaps = True
     .Font.Size = 20
End With
End Sub
2
Can you please say what you want to achieve? Keep the question straight to the pointeithed
@eithed Sorry, that was the coffeeCoffeeFuelsMeNow

2 Answers

1
votes

This works perfectly fine for me: It adds the rows to the document over and over again as a table if i execute the code

Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc =objWord.Documents.Open("C:\Users\schmidmath\Documents\test1.docx")

Range("A1:B10").Copy

With objDoc.Paragraphs(objDoc.Paragraphs.Count).Range
   'All formatting goes here
   .Paste
   .Font.Name = "broadway"
   .Font.Color = wdColorBlue
   .Font.Bold = True
   .Font.Italic = True
   .Font.Allcaps = True
   .Font.Size = 20    
End With
objWord.Visible = True
0
votes

Below modified code works at my end, test it and let me know if it suffice your requirement.

Sub CopyRangeToWord()
 Dim objWord
 Dim objDoc

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
Set wordApp = GetObject("C:\Users\CoffeeFuelsMeNow\Documents\Ladedadeda\Testplate.dotx")
Set obj­Doc = objWord.Documents

objWord.Visible = True
Range("A1:B10").Copy
 With objDoc.Paragraphs(objDoc.Paragraphs.Count).Range
       'All formatting goes here
    .Paste
    .Font.Name = "broadway"
    .Font.Color = wdColorBlue
    .Font.Bold = True
    .Font.Italic = True
    .Font.Allcaps = True
    .Font.Size = 10
End With
End Sub