HOWTO: Visual Basic .NET¿¡¼­ Word¸¦ ÀÚµ¿È­ÇÏ¿© »õ ¹®¼­ ¸¸µé±â

±â¼ú ÀÚ·á: 316383 - ÀÌ ¹®¼­°¡ Àû¿ëµÇ´Â Á¦Ç° º¸±â.
¸ðµÎ È®´ë | ¸ðµÎ Ãà¼Ò

¿ä¾à

ÀÌ ¹®¼­¿¡¼­´Â Visual Basic .NETÀÇ ÀÚµ¿È­¸¦ »ç¿ëÇÏ¿© Word¿¡¼­ »õ ¹®¼­¸¦ ¸¸µå´Â ¹æ¹ýÀ» ´Ü°èº°·Î ¼³¸íÇÕ´Ï´Ù.

¿¹Á¦ ÄÚµå

º» ¹®¼­ÀÇ ¿¹Á¦ ÄÚµå´Â ´ÙÀ½ ÀÛ¾÷À» ¼öÇàÇÏ´Â ¹æ¹ýÀ» º¸¿©ÁÝ´Ï´Ù.
  • ÅØ½ºÆ®¿Í ¼­½ÄÀÌ ÀÖ´Â ´Ü¶ôÀ» »ðÀÔÇÕ´Ï´Ù.
  • ¹®¼­ ³»ÀÇ ¿©·¯ ¹üÀ§¸¦ Ž»ö ¹× ¼öÁ¤ÇÕ´Ï´Ù.
  • Ç¥¸¦ »ðÀÔÇϰí, Ç¥¿¡ ¼­½ÄÀ» ÁöÁ¤Çϰí Ç¥¸¦ µ¥ÀÌÅͷΠä¿ó´Ï´Ù.
  • Â÷Æ®¸¦ Ãß°¡ÇÕ´Ï´Ù.
