Share via


Word.Document class

Document オブジェクトは、最上位レベルのオブジェクトです。 ドキュメント オブジェクトには、1 つ以上のセクション、コンテンツ コントロール、ドキュメントの内容を含む本文が含まれています。

Extends

注釈

[ API セット: WordApi 1.1 ]

プロパティ

body

メイン ドキュメントの body オブジェクトを取得します。 本文は、ヘッダー、フッター、脚注、テキスト ボックスなどを除外するテキストです。

changeTrackingMode

ChangeTracking モードを指定します。

contentControls

ドキュメント内のコンテンツ コントロール オブジェクトのコレクションを取得します。 これには、ドキュメントの本文、ヘッダー、フッター、テキスト ボックスなどのコンテンツ コントロールが含まれます。

context

オブジェクトに関連付けられている要求コンテキスト。 これにより、アドインのプロセスが Office ホスト アプリケーションのプロセスに接続されます。

customXmlParts

ドキュメント内のカスタム XML パーツを取得します。

properties

ドキュメントのプロパティを取得します。

saved

ドキュメント内の変更が保存されているかどうかを示します。 値 true は、ドキュメントが保存されてから変更されていないことを示します。

sections

ドキュメント内のセクション オブジェクトのコレクションを取得します。

settings

ドキュメント内のアドインの設定を取得します。

メソッド

deleteBookmark(name)

ブックマークが存在する場合は、ドキュメントから削除します。

getBookmarkRange(name)

ブックマークの範囲を取得します。 ブックマークが ItemNotFound 存在しない場合は、エラーをスローします。

getBookmarkRangeOrNullObject(name)

ブックマークの範囲を取得します。 ブックマークが存在しない場合、このメソッドは プロパティが に設定されたオブジェクトをisNullObjecttrue返します。 詳細については、「 *OrNullObject メソッドとプロパティ」を参照してください。

getSelection()

ドキュメントの現在の選択範囲を取得します。 複数の選択はサポートされていません。

load(options)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNames)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNamesAndPaths)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

save(saveBehavior, fileName)

ドキュメントを保存します。

save(saveBehaviorString, fileName)

ドキュメントを保存します。

set(properties, options)

オブジェクトの複数のプロパティを同時に設定します。 適切なプロパティを持つプレーン オブジェクトまたは同じ型の別の API オブジェクトを渡すことができます。

set(properties)

既存の読み込まれたオブジェクトに基づいて、オブジェクトに複数のプロパティを同時に設定します。

toJSON()

API オブジェクトが に渡されたときにより便利な出力を提供するために、JavaScript toJSON() メソッドを JSON.stringify()オーバーライドします。 (JSON.stringifyさらに、渡される オブジェクトの メソッドを呼び出toJSONします)。一方、元のWord。Document オブジェクトは API オブジェクトです。メソッドは、元のtoJSONオブジェクトから読み込まれた子プロパティの浅いコピーを含むプレーンな JavaScript オブジェクト (としてWord.Interfaces.DocumentData型指定) を返します。

track()

ドキュメントの環境変更に基づいて自動的に調整する目的でオブジェクトを追跡します。 この呼び出しは、context.trackedObjects.add(thisObject)の短縮形です。 このオブジェクトを呼び出しで .sync 使用していて、".run" バッチのシーケンシャル実行の外部で使用していて、プロパティを設定するとき、またはオブジェクトのメソッドを呼び出すときに "InvalidObjectPath" エラーが発生する場合は、オブジェクトが最初に作成されたときに追跡対象のオブジェクト コレクションにオブジェクトを追加する必要があります。 このオブジェクトがコレクションの一部である場合は、親コレクションも追跡する必要があります。

untrack()

前に追跡されていた場合、このオブジェクトに関連付けられているメモリを解放します。 この呼び出しは、context.trackedObjects.remove(thisObject)の短縮形です。 追跡対象オブジェクトが多いとホスト アプリケーションの動作が遅くなります。追加したオブジェクトが不要になったら、必ずそれを解放してください。 メモリ解放が有効になる前に を呼び出す context.sync() 必要があります。

プロパティの詳細

body

メイン ドキュメントの body オブジェクトを取得します。 本文は、ヘッダー、フッター、脚注、テキスト ボックスなどを除外するテキストです。

readonly body: Word.Body;

プロパティ値

注釈

[ API セット: WordApi 1.1 ]

changeTrackingMode

ChangeTracking モードを指定します。

changeTrackingMode: Word.ChangeTrackingMode | "Off" | "TrackAll" | "TrackMineOnly";

プロパティ値

Word.ChangeTrackingMode | "Off" | "TrackAll" | "TrackMineOnly"

注釈

[ API セット: WordApi 1.4 ]

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-change-tracking.yaml

// Gets the current change tracking mode.
await Word.run(async (context) => {
  const document = context.document;
  document.load("changeTrackingMode");
  await context.sync();

  if (document.changeTrackingMode === Word.ChangeTrackingMode.trackMineOnly) {
    console.log("Only my changes are being tracked.");
  } else if (document.changeTrackingMode === Word.ChangeTrackingMode.trackAll) {
    console.log("Everyone's changes are being tracked.");
  } else {
    console.log("No changes are being tracked.");
  }
});

