티스토리 뷰

반응형

MS Word 에 스타일을 지정하여, 글자를 추가하는 예제를 만들어보았다.

 

○ 스타일을 등록하지 않고 사용하는 방법

○ 기존에 등록된 스타일을 이용하는 방법

 

스타일을 등록하지 않고 사용하는 방법
Ms Word 에 문서를 추가하고 글씨를 쓰는 예제 (WordApp. 사용)
Sub CreateWordDocument()            ' Word 문서 새로 생성하기

' Word 변수 객체 선언
'
Dim WordApp As New Word.Application ' Word 문서를 객체로 설정
WordApp.Visible = True              'New Apps will be hidden by default, so make visible

' 신규 문서 추가
Dim WordDoc As New Word.Document
Set WordDoc = WordApp.Documents.Add(Template:="Normal")

'write the value to the document

WordApp.Selection.ParagraphFormat.SpaceBefore = 50	' 이전단락 가격
WordApp.Selection.ParagraphFormat.SpaceAfter = 36	' 다음단락 간격	
WordApp.Selection.Font.Size = 35					' 글자 크기
WordApp.Selection.Font.Name = "Arial"				' 폰트
WordApp.Selection.Font.Color = wdColorRed			' 글자색
WordApp.Selection.Font.Bold = True					' 굵은글씨
WordApp.Selection.InsertDateTime DateTimeFormat:="MMMM yyyy" '& vbCrLf
WordApp.Selection.TypeParagraph						' 줄바꿈
WordApp.Selection.TypeText Text:="Test Line1" & vbCrLf
WordApp.Selection.InsertBreak						' 페이지바꿈

WordApp.Selection.TypeText Text:="TEST LINE2"

WordDoc.SaveAs ("C:\WORKS\nsc\WORD MACRO\WordMacro")

' 매크로가 끝났을 때, 컴퓨터의 메모리를 관리하기위해 word 객체를 제거한다
Set WordApp = Nothing               'release the memory
Set WordDoc = Nothing               'release the memory

End Sub

 

Ms Word 에 문서를 추가하고 글씨를 쓰는 예제 (With문 사용)
Sub CreateWordDocument()            ' Word 문서 새로 생성하기

' Word 변수 객체 선언
'
Dim WordApp As New Word.Application ' Word 문서를 객체로 설정
WordApp.Visible = True              'New Apps will be hidden by default, so make visible

' 신규 문서 추가
Dim WordDoc As New Word.Document
Set WordDoc = WordApp.Documents.Add(Template:="Normal")

'write the value to the document
With WordApp.Selection
.ParagraphFormat.SpaceBefore = 50
.ParagraphFormat.SpaceAfter = 36
.Font.Size = 35
.Font.Name = "Arial"
.Font.Color = wdColorRed
.Font.Bold = True
.InsertDateTime DateTimeFormat:="MMMM yyyy" '& vbCrLf
.TypeParagraph
.TypeText Text:="Test Line1" & vbCrLf
.InsertBreak
End With

WordApp.Selection.TypeText Text:="TEST LINE2"

WordDoc.SaveAs ("C:\WORKS\nsc\WORD MACRO\WordMacro")

' 매크로가 끝났을 때, 컴퓨터의 메모리를 관리하기위해 word 객체를 제거한다
Set WordApp = Nothing               'release the memory
Set WordDoc = Nothing               'release the memory

End Sub

 

매크로 실행 결과

첫페이지에 지정한 스타일로 글자가 추가된 것을 확인할 수 있다.

두번째 페이지는 특별한 서식을 지정하지 않았기때문에, 첫페이지에서 설정한 스타일이 그대로 유지되었다.

 

 

 

기존 등록된 스타일을 사용하는 방법

문서에 등록된 스타일을 사용하고자 할 경우는, 스타일의 이름을 지정해주면 된다.

Sub CreateWordDocument()            ' Word 문서 새로 생성하기

' Word 변수 객체 선언
'
Dim WordApp As New Word.Application ' Word 문서를 객체로 설정
WordApp.Visible = True              'New Apps will be hidden by default, so make visible

' 신규 문서 추가
Dim WordDoc As New Word.Document
Set WordDoc = WordApp.Documents.Add(Template:="Normal")

'
'write the value to the document
'

WordApp.Selection.Style = "Style Name"		' 기존 Style 적용
WordApp.Selection.TypeText Text:="Test Line1" & vbCrLf
WordApp.Selection.InsertBreak
WordApp.Selection.TypeText Text:="TEST LINE2"

WordDoc.SaveAs ("C:\WORKS\nsc\WORD MACRO\WordMacro")

' 매크로가 끝났을 때, 컴퓨터의 메모리를 관리하기위해 word 객체를 제거한다
Set WordApp = Nothing               'release the memory
Set WordDoc = Nothing               'release the memory

End Sub

 

 

 

 

 

 

댓글