Visual Basic .NETÀÇ ÀÚµ¿È­¸¦ »ç¿ëÇÏ¿© »õ Word ¹®¼­¸¦ ¸¸µé·Á¸é ´ÙÀ½°ú °°ÀÌ ÇϽʽÿÀ.
  1. Microsoft Visual Studio .NETÀ» ½ÃÀÛÇÕ´Ï´Ù. ÆÄÀÏ ¸Þ´º¿¡¼­ »õ·Î ¸¸µé±â¸¦ ´©¸¥ ´ÙÀ½ ÇÁ·ÎÁ§Æ®¸¦ ´©¸¨´Ï´Ù. ÇÁ·ÎÁ§Æ® Çü½Ä¿¡¼­ Visual Basic ÇÁ·ÎÁ§Æ®¸¦ ´©¸¥ ´ÙÀ½ ÅÛÇø´¿¡¼­ Windows ÀÀ¿ë ÇÁ·Î±×·¥À» ´©¸¨´Ï´Ù. ±âº»ÀûÀ¸·Î Form1ÀÌ »ý¼ºµË´Ï´Ù.
  2. Microsoft Word Object Library¿¡ ´ëÇÑ ÂüÁ¶¸¦ Ãß°¡ÇÕ´Ï´Ù. ÀÌ·¸°Ô ÇÏ·Á¸é ´ÙÀ½°ú °°ÀÌ ÇϽʽÿÀ.
    1. ÇÁ·ÎÁ§Æ® ¸Þ´º¿¡¼­ ÂüÁ¶ Ãß°¡¸¦ ´©¸¨´Ï´Ù.
    2. COM ÅÇ¿¡¼­ Microsoft Word °³Ã¼ ¶óÀ̺귯¸®¸¦ ãÀº ´ÙÀ½ ¼±ÅÃÀ» ´©¸¨´Ï´Ù.

      Âü°í Microsoft Office 2003¿¡´Â PIA(±âº» Interop ¾î¼Àºí¸®)°¡ Æ÷ÇԵǾî ÀÖ½À´Ï´Ù. Microsoft Office XP¿¡´Â PIA°¡ Æ÷ÇԵǾî ÀÖÁö¸¸ ´Ù¿î·ÎµåÇÒ ¼ö ¾ø½À´Ï´Ù. Office XP PIA¿¡ ´ëÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº Microsoft ±â¼ú ÀÚ·áÀÇ ´ÙÀ½ ¹®¼­¸¦ ÂüÁ¶ÇϽʽÿÀ.
      328912 INFO: Microsoft Office XP PIA¸¦ ´Ù¿î·ÎµåÇÒ ¼ö ÀÖ´Ù
    3. ÂüÁ¶ Ãß°¡ ´ëÈ­ »óÀÚ¿¡¼­ È®ÀÎÀ» ´­·¯ ¼±ÅÃÇÑ ³»¿ëÀ» Àû¿ëÇÕ´Ï´Ù. ¼±ÅÃÇÑ ¶óÀ̺귯¸®¿¡ ´ëÇØ ·¡ÆÛ¸¦ »ý¼ºÇÒ °ÍÀÎÁö ¹¯´Â ¸Þ½ÃÁö°¡ Ç¥½ÃµÇ¸é ¿¹¸¦ ´©¸¨´Ï´Ù.
  3. º¸±â ¸Þ´º¿¡¼­ µµ±¸ »óÀÚ¸¦ ¼±ÅÃÇÏ¿© µµ±¸ »óÀÚ¸¦ Ç¥½ÃÇÑ ´ÙÀ½ Form1¿¡ Button ÄÁÆ®·ÑÀ» Ãß°¡ÇÕ´Ï´Ù.
  4. Button1À» µÎ ¹ø ´©¸¨´Ï´Ù. ÇØ´ç Æû¿¡ ´ëÇÑ ÄÚµå âÀÌ ³ªÅ¸³³´Ï´Ù.
  5. ÄÚµå â¿¡¼­ ¾Æ·¡ÀÇ Äڵ带
        Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    
        End Sub
    					
    ´ÙÀ½ ÄÚµå·Î ´ëüÇÕ´Ï´Ù.
        Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim oWord As Word.Application
            Dim oDoc As Word.Document
            Dim oTable As Word.Table
            Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
            Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
            Dim oRng As Word.Range
            Dim oShape As Word.InlineShape
            Dim oChart As Object
            Dim Pos As Double
    
            'Start Word and open the document template.
            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add
    
            'Insert a paragraph at the beginning of the document.
            oPara1 = oDoc.Content.Paragraphs.Add
            oPara1.Range.Text = "Heading 1"
            oPara1.Range.Font.Bold = True
            oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter()
    
            'Insert a paragraph at the end of the document.
            '** \endofdoc is a predefined bookmark.
            oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
            oPara2.Range.Text = "Heading 2"
            oPara2.Format.SpaceAfter = 6
            oPara2.Range.InsertParagraphAfter()
    
            'Insert another paragraph.
            oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
            oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
            oPara3.Range.Font.Bold = False
            oPara3.Format.SpaceAfter = 24
            oPara3.Range.InsertParagraphAfter()
    
            'Insert a 3 x 5 table, fill it with data, and make the first row
            'bold and italic.
            Dim r As Integer, c As Integer
            oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
            oTable.Range.ParagraphFormat.SpaceAfter = 6
            For r = 1 To 3
                For c = 1 To 5
                    oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
                Next
            Next
            oTable.Rows.Item(1).Range.Font.Bold = True
            oTable.Rows.Item(1).Range.Font.Italic = True
    
            'Add some text after the table.
            'oTable.Range.InsertParagraphAfter()
            oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
            oPara4.Range.InsertParagraphBefore()
            oPara4.Range.Text = "And here's another table:"
            oPara4.Format.SpaceAfter = 24
            oPara4.Range.InsertParagraphAfter()
    
            'Insert a 5 x 2 table, fill it with data, and change the column widths.
            oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 5, 2)
            oTable.Range.ParagraphFormat.SpaceAfter = 6
            For r = 1 To 5
                For c = 1 To 2
                    oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
                Next
            Next
            oTable.Columns.Item(1).Width = oWord.InchesToPoints(2)   'Change width of columns 1 & 2
            oTable.Columns.Item(2).Width = oWord.InchesToPoints(3)
    
            'Keep inserting text. When you get to 7 inches from top of the
            'document, insert a hard page break.
            Pos = oWord.InchesToPoints(7)
            oDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()
            Do
                oRng = oDoc.Bookmarks.Item("\endofdoc").Range
                oRng.ParagraphFormat.SpaceAfter = 6
                oRng.InsertAfter("A line of text")
                oRng.InsertParagraphAfter()
            Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
            oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            oRng.InsertBreak(Word.WdBreakType.wdPageBreak)
            oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            oRng.InsertAfter("We're now on page 2. Here's my chart:")
            oRng.InsertParagraphAfter()
    
            'Insert a chart and change the chart.
            oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
                ClassType:="MSGraph.Chart.8", FileName _
                :="", LinkToFile:=False, DisplayAsIcon:=False)
            oChart = oShape.OLEFormat.Object
            oChart.charttype = 4 'xlLine = 4
            oChart.Application.Update()
            oChart.Application.Quit()
            'If desired, you can proceed from here using the Microsoft Graph 
            'Object model on the oChart object to make additional changes to the
            'chart.
            oShape.Width = oWord.InchesToPoints(6.25)
            oShape.Height = oWord.InchesToPoints(3.57)
    
            'Add text after the chart.
            oRng = oDoc.Bookmarks.Item("\endofdoc").Range
            oRng.InsertParagraphAfter()
            oRng.InsertAfter("THE END.")
    
            'All done. Close this form.
            Me.Close()
    
        End Sub
    					
  6. ´ÙÀ½ Äڵ带 Form1.vbÀÇ ¸Ç À§¿¡ Ãß°¡ÇÕ´Ï´Ù.
    Imports Word = Microsoft.Office.Interop.Word
    					
  7. F5 ۸¦ ´­·¯ ÇÁ·Î±×·¥À» ºôµåÇÏ°í ½ÇÇàÇÕ´Ï´Ù.
