2012-08-16 4 views
2

의 설정 스타일 :삽입 전에 선택 후 텍스트와 새 텍스트

Selection.InsertBefore "start" 
Selection.InsertAfter "end" 

는하지만 삽입 된 텍스트의 스타일을 제어 할 수 없습니다. 새롭게 삽입 된 텍스트를 특정 스타일로 설정하려면 어떻게해야합니까?

답변

4

아래에는 Insert After와 Insert Before를 처리하는 두 개의 별도 코드가 있습니다. 일단 텍스트를 삽입하면 삽입 된 위치에 따라 삽입 된 텍스트를 선택한 다음 스타일을 변경해야합니다.

Sub test() 
Dim StartingCount As Long 
Dim InsertBeforeCount As Long 

With ActiveDocument 
    StartingCount = .Characters.Count 
    Selection.InsertBefore "start" 
    InsertBeforeCount = .Characters.Count - StartingCount 
    .Range(1, InsertBeforeCount).Font.Bold = True 
    Selection.InsertAfter "end" 
    .Range(StartingCount + InsertBeforeCount, .Characters.Count).Font.Italic = True 
End With 
End Sub 
: 여기
Sub InsertAfter() 
    Dim wrd As String 
    Dim rng As Range 

    wrd = "End" 

    Set rng = Selection.Range 

    rng.InsertAfter wrd 

    '~~> Remove selection. This will move the cursor at end of selected word 
    Selection.MoveRight Unit:=wdCharacter, Count:=1 
    '~~> Select the inserted word 
    Selection.MoveRight Unit:=wdCharacter, Count:=Len(wrd), Extend:=wdExtend 
    '~~> Change Style 
    Selection.Style = ActiveDocument.Styles("List Paragraph") 
End Sub 

Sub InsertBefore() 
    Dim wrd As String 
    Dim rng As Range 

    wrd = "Start" 

    Set rng = Selection.Range 

    rng.InsertBefore wrd 

    '~~> Remove selection. This will move the cursor at begining of inserted word 
    Selection.MoveLeft Unit:=wdCharacter, Count:=1 
    '~~> Select the inserted word 
    Selection.MoveRight Unit:=wdCharacter, Count:=Len(wrd), Extend:=wdExtend 
    '~~> Change Style 
    Selection.Style = ActiveDocument.Styles("List Paragraph") 
End Sub 
1

은 간단한 예제
관련 문제