インデザインスクリプト
2016/02/02
13.インデザインスクリプト 初級講座(9) 状態確認
管理者用テキストフレームに文字を流し込んだ結果を知る事は、自動化を進めるには必ず必要となります。
テキストフレームで文字の溢れを確認して、その結果どうするかは、それぞれ異なりますが、ここでは、単純にフォントサイズを小さくして対応する例で示してます。
また、一行のデータが、フレーム幅によって折り返されている場合の例も紹介してますが、その行に長体をかけた対応例です。
ボタンのクリックイベントに、ページの最後に記述している スクリプト全体を記入してください。コピーしても問題ありません。
ボタンのクリックで、スクリプトがスターとします。
指定した、インデザインドキメントだ開き、 テキストフレームが作られて、操作対象となる文字が流し込まれています。
テキストフレームはオーバフローの状態です。
溢れの対処
MsgBox "フレーム オーバフロー"
Font_OVF = 0
Do While Font_OVF = 0
If myTextFrame.Overflows Then
font_P = font_P - 0.5
myTextFrame.ParentStory.PointSize = CStr(font_P)
Else
Font_OVF = 1
End If
Loop
If myTextFrame.Overflows Then で、フレームがオーバーフローしているか確認をします。
オーバーフローしている場合は、フォントサイズを 0.5ポイントずつ小さくしながら繰り返します。
例では、ホームページの部分が溢れて見えない状態ですが、フォントサイズを小さくした結果、オーバーフローが解消しました。
フィット
MsgBox "フィットします"
myTextFrame.Fit (idFitOptions.idFrameToContent) 'idFrameToContent = 1718906723 (&H66746F63)
haichi_1_zahyo = myTextFrame.GeometricBounds
MsgBox "YS =" & haichi_1_zahyo(0) & " XS =" & haichi_1_zahyo(1) & " YE =" & haichi_1_zahyo(2) & " XE =" & haichi_1_zahyo(3)
フィットした後、フレームのサイズを確認しました。
テキストフレームの行数と、折り返された見た目の行数を取得
L_cunt = myTextFrame.ParentStory.Paragraphs.Count
G_cunt = myTextFrame.Lines.Count
MsgBox L_cunt
MsgBox G_cunt
4行ですが、2行目が折り返され、見た目は5行です。
折り返された行を探しながら、長体をかけて折り返しを解除するスクリプトです。
If L_cunt < G_cunt Then
For aaaaaa = 1 To L_cunt Step 1
'tyoutai_gyo = tyoutai_gyo + 1
Set myStartCharacter = myTextFrame.ParentStory.Paragraphs.Item(tyoutai_gyo)
Set myEndCharacter = myTextFrame.ParentStory.Paragraphs.Item(tyoutai_gyo)
lincounA = myTextFrame.Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1).Lines.Count
If lincounA > 1 Then
Font_OVF = 0
tyoutai = 100
Do While Font_OVF = 0
tyoutai = tyoutai - 1
myTextFrame.Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1).HorizontalScale = tyoutai
lincounA = myTextFrame.Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1).Lines.Count
If lincounA = 1 Then
Font_OVF = 1
Else
If tyoutai <= 50 Then
Font_OVF = 1
End If
End If
Loop
End If
tyoutai_gyo = tyoutai_gyo + 1
Next
End If
2行目の電話/ファックスの行が、長体がかかって、折り返しが解除されました。
MsgBox "フィットします"
myTextFrame.Fit (idFitOptions.idFrameToContent) 'idFrameToContent = 1718906723 (&H66746F63)
MsgBox "YS =" & haichi_1_zahyo(0) & " XS =" & haichi_1_zahyo(1) & " YE =" & haichi_1_zahyo(2) & " XE =" & haichi_1_zahyo(3)
フットして、フレームの座標を取得しました。
スクリプト全体
Private Sub CommandButton1_Click()
Dim myInDesign, myDocument, myTextFrame
Dim haichi_1_YS, haichi_1_XS, haichi_1_YE, haichi_1_XE
Dim filde_jyusyo1, filde_jyusyo2, filde_jyusyo3, filde_jyusyo4
Dim Me_size_GH, Me_size_GY
Dim dir_mei, INDD_name
Dim mov_XS, mov_YS
Dim FrameZahyo
Dim myTabStop1, myTabStop2
Dim myStartCharacter, myEndCharacter
Dim Font_OVF, font_P, lincounA, tyoutai_gyo, tyoutai
Rem この行は、コメント行です。 先頭に Rem を記入してコメント行とします。
' 'は、この後ろの記述がコメントとなります。 行の記述途中から後ろをコメントとします。
Rem 次の行は、インデザインを起動します。
Set myInDesign = CreateObject("InDesign.Application.CC_J") 'インデザインを起動する
MsgBox "ドキュメントを開きます!"
dir_mei = "E:\NAVI_VBA"
INDD_name = dir_mei & "\テストサンプル.indd"
Set myDocument = myInDesign.Open(INDD_name)
Rem テキストフレームサイズを設定
Set myTextFrame = myDocument.TextFrames.Add
Rem テキストフレームサイズを変更
haichi_1_YS = 100
haichi_1_XS = 20
haichi_1_YE = 150
haichi_1_XE = 150
myTextFrame.GeometricBounds = Array(CStr(haichi_1_YS) + "mm", CStr(haichi_1_XS) + "mm", CStr(haichi_1_YE) + "mm", CStr(haichi_1_XE) + "mm")
filde_jyusyo1 = "東京都品川区中延6-1-29"
filde_jyusyo1 = filde_jyusyo1 & vbCr & "電 話: 03ー3785ー4402" & " " & "FAX: 03ー3785ー4401"
filde_jyusyo1 = filde_jyusyo1 & vbCr & "E-mail: info@sanei-rint.co.jp"
filde_jyusyo1 = filde_jyusyo1 & vbCr & "http:www.sanei-print.co.jp"
myTextFrame.Contents = filde_jyusyo1 'テキストフレームに住所データを書き込む
font_P = 18
myTextFrame.ParentStory.PointSize = CStr(font_P)
MsgBox "フレーム オーバフロー"
Font_OVF = 0
Do While Font_OVF = 0
If myTextFrame.Overflows Then
font_P = font_P - 0.5
myTextFrame.ParentStory.PointSize = CStr(font_P)
Else
Font_OVF = 1
End If
Loop
MsgBox "フィットします"
myTextFrame.Fit (idFitOptions.idFrameToContent) 'idFrameToContent = 1718906723 (&H66746F63)
haichi_1_zahyo = myTextFrame.GeometricBounds
MsgBox "YS =" & haichi_1_zahyo(0) & " XS =" & haichi_1_zahyo(1) & " YE =" & haichi_1_zahyo(2) & " XE =" & haichi_1_zahyo(3)
L_cunt = myTextFrame.ParentStory.Paragraphs.Count
G_cunt = myTextFrame.Lines.Count
MsgBox L_cunt
MsgBox G_cunt
tyoutai_gyo = 1
If L_cunt < G_cunt Then
For aaaaaa = 1 To L_cunt Step 1
'tyoutai_gyo = tyoutai_gyo + 1
Set myStartCharacter = myTextFrame.ParentStory.Paragraphs.Item(tyoutai_gyo)
Set myEndCharacter = myTextFrame.ParentStory.Paragraphs.Item(tyoutai_gyo)
lincounA = myTextFrame.Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1).Lines.Count
If lincounA > 1 Then
Font_OVF = 0
tyoutai = 100
Do While Font_OVF = 0
tyoutai = tyoutai - 1
myTextFrame.Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1).HorizontalScale = tyoutai
lincounA = myTextFrame.Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1).Lines.Count
If lincounA = 1 Then
Font_OVF = 1
Else
If tyoutai <= 50 Then
Font_OVF = 1
End If
End If
Loop
End If
tyoutai_gyo = tyoutai_gyo + 1
Next
End If
MsgBox "フィットします"
myTextFrame.Fit (idFitOptions.idFrameToContent) 'idFrameToContent = 1718906723 (&H66746F63)
MsgBox "YS =" & haichi_1_zahyo(0) & " XS =" & haichi_1_zahyo(1) & " YE =" & haichi_1_zahyo(2) & " XE =" & haichi_1_zahyo(3)
'idApplyFrameFittingOptions = 1634100847 (&H6166666F)
'idCenterContent = 1667591779 (&H63656E63)
'idContentToFrame = 1668575078 (&H63746F66)
'idFillProportionally = 1718185072 (&H66696C70)
'idFrameToContent = 1718906723 (&H66746F63)
'idProportionally = 1668247152 (&H636F6E70)
End Sub