출처 : http://exceltip.com/st/Control_Word_from_Excel_using_VBA_in_Microsoft_Excel/465.html

 

Sub CreateNewWordDoc()

' to test this code, paste it into an Excel module

' add a reference to the Word-library

' create a new folder named C:\Foldername or edit the filenames in the code

Dim wrdApp As Word.Application

Dim wrdDoc As Word.Document

Dim i As Integer

  Set wrdApp = CreateObject("Word.Application")

  wrdApp.Visible = True

  Set wrdDoc = wrdApp.Documents.Add '새로운 문서 생성

  ' 또는

  'Set wrdDoc = wrdApp.Documents.Open("C:\FolderName\Filename.doc")

  ' 로컬 파일 열기

  ' word 작업 샘플

  With wrdDoc

    For i = 1

      .Content.InsertAtfer "Here is a example test line #" & i

      .Content.InsertParagraphAfter

    Next i

    If Dir("C:\FolderName\MyNewWordDoc.doc") <> "" Then

      Kill "C:\FolderName\MyNewWordDoc.doc"

    End If

    .SaveAs("C:\FolderName\MyNewWordDoc.doc")

    .Close ' 문서 close

  End With

  wrdApp.Quit ' MSWord 프로그램 종료

  Set wrdDoc = Nothing

  Set wrdApp = Nothing

End Sub

 

Sub OpenAndReadWordDoc()

' assumes that the previous procedure has been executed

Dim wrdApp As Word.Application

Dim wrdDoc As Word.Document

Dim tSting As String, tRange As Word.Range

Dim p As Long, r As Long

  Workbooks.Add ' 새로운 workbook 생성

  With Range("A1")

    .Formula = "Word Document Contents:"

    .Font.Bold = True

    .Font.Size = 14

    .Offset(1, 0).Select

  End With

  r = 3 ' MSWord 문서로부터 복사한 문자열이 시작하는 row

  Set wrdApp = CreateObject("Word.Application")

  'wrdApp.Visible = True

  Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\MyNewWordDoc.doc")

  'example word operations

  With wrdDoc

    For p = 1 To .Paragraphs.Count

      Set tRange = .Range(Start:=.Paragraphs(p).Range.Start, _

           End:=.Paragraphs(p).Range.End)

      tString = tRange.Text

      tString = Left(tString, Len(tString) - 1)

      'exclude the paragraph-max

      'check if the text has the content you want

      If InStr(1, tString, "1") > 0 Then

        ' 활성화된 worksheet에 데이터 추가

        ActiveSheet.Range("A" & r).Formula = tString

        r = r + 1

      End If

    Next p

  End With

  wrdApp.Quit ' Word 프로그램 종료

  Set wrdDoc = Nothing

  Set wrdApp = Nothing

  ActiveWorkbook.Saved = True

End Sub

반응형

'VBA' 카테고리의 다른 글

MSDN - VBA SQL Union  (0) 2013.04.11
VBA 날짜 함수 정리된 곳  (0) 2013.04.04
Application.OnTime 설명  (0) 2012.12.28
cdbl(Convert to a double)  (0) 2012.12.28
벽에 부딪힌 vba  (0) 2012.08.13

+ Recent posts