bongmaihung
Thành viên hoạt động
- Tham gia
- 7/8/14
- Bài viết
- 173
- Được thích
- 39
[COLOR=#000000][COLOR=#0000BB]Sub EXCEL_TO_WORD[/COLOR][COLOR=#007700]()
[/COLOR][COLOR=#0000BB]Dim WordDoc [/COLOR][COLOR=#007700]As [/COLOR][COLOR=#0000BB]Object
Set WordDoc [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]CreateObject[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Word.Application"[/COLOR][COLOR=#007700])
[/COLOR][COLOR=#0000BB]WordDoc[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Visible [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]True
WordDoc[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Documents[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Open [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]ThisWorkbook[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Path [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#DD0000]"\BANG BAO GIA.doc"[/COLOR][COLOR=#007700])
[/COLOR][COLOR=#0000BB]Range[/COLOR][COLOR=#007700]([[/COLOR][COLOR=#0000BB]A2[/COLOR][COLOR=#007700]], [[/COLOR][COLOR=#0000BB]A65536[/COLOR][COLOR=#007700]].[/COLOR][COLOR=#0000BB]End[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]3[/COLOR][COLOR=#007700])).[/COLOR][COLOR=#0000BB]Resize[/COLOR][COLOR=#007700](, [/COLOR][COLOR=#0000BB]5[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000BB]Copy
With WordDoc[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Selection
[/COLOR][/COLOR][COLOR=#b22222].MoveDown Unit:=wdLine, Count:=3[/COLOR][COLOR=#000000][COLOR=#0000BB]
[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]PasteSpecial [/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]DataType[/COLOR][COLOR=#007700]:=[/COLOR][COLOR=#0000BB]wdPasteOLEObject[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]Placement[/COLOR][COLOR=#007700]:=[/COLOR][COLOR=#0000BB]wdInLine
End With
Set WordDoc [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]Nothing
End Sub [/COLOR][/COLOR]
[COLOR=#00007F]Sub[/COLOR] ExcelRangeToWord()
[COLOR=#007F00]'PURPOSE: Copy/Paste An Excel Table Into a New Word Document[/COLOR]
[COLOR=#007F00]'NOTE: Must have Word Object Library Active in Order to Run _
(VBE > Tools > References > Microsoft Word 12.0 Object Library)[/COLOR]
[COLOR=#007F00]'SOURCE: www.TheSpreadsheetGuru.com[/COLOR]
[COLOR=#00007F]Dim[/COLOR] tbl [COLOR=#00007F]As[/COLOR] Excel.Range
[COLOR=#00007F]Dim[/COLOR] WordApp [COLOR=#00007F]As[/COLOR] Word.Application
[COLOR=#00007F]Dim[/COLOR] myDoc [COLOR=#00007F]As[/COLOR] Word.Document
[COLOR=#00007F]Dim[/COLOR] WordTable [COLOR=#00007F]As[/COLOR] Word.Table
[COLOR=#007F00]'Optimize Code[/COLOR]
Application.ScreenUpdating = [COLOR=#00007F]False[/COLOR]
Application.EnableEvents = [COLOR=#00007F]False[/COLOR]
[COLOR=#007F00]'Copy Range from Excel[/COLOR]
[COLOR=#00007F]Set[/COLOR] tbl = ThisWorkbook.Worksheets(Sheet1.Name).ListObjects("Table1").Range
[COLOR=#007F00]'Create an Instance of MS Word[/COLOR]
[COLOR=#00007F]On[/COLOR] [COLOR=#00007F]Error[/COLOR] [COLOR=#00007F]Resume[/COLOR] [COLOR=#00007F]Next[/COLOR]
[COLOR=#007F00]'Is MS Word already opened?[/COLOR]
[COLOR=#00007F]Set[/COLOR] WordApp = GetObject(class:="Word.Application")
[COLOR=#007F00]'Clear the error between errors[/COLOR]
Err.Clear
[COLOR=#007F00]'If MS Word is not already open then open MS Word[/COLOR]
[COLOR=#00007F]If[/COLOR] WordApp [COLOR=#00007F]Is[/COLOR] [COLOR=#00007F]Nothing[/COLOR] [COLOR=#00007F]Then[/COLOR] [COLOR=#00007F]Set[/COLOR] WordApp = CreateObject(class:="Word.Application")
[COLOR=#007F00]'Handle if the Word Application is not found[/COLOR]
[COLOR=#00007F]If[/COLOR] Err.Number = 429 [COLOR=#00007F]Then[/COLOR]
MsgBox "Microsoft Word could not be found, aborting."
[COLOR=#00007F]GoTo[/COLOR] EndRoutine
End [COLOR=#00007F]If[/COLOR]
[COLOR=#00007F]On[/COLOR] [COLOR=#00007F]Error[/COLOR] [COLOR=#00007F]GoTo[/COLOR] 0
[COLOR=#007F00]'Make MS Word Visible and Active[/COLOR]
WordApp.Visible = [COLOR=#00007F]True[/COLOR]
WordApp.Activate
[COLOR=#007F00]'Create a New Document[/COLOR]
[COLOR=#00007F]Set[/COLOR] myDoc = WordApp.Documents.Add
[COLOR=#007F00]'Copy Excel Table Range[/COLOR]
tbl.Copy
[COLOR=#007F00]'Paste Table into MS Word[/COLOR]
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=[COLOR=#00007F]False[/COLOR], _
RTF:=[COLOR=#00007F]False[/COLOR]
[COLOR=#007F00]'Autofit Table so it fits inside Word Document[/COLOR]
[COLOR=#00007F]Set[/COLOR] WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
[COLOR=#00007F]End[/COLOR]Routine:
[COLOR=#007F00]'Optimize Code[/COLOR]
Application.ScreenUpdating = [COLOR=#00007F]True[/COLOR]
Application.EnableEvents = [COLOR=#00007F]True[/COLOR]
[COLOR=#007F00]'Clear The Clipboard[/COLOR]
Application.CutCopyMode = False
End [COLOR=#00007F]Sub[/COLOR]
Sub Data2Word()
'Remember: this code requires a referece to the Word object model
'dimension some local variables
Dim rng As Range 'our source range
Dim wdApp As New Word.Application 'a new instance of Word
Dim wdDoc As Word.Document 'our new Word document
Dim t As Word.Range 'the new table in Word as a range
Dim myWordFile As String 'path to Word template
'initialize the Word template path
'here, it's set to be in the same directory as our source workbook
myWordFile = ThisWorkbook.Path & "\DocWithTableStyle.dot"
'get the range of the contiguous data from Cell A1
Set rng = Range("A1").CurrentRegion
'you can do some pre-formatting with the range here
rng.HorizontalAlignment = xlCenter 'center align the data
rng.Copy 'copy the range
'open a new word document from the template
Set wdDoc = wdApp.Documents.Add(myWordFile)
Set t = wdDoc.Content 'set the range in Word
t.Paste 'paste in the table
With t 'working with the table range
.Style = "GreenBar" 'set the style created for the table
'we can use the range object to do some more formatting
'here, I'm matching the table with using the Excel range's properties
.Tables(1).Columns.SetWidth (rng.Width / rng.Columns.Count), wdAdjustSameWidth
End With
'until now the Word app has been a background process
wdApp.Visible = True
'we could use the Word app object to finish off
'you may also want to things like generate a filename and save the file
wdApp.Activate
End Sub
tôi cũng có sử dụng code để chép từ excel sang word
bạn yêu cầu làm cái gì vậy?
1-mở một file word trắng tinh
2-copy và paste cái table trong file bài #1 ra trang word đó?
phải vậy ko?
tôi cũng có sử dụng code để chép từ excel sang word
bạn yêu cầu làm cái gì vậy?
1-mở một file word trắng tinh
2-copy và paste cái table trong file bài #1 ra trang word đó?
phải vậy ko?
http://www.giaiphapexcel.com/forum/...py-từ-excel-qua-word-ai-rành-chỉ-giúp-tôi-vớiMã:[COLOR=#000000][COLOR=#0000BB]Sub EXCEL_TO_WORD[/COLOR][COLOR=#007700]() [/COLOR][COLOR=#0000BB]Dim WordDoc [/COLOR][COLOR=#007700]As [/COLOR][COLOR=#0000BB]Object Set WordDoc [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]CreateObject[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Word.Application"[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000BB]WordDoc[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Visible [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]True WordDoc[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Documents[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Open [/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]ThisWorkbook[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Path [/COLOR][COLOR=#007700]& [/COLOR][COLOR=#DD0000]"\BANG BAO GIA.doc"[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000BB]Range[/COLOR][COLOR=#007700]([[/COLOR][COLOR=#0000BB]A2[/COLOR][COLOR=#007700]], [[/COLOR][COLOR=#0000BB]A65536[/COLOR][COLOR=#007700]].[/COLOR][COLOR=#0000BB]End[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]3[/COLOR][COLOR=#007700])).[/COLOR][COLOR=#0000BB]Resize[/COLOR][COLOR=#007700](, [/COLOR][COLOR=#0000BB]5[/COLOR][COLOR=#007700]).[/COLOR][COLOR=#0000BB]Copy With WordDoc[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]Selection [/COLOR][/COLOR][COLOR=#b22222].MoveDown Unit:=wdLine, Count:=3[/COLOR][COLOR=#000000][COLOR=#0000BB] [/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000BB]PasteSpecial [/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]DataType[/COLOR][COLOR=#007700]:=[/COLOR][COLOR=#0000BB]wdPasteOLEObject[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]Placement[/COLOR][COLOR=#007700]:=[/COLOR][COLOR=#0000BB]wdInLine End With Set WordDoc [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]Nothing End Sub [/COLOR][/COLOR]
Em thử theo đường link trên nhưng báo lỗi ở vùng màu đỏ, có thể em áp dụng chưa đúng. Không biết có phải khi di chuyển xuống dòng thứ ba, bảng báo giá theo link trên sẽ đường chèn ko ah.
Nhờ anh chị giúp đề tài này ah
Em cảm ơn!
Sub EXCEL_TO_WORD()
Range([A2], [A65536].End(3)).Resize(, 6).Copy
With CreateObject("Word.Application")
.Visible = True
.Documents.Add
.Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteOLEObject
End With
End Sub
Nhớ là phải vào mục Reference đánh dấu chọn Microsoft Word 14...PHP:Sub EXCEL_TO_WORD() Range([A2], [A65536].End(3)).Resize(, 6).Copy With CreateObject("Word.Application") .Visible = True .Documents.Add .Selection.PasteSpecial Placement:=wdInLine, DataType:=wdPasteOLEObject End With End Sub
Nếu không sẽ báo lỗi
Chắc là có thay đổi ít nhiều chứ. Code này mình cũng sưu tầm thôi, nên cũng không biết phải thay đổi gì nữa.Em test thử cũng rất ok. Anh cho em hỏi thêm có phải Reference đánh dấu chọn Microsoft Word 12 là cho word 2007, còn 14 là cho word 2010 đúng ko ah. Nếu bây h em muốn copy dữ liệu đó vào file word có sẵn dữ liệu rồi or dữ liệu excel nhiều hơn 1 trang word thì ko biết code có thay đổi gì ko hả anh?
Ở nhà em đang dùng office 2010
Em cảm ơn anh. Code của anh giống như kiểu copy Object nhưng em loay hoay ko biết nếu file excel dữ liệu hơn 1 trang thì file word phải kéo và chỉnh file word như thế nào để sang trang thứ hai hoặc nhhiều hơnChắc là có thay đổi ít nhiều chứ. Code này mình cũng sưu tầm thôi, nên cũng không biết phải thay đổi gì nữa.
Thử xóa bỏ cụm code này xemEm cảm ơn anh. Code của anh giống như kiểu copy Object nhưng em loay hoay ko biết nếu file excel dữ liệu hơn 1 trang thì file word phải kéo và chỉnh file word như thế nào để sang trang thứ hai hoặc nhhiều hơn
Thử xóa bỏ cụm code này xem
Placement:=wdInLine
Chơi code khác lưu sheet đó thành File Word luôn được không?Hỏi thêm quanghai1969:
thêm lệnh gì để
1. save file đó với tên đã có
2. Đóng file đã save
3. Đóng Word
Cảm ơn trước.
(Tôi vốn là haonlh, gần đây không vô được. Nên lấy tên là haog. Nếu ai giúp lấy lại tên cũ được thì báo luôn vô đây. Hoặc gửi cho nguyenxuanhao@huaf.edu.vn)
Được ạ.Chơi code khác lưu sheet đó thành File Word luôn được không?
Xem thử File. Lưu ý:
- Muốn lưu tên File Word là gì thì sửa tên sheet.
- Tạo 1 Folder rồi bỏ File Excel vào, khi chạy code nó sẽ lưu File Word trong Folder này.
- Khi xuất ra File Word chịu khó vào Page Setup chọn lại trang A4.
Nhưng nếu là riêng Document thì có phương thức Close:Không có toàn bộ code nên đoán mò. Tôi chỉ đọc bài của bạn thôi. Đừng bắt người khác phải đọc các bài khác.
F_Word không có phương thức CLOSE. Hãy dùng F_Word.Quit.
On Error GoTo errorHandler
ActiveDocument.Close _
SaveChanges:=wdPromptToSaveChanges, _
OriginalFormat:=wdPromptUser
errorHandler:
If Err = 4198 Then MsgBox "Document was not closed"
Không có toàn bộ code nên đoán mò.
F_Word không có phương thức CLOSE. Hãy dùng F_Word.Quit.