Skip to content

Instantly share code, notes, and snippets.

@DuongAQ
Created July 30, 2018 09:37
Embed
What would you like to do?
Sub Export_to_Word() 'Xuất dữ liệu ra file Word
'Khai báo các biến
Dim wdapp As Object, wddoc As Object
Dim strdocname As String
On Error Resume Next
'Lấy nội dung vùng dữ liệu bằng cách copy
Sheet1.Range("D1:D81").Copy
'Mở ứng dụng Word từ VBA
Set wdapp = GetObject(, "word.Application")
If Err.Number = 429 Then
Err.Clear
Set wdapp = CreateObject("Word.Application")
End If
wdapp.Visible = True
wdapp.Active
'Tạo mới 1 file Word và dán nội dung vào
Set wddoc = wdapp.Documents.Add
wddoc.Active
wddoc.Range.PasteSpecial xlPasteValues
'Làm trống các biến để giải phóng bộ nhớ
Set wddoc = Nothing
Set wdapp = Nothing
Application.CutCopyMode = False
'Thông báo hoàn thành
MsgBox "Done!"
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment