2011-12-19 5 views
0

커서 위치에 MS Word 모양을 삽입하는 방법을 찾고 있습니다.커서 위치에 단어 모양 삽입

 Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range; 

     //Get the id of the MS Word shape to be inserted 
     int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle; 
     //Get the value of the name attribute from the selected tree view item 
     string nodeText = treeViewItem.GetAttribute("name"); 

     //Add a new shape to the MS Word designer and set shape properties 
     var shape = CurrRange.Document.Shapes.AddShape(shapeId, 170, 200, 100, 20); 
     shape.AlternativeText = String.Format("Alt {0}", nodeText); 
     shape.TextFrame.ContainingRange.Text = nodeText; 
     shape.TextFrame.ContainingRange.Font.Size = 8; 

모양이 하드 코드 삽입됩니다 위치는 :

이 2 차와 3 차 매개 변수에서 볼 수있는 순간에 나는 소정 위치에 모양을 삽입 다음 코드가 AddShape() 방법

= 200 위치는 도형의 상단까지의 포인트에서 측정 된 도형의 왼쪽 가장자리에 점 측정 위치

170 = 012,351,

Range 개체의 속성과 메서드를 살펴 봤지만 필요한 위치 값을 저장하지 못하는 것 같습니다.

답변

2

AddShape의 마지막 매개 변수는 Anchor이며 범위 개체를 필요로합니다. 그로 범위를 통과하십시오 :

var shape = CurrRange.Document.Shapes.AddShape(shapeId, 0, 0, 100, 20,CurrRange); 

업데이트 : 그것은 앵커을 존중하지 않는 워드 2010 문서에 bug가있는 것 같습니다. .doc 파일로 문서를 저장하고 다시 테스트하십시오. 그렇게하면 단락의 시작 부분에 문서가 고정됩니다. 위의 링크는 Microsoft 포럼에 대한 것이므로 문제에 대한 연결 버그 보고서를 찾을 수 없습니다. 이 형태는 여전히 같은 위치에 삽입 퍼지고하지 않습니다, 나는 이미 시도했습니다

Microsoft.Office.Interop.Word.Range CurrRange = Globals.ThisAddIn.Application.Selection.Range; 

//Get the id of the MS Word shape to be inserted 
int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle; 

//Get the value of the name attribute from the selected tree view item 
string nodeText = "Hello World"; 

var left = Globals.ThisAddIn.Application.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage); 
var top = Globals.ThisAddIn.Application.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage); 

//Add a new shape to the MS Word designer and set shape properties 
var shape = CurrRange.Document.Shapes.AddShape(shapeId, left, top, 100, 20); 
shape.AlternativeText = String.Format("Alt {0}", nodeText); 
shape.TextFrame.ContainingRange.Text = nodeText; 
shape.TextFrame.ContainingRange.Font.Size = 8; 
+0

:

해결 방법은 상단을 지정하는 것하고 선택의 위치에 따라 왼쪽 –

+0

Microsoft의 포럼에서 찾은 정보로 답변을 업데이트했습니다. –

+0

나는 또한 msdn에 그 포스트를 보았습니다 - http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/53d9baff-c10c-4655-822a-4c7a1b0fa885/ 그러나 왼쪽과 맨 위의 값은 항상 그렇지 않으면 커서의 왼쪽 위와 왼쪽으로 몇 cm 정도 삽입됩니다 ... 이것은 매우 이상하고 정확히 수행해야하는 이유는 정확히 모르지만 작동하는 것 같습니다. 감사합니다. 노력에 대한 답변으로 표시 –

관련 문제