2017-11-21 1 views
1

다음 Excel 컨텐츠를 워드 문서로 변환하려고합니다. 새 단어 보고서에는 학생 이름, 날짜, 제목, 원래 시험 시간 및 새 시험 시간이 포함됩니다. enter image description here enter image description hereExcel에서 단어 보고서를 생성하는 방법은 무엇입니까?

나는 이것을하기 위해 간단한 방법을 사용하려고 노력했다. 범위 (a79 : L85) & 범위 (A90 : L92)를 새 단어 문서에 복사합니다. 그러나 작동하지 않고 함께 두 테이블을 조인합니다 (동일한 행에).

Sub ExcelRangeToWord() 

'PURPOSE: Copy/Paste An Excel Table Into a New Word Document 
'NOTE: Must have Word Object Library Active in Order to Run _ 
    (VBE > Tools > References > Microsoft Word 12.0 Object Library) 
'SOURCE: www.TheSpreadsheetGuru.com 

Dim tbl As Excel.RANGE 
Dim tbl2 As Excel.RANGE 
Dim WordApp As Word.Application 
Dim myDoc As Word.Document 
Dim WordTable As Word.Table 

'Optimize Code 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 

'Copy Range from Excel 
    Set tbl = ThisWorkbook.Worksheets(sheet9.Name).RANGE("A79:L85") 'copy the name ,subject and old exam time 
    Set tbl2 = ThisWorkbook.Worksheets(sheet99.Name).RANGE("A90:L92")'copy the new exam time 
'Create an Instance of MS Word 
    On Error Resume Next 

    'Is MS Word already opened? 
     Set WordApp = GetObject(Class:="Word.Application") 

    'Clear the error between errors 
     Err.Clear 

    'If MS Word is not already open then open MS Word 
     If WordApp Is Nothing Then Set WordApp = CreateObject(Class:="Word.Application") 

    'Handle if the Word Application is not found 
     If Err.Number = 429 Then 
     MsgBox "Microsoft Word could not be found, aborting." 
     GoTo EndRoutine 
     End If 

    On Error GoTo 0 

'Make MS Word Visible and Active 
    WordApp.Visible = True 
    WordApp.Activate 

'Create a New Document 
    Set myDoc = WordApp.Documents.Add 


'Copy Excel Table Range 
    tbl.Copy ' paste range1 
    tbl2.Copy 'paste range2 

'Paste Table into MS Word 
    myDoc.Paragraphs(1).RANGE.PasteExcelTable _ 
    LinkedToExcel:=False, _ 
    WordFormatting:=False, _ 
    RTF:=False 

'Autofit Table so it fits inside Word Document 
    Set WordTable = myDoc.Tables(1) 
    WordTable.AutoFitBehavior (wdAutoFitWindow) 

EndRoutine: 
'Optimize Code 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 

'Clear The Clipboard 
    Application.CutCopyMode = False 

End Sub 

어떤 힌트 또는 방법은 다음과 같이 단어 보고서를 생성 할 수 있습니까?

+0

출력이 필요하고 실습으로이 작업을 수행하지 않는 경우 편지 병합이 더 간단 해지고 훨씬 빠르게 실행됩니다. –

+0

@ MihaiOvidiuDrăgoi 그러나 Excel을 사용하여 새 시간을 계산해야합니다. 개별 문서는 단어이어야합니다. 편지 병합이 수행 할 수 있습니까? 참조 링크가 있습니까? – Vito

답변

0

이것은 + 붙여 넣기 테이블을 차례로 복사하는 방법을 설명하는 솔루션의 일부일 수 있습니다.

+0

코드를 사용하면 두 개의 서로 다른 표가 생성됩니다. 형식이 일치하지 않습니다 (열의 형식이 동일하지 않음). 또한 일부 단어 크기는 2 행이됩니다. 마지막으로, 원래의 Excel 파일에서 글꼴 위치 (가운데)를 상속받을 수 없습니다. 그것을 향상시키는 방법? – Vito

+0

이렇게됩니다. https://ibb.co/fqmVXm – Vito

+0

1 개의 테이블로 만드는 것이 좋습니다. 그리고 원래 Excel 파일의 형식을 더 잘 상속받습니다. 또한 셀 공간은 단어 길이에 맞아야합니다. 문제를 해결할 생각이 있습니까? – Vito

관련 문제