Äڵ尡 ¿Ï·áµÈ ÈÄ ¸¸µé¾îÁø ¹®¼­¸¦ °Ë»çÇϽʽÿÀ. ¹®¼­¿¡ ¼­½ÄÀÌ ÁöÁ¤µÈ ´Ü¶ô, Ç¥ ¹× Â÷Æ®°¡ µÎ ÆäÀÌÁö Æ÷ÇԵǾî ÀÖ½À´Ï´Ù.

ÅÛÇø´ »ç¿ë

ÀÚµ¿È­¸¦ »ç¿ëÇÏ¿© ¸ðµÎ ÇϳªÀÇ °øÅë Çü½ÄÀ¸·Î µÇ¾î ÀÖ´Â ¹®¼­¸¦ ÀÛ¼ºÇÏ´Â °æ¿ì, ¹Ì¸® ¼­½ÄÀÌ ÁöÁ¤µÈ ÅÛÇø´ ±â¹ÝÀÇ »õ ¹®¼­¸¦ »ç¿ëÇÏ¿© ÇÁ·Î¼¼½º¸¦ ½ÃÀÛÇÏ¸é µµ¿òÀÌ µË´Ï´Ù. Word ÀÚµ¿È­ Ŭ¶óÀÌ¾ðÆ®¿¡¼­ ÅÛÇø´À» »ç¿ëÇÏ¸é ¹®¼­¸¦ »õ·Î ÀÛ¼ºÇÏ´Â µ¥ ´ÙÀ½ µÎ °¡Áö Áß¿äÇÑ ÀåÁ¡ÀÌ ÀÖ½À´Ï´Ù.
  • ¹®¼­ Àüü¿¡¼­ °³Ã¼ÀÇ ¼­½Ä ÁöÁ¤°ú ¹èÄ¡¸¦ ´õ Àß Á¦¾îÇÒ ¼ö ÀÖ½À´Ï´Ù.
  • ´õ ÀûÀº Äڵ带 »ç¿ëÇÏ¿© ¹®¼­¸¦ ÀÛ¼ºÇÒ ¼ö ÀÖ½À´Ï´Ù.
