はこふぐのメモ

めもです

自分へのメモ BluePrism Wordで文字列を検索してその位置を得る&その行を取得する

明日の出社した自分へのメモです。

 

なんだかVBAの勉強になってきた。

VBAわからない・・・

 

WordのVBOにはFind Textアクションがあるが、戻り値は「found」のみ。

つまり、検索文字列があったかどうかのフラグのみ。

検索文字列が何ページめの何行目にあったか取得するアクションを自作してみた。

 

★入力パラメータはFind Textそのまま

f:id:sazanamifugu:20191107231521p:plain

★出力パラメータにpageとlineを追加

f:id:sazanamifugu:20191107231624p:plain

 

★コードステージ

Dim d As Object = GetDocument(handle,document_name)
Dim w As Object = d.Application
Dim s As Object = w.Selection
Dim f As Object = s.Find


Try

f.ClearFormatting
With f
	.Text = Text
	.Replacement.Text = ""
	.Forward = True
	.Wrap = 1 'wdFindContinue
	.Format = False
	.MatchCase = match_case
	.MatchWholeWord = match_whole_word
	.MatchWildcards = False
	.MatchSoundsLike = False
	.MatchAllWordForms = False
End With
found = f.Execute

page = s.Information(1)'wdActiveEndAdjustedPageNumber
line = s.Information(10) 'wdFirstCharacterLineNumber

Catch ex As Exception

found = False
page = ""
line = ""

Finally

d = Nothing
w = Nothing
s = Nothing
f = Nothing

End try



Informationに与えている定数はこちらを参照させていただいた。

Informaitonプロパティの定数一覧 :ワードマクロ・Word VBAの使い方


定数定義の文字列はそのまま使えない?
wdFirstCharacterLineNumberが宣言されていないと怒られる。
定数の数字をそのまま設定するとうまくいった。

次は、このpageとlineを使ってその行すべての文字列を得られるか検討する。

 

→追記

lineText = s.Paragraphs(1).Range.Text

で検索した文字列の含まれる行(行というかParagraph)を取得できた。

プライバシーポリシー
©2019 はこふぐのメモ All rights reserved