다음을 통해 공유


방법: Word 문서에 텍스트 삽입

Microsoft Office Word 문서에 텍스트를 삽입하는 데는 주로 세 가지 방법을 사용합니다.

  • 범위에 텍스트를 삽입합니다.

  • 범위의 텍스트를 새 텍스트로 바꿉니다.

  • Selection 개체의 TypeText 메서드를 사용하여 커서 또는 선택 영역에 텍스트를 삽입합니다.

참고

텍스트를 콘텐츠 컨트롤 및 책갈피에 삽입할 수도 있습니다. 자세한 내용은 콘텐츠 컨트롤책갈피 컨트롤을 참조하십시오.

적용 대상: 이 항목의 정보는 Word 2007 및 Word 2010의 문서 수준 프로젝트 및 응용 프로그램 수준 프로젝트에 적용됩니다. 자세한 내용은 Office 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

범위에 텍스트 삽입

Range 개체의 Text 속성을 사용하여 문서에 텍스트를 삽입합니다.

범위에 텍스트를 삽입하려면

  1. 문서의 처음 부분에 범위를 지정하고 New Text라는 텍스트를 삽입합니다.

    다음 코드 예제는 문서 수준 사용자 지정에 사용할 수 있습니다.

    Dim rng As Word.Range = Me.Range(Start:=0, End:=0)
    rng.Text = " New Text "
    
    object start = 0; 
    object end = 0; 
    
    Word.Range rng = this.Range(ref start, ref end); 
    rng.Text = "New Text"; 
    

    다음 코드 예제는 응용 프로그램 수준 추가 기능에 사용할 수 있습니다. 이 코드에서는 활성 문서를 사용합니다.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=0)
    rng.Text = " New Text "
    
    Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
    rng.Text = "New Text";
    
  2. 한 문자에서 삽입된 텍스트의 길이로 확장된 Range 개체를 선택합니다.

    rng.Select()
    
    rng.Select();
    

범위에서 텍스트 바꾸기

지정한 범위에 텍스트가 들어 있으면 해당 범위의 모든 텍스트가 삽입된 텍스트로 바뀝니다.

범위에서 텍스트를 바꾸려면

  1. 문서에 있는 처음 12개의 문자로 구성된 Range 개체를 만듭니다.

    다음 코드 예제는 문서 수준 사용자 지정에 사용할 수 있습니다.

    Dim rng As Word.Range = Me.Range(Start:=0, End:=12)
    
    object start = 0; 
    object end = 12; 
    
    Word.Range rng = this.Range(ref start, ref end); 
    

    다음 코드 예제는 응용 프로그램 수준 추가 기능에 사용할 수 있습니다. 이 코드에서는 활성 문서를 사용합니다.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=12)
    
    Word.Range rng = this.Application.ActiveDocument.Range(0, 12);
    
  2. 이 개체의 문자를 New Text라는 문자열로 바꿉니다.

    rng.Text = " New Text "
    
    rng.Text = "New Text"; 
    
  3. 범위를 선택합니다.

    rng.Select()
    
    rng.Select();
    

TypeText를 사용하여 텍스트 삽입

TypeText 메서드는 선택 영역에 텍스트를 삽입합니다. TypeText의 동작은 사용자 컴퓨터에 설정된 옵션에 따라 달라집니다. 다음 프로시저의 코드에서는 Selection 개체 변수를 선언하고 Overtype 옵션이 설정되어 있는 경우 이 옵션을 해제합니다. Overtype 옵션이 활성화되어 있으면 커서 다음의 모든 텍스트를 덮어쓰게 됩니다.

TypeText 메서드를 사용하여 텍스트를 삽입하려면

  1. Selection 개체 변수를 선언합니다.

    Dim currentSelection As Word.Selection = Application.Selection
    
    Word.Selection currentSelection = Application.Selection; 
    
  2. Overtype 옵션이 설정되어 있으면 이 옵션을 해제합니다.

    If Application.Options.Overtype Then
        Application.Options.Overtype = False
    End If
    
    if (Application.Options.Overtype) 
    { 
        Application.Options.Overtype = false; 
    } 
    
  3. 현재 선택 영역이 삽입 지점인지 확인합니다.

    선택 영역이 삽입 지점인 경우 이 코드는 TypeText를 사용하여 문장을 삽입한 다음 TypeParagraph 메서드를 사용하여 단락 기호를 삽입합니다.

    With currentSelection
    
        ' Test to see if selection is an insertion point.
        If .Type = Word.WdSelectionType.wdSelectionIP Then
            .TypeText("Inserting at insertion point. ")
            .TypeParagraph()
    
    // Test to see if selection is an insertion point.
    if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) 
    { 
        currentSelection.TypeText("Inserting at insertion point. ");
        currentSelection.TypeParagraph(); 
    } 
    
  4. ElseIf 블록의 코드는 선택 영역이 보통 선택 영역인지를 확인합니다. 보통 선택 영역인 경우 다른 If 블록을 통해 ReplaceSelection 옵션이 설정되어 있는지를 확인합니다. 이 옵션이 설정되어 있으면 코드는 선택 영역의 Collapse 메서드를 사용하여 선택 영역을 텍스트에서 선택된 블록의 시작 부분에 있는 삽입 지점으로 축소합니다. 텍스트와 단락 기호를 삽입합니다.

    ElseIf .Type = Word.WdSelectionType.wdSelectionNormal Then
        ' Move to start of selection.
        If Application.Options.ReplaceSelection Then
            .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseStart)
        End If
        .TypeText("Inserting before a text block. ")
        .TypeParagraph()
    
    else 
        if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
        { 
            // Move to start of selection.
            if (Application.Options.ReplaceSelection)
            { 
                object direction = Word.WdCollapseDirection.wdCollapseStart;
                currentSelection.Collapse(ref direction);
            }
            currentSelection.TypeText("Inserting before a text block. ");
            currentSelection.TypeParagraph();
        }
    
  5. 선택 영역이 삽입 지점 또는 선택된 텍스트의 블록이 아닌 경우 Else 블록의 코드는 아무런 작업도 수행하지 않습니다.

    Else
        ' Do nothing.
    End If
    
    else
    {
        // Do nothing.
    }
    

Selection 개체의 TypeBackspace 메서드를 사용하여 키보드에 있는 백스페이스 키의 동작과 유사한 동작을 구현할 수도 있습니다. 그러나, 텍스트를 삽입하고 조작해야 하는 경우 Range 개체를 사용하면 더 세밀한 제어가 가능합니다.

다음 예제에서는 전체 코드를 보여 줍니다. 이 예제를 사용하려면 프로젝트의 ThisDocument 또는 ThisAddIn 클래스에서 이 코드를 실행하십시오.

Friend Sub SelectionInsertText()
    Dim currentSelection As Word.Selection = Application.Selection

    ' Store the user's current Overtype selection
    Dim userOvertype As Boolean = Application.Options.Overtype

    ' Make sure Overtype is turned off.
    If Application.Options.Overtype Then
        Application.Options.Overtype = False
    End If

    With currentSelection

        ' Test to see if selection is an insertion point.
        If .Type = Word.WdSelectionType.wdSelectionIP Then
            .TypeText("Inserting at insertion point. ")
            .TypeParagraph()

        ElseIf .Type = Word.WdSelectionType.wdSelectionNormal Then
            ' Move to start of selection.
            If Application.Options.ReplaceSelection Then
                .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseStart)
            End If
            .TypeText("Inserting before a text block. ")
            .TypeParagraph()

        Else
            ' Do nothing.
        End If
    End With

    ' Restore the user's Overtype selection
    Application.Options.Overtype = userOvertype
End Sub
private void SelectionInsertText() 
{ 
    Word.Selection currentSelection = Application.Selection; 

    // Store the user's current Overtype selection
    bool userOvertype = Application.Options.Overtype;

    // Make sure Overtype is turned off.
    if (Application.Options.Overtype) 
    { 
        Application.Options.Overtype = false; 
    } 

    // Test to see if selection is an insertion point.
    if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) 
    { 
        currentSelection.TypeText("Inserting at insertion point. ");
        currentSelection.TypeParagraph(); 
    } 
    else 
        if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
        { 
            // Move to start of selection.
            if (Application.Options.ReplaceSelection)
            { 
                object direction = Word.WdCollapseDirection.wdCollapseStart;
                currentSelection.Collapse(ref direction);
            }
            currentSelection.TypeText("Inserting before a text block. ");
            currentSelection.TypeParagraph();
        }
        else
        {
            // Do nothing.
        }

    // Restore the user's Overtype selection
    Application.Options.Overtype = userOvertype;
}

참고 항목

작업

방법: 문서에서 텍스트 서식 지정

방법: 문서 내의 범위 정의 및 선택

방법: 문서에서 범위 확장