contentControls

ドキュメント内のコンテンツ コントロール オブジェクトのコレクションを取得します。 これには、ドキュメントの本文、ヘッダー、フッター、テキスト ボックスなどのコンテンツ コントロールが含まれます。

readonly contentControls: Word.ContentControlCollection;

プロパティ値

注釈

[ API セット: WordApi 1.1 ]

context

オブジェクトに関連付けられている要求コンテキスト。 これにより、アドインのプロセスが Office ホスト アプリケーションのプロセスに接続されます。

context: RequestContext;

プロパティ値

customXmlParts

ドキュメント内のカスタム XML パーツを取得します。

readonly customXmlParts: Word.CustomXmlPartCollection;

プロパティ値

注釈

[ API セット: WordApi 1.4 ]

properties

ドキュメントのプロパティを取得します。

readonly properties: Word.DocumentProperties;

プロパティ値

注釈

[ API セット: WordApi 1.3 ]

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/30-properties/get-built-in-properties.yaml

await Word.run(async (context) => {
    const builtInProperties = context.document.properties;
    builtInProperties.load("*"); // Let's get all!

    await context.sync();
    console.log(JSON.stringify(builtInProperties, null, 4));
});

saved

ドキュメント内の変更が保存されているかどうかを示します。 値 true は、ドキュメントが保存されてから変更されていないことを示します。

readonly saved: boolean;

プロパティ値

boolean

注釈

[ API セット: WordApi 1.1 ]

sections

ドキュメント内のセクション オブジェクトのコレクションを取得します。

readonly sections: Word.SectionCollection;

プロパティ値

注釈

[ API セット: WordApi 1.1 ]

settings

ドキュメント内のアドインの設定を取得します。

readonly settings: Word.SettingCollection;

プロパティ値

注釈

[ API セット: WordApi 1.4 ]

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-settings.yaml

// Gets all custom settings this add-in set on this document.
await Word.run(async (context) => {
  const settings = context.document.settings;
  settings.load("items");
  await context.sync();

  if (settings.items.length == 0) {
    console.log("There are no settings.");
  } else {
    console.log("All settings:");
    for (let i = 0; i < settings.items.length; i++) {
      console.log(settings.items[i]);
    }
  }
});

メソッドの詳細

deleteBookmark(name)

ブックマークが存在する場合は、ドキュメントから削除します。

deleteBookmark(name: string): void;

パラメーター

name

string

必須です。 大文字と小文字を区別しないブックマーク名。

戻り値

void

注釈

[ API セット: WordApi 1.4 ]

getBookmarkRange(name)

ブックマークの範囲を取得します。 ブックマークが ItemNotFound 存在しない場合は、エラーをスローします。

getBookmarkRange(name: string): Word.Range;

パラメーター

name

string

必須です。 大文字と小文字を区別しないブックマーク名。

戻り値

注釈

[ API セット: WordApi 1.4 ]

getBookmarkRangeOrNullObject(name)

ブックマークの範囲を取得します。 ブックマークが存在しない場合、このメソッドは プロパティが に設定されたオブジェクトをisNullObjecttrue返します。 詳細については、「 *OrNullObject メソッドとプロパティ」を参照してください。

getBookmarkRangeOrNullObject(name: string): Word.Range;

パラメーター

name

string

必須です。 大文字と小文字を区別しないブックマーク名。

戻り値

注釈

[ API セット: WordApi 1.4 ]

getSelection()

ドキュメントの現在の選択範囲を取得します。 複数の選択はサポートされていません。

getSelection(): Word.Range;

戻り値

注釈

[ API セット: WordApi 1.1 ]

// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    const textSample = 'This is an example of the insert text method. This is a method ' + 
        'which allows users to insert text into a selection. It can insert text into a ' +
        'relative location or it can overwrite the current selection. Since the ' +
        'getSelection method returns a range object, look up the range object documentation ' +
        'for everything you can do with a selection.';
    
    // Create a range proxy object for the current selection.
    const range = context.document.getSelection();
    
    // Queue a command to insert text at the end of the selection.
    range.insertText(textSample, Word.InsertLocation.end);
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
    console.log('Inserted the text at the end of the selection.');
});  

load(options)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(options?: Word.Interfaces.DocumentLoadOptions): Word.Document;

パラメーター

options
Word.Interfaces.DocumentLoadOptions

読み込むオブジェクトのプロパティのオプションを提供します。

戻り値

// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Create a proxy object for the document.
    const thisDocument = context.document;
    
    // Queue a command to load content control properties.
    thisDocument.load('contentControls/id, contentControls/text, contentControls/tag');
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
    if (thisDocument.contentControls.items.length !== 0) {
        for (let i = 0; i < thisDocument.contentControls.items.length; i++) {
            console.log(thisDocument.contentControls.items[i].id);
            console.log(thisDocument.contentControls.items[i].text);
            console.log(thisDocument.contentControls.items[i].tag);
        }
    } else {
        console.log('No content controls in this document.');
    }
});

