2016-07-04 24 views
0

Excel VBAでより大きなプロジェクトを作成していて、次の問題が発生しました。私はVBAを使ってExcelをMS Wordでテンプレートをコンパイルするようにしています。私は、黄色のテンプレートで特定のフレーズを強調したいと思っていました。それはある時点で働いていましたが、明らかに理由がなくなってから停止しました。私が今コードを実行すると、パーツはエラーなく実行されますが、テンプレート内にあると確信していても、その単語は強調表示されません。 debug.printを使用して.replacement.highlight値の値を調べ、イミディエイトウィンドウに9999999、ブレークモードで式にマウスカーソルを合わせると-1が表示されます。いずれかで何が起こっているか知っていMS WordでVBAの.replacement.highlight = trueが有効にならない

Dim WdApp As Word.Application 
Set WdApp = New Word.Application 
With WdApp 
    .ActiveDocument.Select 
    With .Selection.Find 
    'the code creating the document and writing the template continues here 
     .ClearFormatting 
     .Text = "{Optional: Please also confirm the terms of transactions and other key information (for example: rights of return, allowances and rebates, special agreements, payment terms, incoterms, etc.) which may affect the accounting for the transactions.}" 
     .Replacement.Highlight = True 
     .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 

     .ClearFormatting 
     .Text = PlaceholderAdditionalInfoRequest 
     .Replacement.Text = .Text 
     .Replacement.Highlight = True 
     .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 

     .ClearFormatting 
     .Text = "<<<@@@Client's [email protected]@@>>>" 
     .Replacement.Text = .Text 
     .Replacement.Highlight = True 
     .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 

     .ClearFormatting 
     .Text = "{Optional: A statement of account with the above invoices marked is attached. ALTERNATIVELY: Copies of the above invoices are attached.}" 
     .Replacement.Highlight = True 
     .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 
    End With 
End With 

は、コードの関連フラグメントを下に引用されていますか?私は、特に単純な英語で表現されていれば、どんな指摘にも感謝しています:)私はVBAには新しく、プログラミングで正式な教育を受けていません。

敬具、

新米

+0

私はあなたのコードを試していませんが、.Replacement.Highlightを設定する前に ".Replacement.ClearFormatting"と.ClearFormattingを試してみましたか? (ちょっとしたことがあってはならない時に何かが「こだわる」) –

答えて

0

これを試してみてください:

Options.DefaultHighlightColorIndex = wdYellow 

前:

With .Selection.Find 

あなたがいないテンプレートにそれにFind機能を適用しているようですテンプレートの作成前にアクティブ化されたドキュメント。これらの行の前に

the code creating the document and writing the template continues here…

: これらの行を移動してみてください

.ActiveDocument.Select With .Selection.Find

を作るWordのアプリケーションWdApp見える(WdApp.Visible = True)コードが何をしているかを確認し、それが閉じています確実にする(WdApp.Quit )を実行します。

+0

最初はうまくいくように思えましたが、それはやめました。システムを再起動すると(アプリケーションだけでなく)役立つようですが、問題はむしろ不安定です。特定のシナリオに固定するのは難しいと思っています。誰も似たような問題を経験していますか? – Tyro

+0

最後の変更を適用しましたか?どんな更新? – EEM

+0

こんにちは、まだです。私はノートパソコンをオフィスに置いていましたので、週末には試してもらえませんが、明日に試してみることにしましょう。それでも、私が.Replacement.Text = "abc"と入力すると適切なドキュメントに置き換えられるので、間違ったドキュメントに.find関数を適用しているとは思わない。私はまた、ユーザーがテンプレートを編集できるようにサブルーチンの最後に文書を終了しないことをお勧めします。私は、サブが終了したときにWdAppを何も設定しないと思う。 .Replacement.ClearFormattingを試して、その仕組みを見てみましょう。 – Tyro

関連する問題