VBA copy from excel to word

petars87

New Member
Joined
Sep 14, 2016
Messages
49
Hi people,

I have this code which I want to improve

Code:
  Sub CopyToWord()
     Dim objWord As Object
     Dim objDoc As Object
     Set objWord = CreateObject("Word.Application")
     Set objDoc = objWord.Documents.Add(Template:= _
         "[URL="file://\\xxx\vb\PetarTest\template.docx"]\\xxx\vb\PetarTest\template.docx[/URL]")
         
     Set Ar = Worksheets("Sheet1").Range("A1:F" & Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row)
    
     Ar.Copy
     
     objDoc.Range.Paste
     
     objWord.Visible = True
     Set objDoc = Nothing
     Set objWord = Nothing
 End Sub


i changed range an now i have :

Code:
Sub CopyToWord()
     Dim objWord As Object
     Dim objDoc As Object
     Dim i As Long
     Dim LR As Long
     
     Set objWord = CreateObject("Word.Application")
     Set objDoc = objWord.Documents.Add(Template:= _
         "[URL="file://\\xxx\vb\PetarTest\template.docx"]\\xxx\vb\PetarTest\template.docx[/URL]")
     
     
     
     LR = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
     
    For i = 0 To LR
     
     Set Ar = Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells((i * 36) + 1, 1), Worksheets("Sheet1").Cells((i * 36) + 36, 6))
     Ar.Copy
     
    '----Here i need your help----
    
    Next i
    
    Set objDoc = Nothing
    Set objWord = Nothing
   
    
 End Sub

How to copy all this ranges in separated word documents, save them and close at the end?

Thank you.
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Where do the names for these documents come from? You can't save one without giving it a name. Also, what folder are they to be saved to?
 
Upvote 0
I know that i should specify name and location but exactly that syntax is my question :)
i don't know how to specify that, so in that part of code where name and location is specified i need ur help

i was trying with these but doesn't work(don't lough):

Code:
Sub CopyToWord()
     Dim objWord As Object
     Dim objDoc As Object
     Dim i As Long
     Dim LR As Long
     
     Set objWord = CreateObject("Word.Application")
     Set objDoc = objWord.Documents.Add(Template:= _
         "[URL="file://xxx/vb/PetarTest/template.docx"]\\xxx\vb\PetarTest\template.docx[/URL]")
     
     
     
     LR = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
     
    For i = 0 To LR
     
     Set Ar = Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells((i * 36) + 1, 1), Worksheets("Sheet1").Cells((i * 36) + 36, 6))
     Ar.Copy
     
   
   'ChDir "C:\Users\xxx\Desktop\Fie\BUD"
    'wbNam = "BUD_"
    'dt = Format(CStr(Now), "yyyy_mm_dd_hh_mm")
    'objDoc.Range.Paste
  ' objWord.SaveAs Filename:= _
   '       wbNam & dt & Environ("UserName") 
  'objDoc.Close
    Next i
    
    Set objDoc = Nothing
    Set objWord = Nothing
   
    
 End Sub


Thak you.
 
Upvote 0
Try something along the lines of:
Code:
Sub CopyToWord()
Dim objWord As Object, objDoc As Object, i As Long, strPath As String, strName As String
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
strPath = "C:\Users\" & Environ("UserName") & "\Desktop\Fie\BUD\"
strName = "BUD_" & Format(Now, "yyyy_mm_dd_hh_mm_")
With Sheets("Sheet1")
  For i = 0 To .Cells(.Rows.Count, 1).End(xlUp).Row
    .Range(.Cells((i * 36) + 1, 1), .Cells((i * 36) + 36, 6)).Copy
    Set objDoc = objWord.Documents.Add(Template:="\\" & Environ("UserName") & "\vb\PetarTest\template.docx")
    With objDoc
      .Range.Paste
      .SaveAs2 strPath & strName & "(" & Format(i, "00") & ").docx", 12 '12=XML document format.
      .Close
    End With
  Next i
End With
objWord.Quit
Set objDoc = Nothing: Set objWord = Nothing
End Sub
Note that I've added the 'i' counter to the filename; simply using the current date & time with only one-minute accuracy would result in files being overwritten.
 
Upvote 0

Forum statistics

Threads
1,223,796
Messages
6,174,657
Members
452,575
Latest member
Fstick546

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