load(propertyNames)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNames?: string | string[]): Word.Document;

パラメーター

propertyNames

string | string[]

読み込むプロパティを指定するコンマ区切り文字列または文字列の配列。

戻り値

load(propertyNamesAndPaths)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Word.Document;

パラメーター

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select は、読み込むプロパティを指定するコンマ区切り文字列で propertyNamesAndPaths.expand 、読み込むナビゲーション プロパティを指定するコンマ区切りの文字列です。

戻り値

save(saveBehavior, fileName)

ドキュメントを保存します。

save(saveBehavior?: Word.SaveBehavior, fileName?: string): void;

パラメーター

saveBehavior
Word.SaveBehavior

オプション。 保存動作は、'Save' または 'Prompt' である必要があります。 既定値は 'Save' です。

fileName

string

オプション。 ファイル名 (ファイル拡張子を除外)。 新しいドキュメントに対してのみ有効になります。

戻り値

void

注釈

[ API セット: WordApi 1.1 ]

注: パラメーターと fileName パラメーターは saveBehavior WordApi 1.5 で導入されました。

// Run a batch operation against the Word object model.
await Word.run(async (context) => {
    
    // Create a proxy object for the document.
    const thisDocument = context.document;

    // Queue a command to load the document save state (on the saved property).
    thisDocument.load('saved');    
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    await context.sync();
        
    if (thisDocument.saved === false) {
        // Queue a command to save this document.
        thisDocument.save();
        
        // Synchronize the document state by executing the queued commands, 
        // and return a promise to indicate task completion.
        await context.sync();
        console.log('Saved the document');
    } else {
        console.log('The document has not changed since the last save.');
    }
});
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/save-close.yaml

// Saves the document with default behavior
// for current state of the document.
await Word.run(async (context) => {
  context.document.save();
  await context.sync();
});

save(saveBehaviorString, fileName)

ドキュメントを保存します。

save(saveBehaviorString?: "Save" | "Prompt", fileName?: string): void;

パラメーター

saveBehaviorString

"Save" | "Prompt"

オプション。 保存動作は、'Save' または 'Prompt' である必要があります。 既定値は 'Save' です。

fileName

string

オプション。 ファイル名 (ファイル拡張子を除外)。 新しいドキュメントに対してのみ有効になります。

戻り値

void

注釈

[ API セット: WordApi 1.1 ]

注: パラメーターと fileName パラメーターは saveBehavior WordApi 1.5 で導入されました。

set(properties, options)

オブジェクトの複数のプロパティを同時に設定します。 適切なプロパティを持つプレーン オブジェクトまたは同じ型の別の API オブジェクトを渡すことができます。

set(properties: Interfaces.DocumentUpdateData, options?: OfficeExtension.UpdateOptions): void;

パラメーター

properties
Word.Interfaces.DocumentUpdateData

メソッドが呼び出されるオブジェクトのプロパティに等形的に構造化されたプロパティを持つ JavaScript オブジェクト。

options
OfficeExtension.UpdateOptions

properties オブジェクトが読み取り専用プロパティを設定しようとした場合にエラーを抑制するオプションを提供します。

戻り値

void

set(properties)

既存の読み込まれたオブジェクトに基づいて、オブジェクトに複数のプロパティを同時に設定します。

set(properties: Word.Document): void;

パラメーター

properties
Word.Document

戻り値

void

toJSON()

API オブジェクトが に渡されたときにより便利な出力を提供するために、JavaScript toJSON() メソッドを JSON.stringify()オーバーライドします。 (JSON.stringifyさらに、渡される オブジェクトの メソッドを呼び出toJSONします)。一方、元のWord。Document オブジェクトは API オブジェクトです。メソッドは、元のtoJSONオブジェクトから読み込まれた子プロパティの浅いコピーを含むプレーンな JavaScript オブジェクト (としてWord.Interfaces.DocumentData型指定) を返します。

toJSON(): Word.Interfaces.DocumentData;

戻り値

track()

ドキュメントの環境変更に基づいて自動的に調整する目的でオブジェクトを追跡します。 この呼び出しは、context.trackedObjects.add(thisObject)の短縮形です。 このオブジェクトを呼び出しで .sync 使用していて、".run" バッチのシーケンシャル実行の外部で使用していて、プロパティを設定するとき、またはオブジェクトのメソッドを呼び出すときに "InvalidObjectPath" エラーが発生する場合は、オブジェクトが最初に作成されたときに追跡対象のオブジェクト コレクションにオブジェクトを追加する必要があります。 このオブジェクトがコレクションの一部である場合は、親コレクションも追跡する必要があります。

track(): Word.Document;

戻り値

untrack()

前に追跡されていた場合、このオブジェクトに関連付けられているメモリを解放します。 この呼び出しは、context.trackedObjects.remove(thisObject)の短縮形です。 追跡対象オブジェクトが多いとホスト アプリケーションの動作が遅くなります。追加したオブジェクトが不要になったら、必ずそれを解放してください。 メモリ解放が有効になる前に を呼び出す context.sync() 必要があります。

untrack(): Word.Document;

戻り値