2012-01-17 24 views
1

C#のコードからオートメーションを使用して作成しているWord文書に余白を設定したいとします。C#でWord 2010文書の余白を設定する

すべてのヘルプは大幅にあなたはWordアプリケーションのインスタンスを取得する必要があります

答えて

3

をいただければ幸い私はActiveDocument.TopMargin =を使用してプロセスを開始しましたが、私はVB Word.InchesToPoint(.5)に似たC#のコードを見つけることができません。

Word.Application oWord = new Word.Application(); 
oWord.InchesToPoint((float)0.5); 

参照: http://msdn.microsoft.com/en-us/library/ff197549.aspx

+0

回答ありがとうございますが、指示に従っていますが、C#ではMicrosoft.Office.Interop.Word._Application.InchesToPoint(float)に最適なオーバーロードされたメソッドマッチがいくつかの無効な引数を持っています。 – RCam

+0

あなたの値をフロートにキャストしようとしましたか?私の更新された答えを見てください。 – gideon

7

時には最も簡単な方法が動作します。このコード行は、あなたがこのようにWordのApplicationオブジェクトのInchesToPointsメソッドを使用することができます

oWord.ActiveDocument.PageSetup.TopMargin = (float)50; 
1

問題解決:あなたが好きなら、あなたは...あなた自身作ることができ

 Word.Application wrdApplication = new Word.Application(); 
     Word.Document wrdDocument; 
     wrdApplication.Visible = true; 
     wrdDocument = wrdApplication.Documents.Add(); 
     wrdDocument.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; 
     wrdDocument.PageSetup.TopMargin = wrdApplication.InchesToPoints(0.5f); 
     wrdDocument.PageSetup.BottomMargin = wrdApplication.InchesToPoints(0.5f); 
     wrdDocument.PageSetup.LeftMargin = wrdApplication.InchesToPoints(0.5f); 
     wrdDocument.PageSetup.RightMargin = wrdApplication.InchesToPoints(0.5f); 

かを

private float InchesToPoints(float fInches) 
    { 
     return fInches * 72.0f; 
    } 

それはこのようなもので使用することができる。

 Word.Application wrdApplication = new Word.Application(); 
     Word.Document wrdDocument; 
     wrdDocument = wrdApplication.Documents.Add(); 
     wrdDocument.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; 
     wrdDocument.PageSetup.TopMargin = InchesToPoints(0.5f); //half an inch in points 
     wrdDocument.PageSetup.BottomMargin = InchesToPoints(0.5f); 
     wrdDocument.PageSetup.LeftMargin = InchesToPoints(0.5f); 
     wrdDocument.PageSetup.RightMargin = InchesToPoints(0.5f); 
     wrdApplication.Visible = true; 

Wordは、その間隔で1インチあたり72ポイントを使用します。

+0

この回答を少し説明するとよいでしょう(たとえば、1インチあたり72ポイントが測定値です)。 – theMayer

関連する問題