ÅÛÇø´À» »ç¿ëÇÏ¸é ¹®¼­ ³»¿¡¼­ÀÇ Ç¥, ´Ü¶ô ¹× ±âŸ °³Ã¼ÀÇ ¹èÄ¡¸¦ ¹Ì¼¼ Á¶Á¤ÇÒ ¼ö ÀÖÀ» »Ó ¾Æ´Ï¶ó ±×·¯ÇÑ °³Ã¼¿¡ ¼­½ÄÀ» Æ÷ÇÔ½Ãų ¼ö ÀÖ½À´Ï´Ù. ÀÚµ¿È­¸¦ »ç¿ëÇÏ¸é ´ÙÀ½°ú °°Àº Äڵ带 »ç¿ëÇÏ¿© ÅÛÇø´ ±â¹ÝÀÇ »õ ¹®¼­¸¦ ¸¸µé ¼ö ÀÖ½À´Ï´Ù.
oWord.Documents.Add "<Path to your template>\MyTemplate.dot"
				
ÅÛÇø´¿¡¼­ ÀÚµ¿È­ Ŭ¶óÀÌ¾ðÆ®°¡ ¹®¼­ÀÇ Æ¯Á¤ À§Ä¡¿¡ º¯¼ö ÅØ½ºÆ®¸¦ ä¿ï ¼ö ÀÖµµ·Ï ´ÙÀ½°ú °°ÀÌ Ã¥°¥ÇǸ¦ Á¤ÀÇÇÒ ¼ö ÀÖ½À´Ï´Ù.
oDoc.Bookmarks.Item("MyBookmark").Range.Text = "Some Text Here"
				
ÅÛÇø´À» »ç¿ëÇÒ ¶§ÀÇ ¶Ç ´Ù¸¥ ÀåÁ¡Àº ´ÙÀ½°ú °°ÀÌ ·±Å¸ÀÓ¿¡ Àû¿ëÇÒ ¼­½Ä ½ºÅ¸ÀÏÀ» ¸¸µé¾î¼­ ÀúÀåÇÒ ¼ö ÀÖ´Ù´Â °ÍÀÔ´Ï´Ù.
oDoc.Bookmarks.Item("MyBookmark").Range.Style = "MyStyle"
				
¶Ç´Â
oWord.Selection.Style = "MyStyle"
				

ÂüÁ¶

Visual Basic .NET¸¦ »ç¿ëÇÏ¿© Microsoft Word¸¦ ÀÚµ¿È­ÇÏ´Â ¹æ¹ý¿¡ ´ëÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº Microsoft ±â¼ú ÀÚ·áÀÇ ´ÙÀ½ ¹®¼­¸¦ ÂüÁ¶ÇϽʽÿÀ.
301656 HOWTO: Visual Basic .NET¿¡¼­ ÆíÁö º´ÇÕÀ» ¼öÇàÇϱâ À§ÇØ Microsoft Word ÀÚµ¿È­
ÀÚ¼¼ÇÑ ³»¿ëÀº ´ÙÀ½ MSDN(Microsoft Developer Network) À¥ »çÀÌÆ®¸¦ ÂüÁ¶ÇϽʽÿÀ.
OfficeÀÇ Word(¿µ¹®)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office112000.asp

Word¿¡ ´ëÇÑ Ãß°¡ ¼³¸í(¿µ¹®)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office12072000.asp




Microsoft Á¦Ç° °ü·Ã ±â¼ú Àü¹®°¡µé°ú ¿Â¶óÀÎÀ¸·Î Á¤º¸¸¦ ±³È¯ÇϽ÷Á¸é Microsoft ´º½º ±×·ì¿¡ Âü¿©ÇϽñ⠹ٶø´Ï´Ù.

¼Ó¼º

±â¼ú ÀÚ·á: 316383 - ¸¶Áö¸· °ËÅä: 2007³â 1¿ù 17ÀÏ ¼ö¿äÀÏ - ¼öÁ¤: 7.3
º» ¹®¼­ÀÇ Á¤º¸´Â ´ÙÀ½ÀÇ Á¦Ç°¿¡ Àû¿ëµË´Ï´Ù.
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
Ű¿öµå:?
kbhowtomaster kbautomation KB316383

Çǵå¹é º¸³»±â