ページ 11

C# Wordの数式エディタのAlignParagraphCenterが設定出来ない

Posted: 2014年4月07日(月) 22:41
by まーさ
Microsoft Office Word の docxファイルを Visual Studio 2012 の Office開発ツール(C#) を使って編集しています。
Wordファイルには、数式エディタを使って記述した数式を記述しており、プログラムで文章の中央に移動したいです。

以下のようにやっていますがうまく中央に変更できません。

コード:

using Microsoft.Office.Interop.Word;

コード:

Application application = new Application();
Documents documents = application.Documents;
Document document = null;
document = (Document)documents.Open("Wordファイルのパス");

// 文書内のOMathオブジェクトを取得
OMaths maths = document.OMaths;
foreach (OMath math in maths) {
  Range rng = math.Range;
  rng.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
  rng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
  rng.Select();
}
ご教授いただけますとうれしいです。
よろしくお願いします。

Re: C# Wordの数式エディタのAlignParagraphCenterが設定出来ない

Posted: 2014年4月07日(月) 23:09
by YuO
Word 2013ですが,数式を挿入したあと,「数式を両端揃えする」マクロを生成してみたところ,

コード:

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.OMaths(1).ParentOMath.Justification = wdOMathJcCenterGroup
End Sub
と出力されました。

VSTOであっても,やっていることは同じなので,OMathオブジェクトのJustificationプロパティ値を変更することで対処できないでしょうか。

Re: C# Wordの数式エディタのAlignParagraphCenterが設定出来ない

Posted: 2014年4月07日(月) 23:41
by まーさ
YuOさん

ありがとうございます。うまくいきました。
OMathの親の Justification (行端揃え)をCenterにすることで、数式のエディタを中央に変更できました。
Justification プロパティを知りませんでした。親に設定するんですね、大変勉強になりました。

以下が動作したコードです。

コード:

OMaths maths = document.OMaths;
foreach (OMath math in maths) {
  math.ParentOMath.Justification = WdOMathJc.wdOMathJcCenterGroup;
}