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

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
まーさ
記事: 20
登録日時: 12年前

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

#1

投稿記事 by まーさ » 11年前

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();
}
ご教授いただけますとうれしいです。
よろしくお願いします。

YuO
記事: 947
登録日時: 15年前
住所: 東京都世田谷区

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

#2

投稿記事 by YuO » 11年前

Word 2013ですが,数式を挿入したあと,「数式を両端揃えする」マクロを生成してみたところ,

コード:

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

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

まーさ
記事: 20
登録日時: 12年前

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

#3

投稿記事 by まーさ » 11年前

YuOさん

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

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

コード:

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

閉鎖

“C言語何でも質問掲示板” へ戻る