2013-07-17 6 views
0

나는 2010 년 단어에 다음과 같은 매크로가 있습니다. 매크로를 실행할 때 값 1 (코펜하겐)을 미리 선택하고 싶습니다. - 코펜하겐이 미리 선택된 상태로 내 콘텐트 드롭 다운 목록을 문서에 놓습니다. 나는이 작업을 수행 할 방법매크로에서 옵션을 기본으로 선택된 단어로 만드는 방법은 무엇입니까?

: 사전에

Sub Cities() 
' 
' Cities Macro 
' 
' 
Selection.Range.ContentControls.Add (wdContentControlDropdownList) 
Selection.ParentContentControl.Title = "Cities" 
Selection.ParentContentControl.LockContentControl = False 
Selection.ParentContentControl.DropdownListEntries.Add Text:="Copenhagen", Value:="1" 
Selection.ParentContentControl.DropdownListEntries.Add Text:="New York", Value:="2" 
Selection.ParentContentControl.DropdownListEntries.Add Text:="London", Value:="3" 
Selection.ParentContentControl.DropdownListEntries.Add Text:="Paris", Value:="4" 
Selection.MoveRight Unit:=wdCharacter, Count:=1 
End Sub 

감사합니다!

/앤더스 H.

답변

0

당신이 the documentation을 공부 했습니까? 이것은 Word 2013에 대한 것이지만이 페이지의 2010 버전보다 자세한 내용입니다.

Sub Cities() 
    ' 
    ' Cities Macro 
    ' 
    ' 
    Dim objCC As ContentControl 
    Dim objCE As ContentControlListEntry 

    Set objCC = Selection.Range.ContentControls.Add(wdContentControlDropdownList) 
    With objCC 
     .Title = "Cities" 
     .LockContentControl = False 
     '.DropdownListEntries.Add Text:="Copenhagen", Value:="1" 
     Set objCE = .DropdownListEntries.Add("Copenhagen", "1") 
     objCE.Select 
     'or 
     '.DropdownListEntries.Add("Copenhagen", "1").Select 

     .DropdownListEntries.Add Text:="New York", Value:="2" 
     .DropdownListEntries.Add Text:="London", Value:="3" 
     .DropdownListEntries.Add Text:="Paris", Value:="4" 
    End With 
    'Selection.MoveRight Unit:=wdCharacter, Count:=1 
    'Selection is no longer in the document range 
End Sub 

여기서는 Select 방법을 사용하여 코펜하겐을 선택합니다. 그러나 Selection이 더 이상 문서 범위에 없다는 것을 의미합니다.

ActiveDocument.Characters(1).Select 

(또는 기타 다른 방법)을 사용하여 코펜하겐을 선택된 상태로두고 커서를 문서 범위로 다시 이동시킬 수 있습니다.

+0

내가 같은 매크로에 주위에 텍스트를 넣을 것인지? Fx : "나는 의 2013 년 7 월 2 일에 왔습니다." –

+0

무엇? 나는 이해하지 못한다. –

+0

VBA에 불쌍해서 죄송합니다 ... 매크로에 som 텍스트가 있고 ContentControlDropdownList 다음에 som 텍스트가 다시 나타납니다. FX : "나는 2012 년 7 월 2 일 에 왔습니다." –

0
Sub Cities() 
' 
' Cities Macro 
' 
' 

Selection.TypeText Text:='I learned English with Google translate in " 
Dim oldRange as Range 
Set oldRange = Selection ' I didn't try this but it should work 

Dim objCC As ContentControl 

Set objCC = Selection.Range.ContentControls.Add(wdContentControlDropdownList) 
With objCC 
    .Title = "Cities" 
    .LockContentControl = False 
    .DropdownListEntries.Add("Copenhagen", "1").Select 
    .DropdownListEntries.Add("New York", "2") 
    .DropdownListEntries.Add("London",  "3") 
    .DropdownListEntries.Add("Paris",  "4") 
End With 
Set Selection = oldRange ' didn't try this but should work. We should be back in the document 
' or try ActiveDocument.Characters(1).Select followed by a move to the end of the doc. 
' or put in a placeholder (text like "zzzxxxzzz" and then find it) or a bookmark and then use it. 
Selection.MoveRight Unit:=wdCharacter, Count:=1 ' didn't try this but should work. If it "falls" back into the content control then try the following: 
Selection.TypeText Text:=" on the 2nd of July, 2013." 
' if there is an error mark out the last line and check where the selection ends up after MoveRight. 

최종 하위

관련 문제