dポイントプレゼントキャンペーン実施中!

VBScriptでhtmlファイルの括弧( )を色付けしようとしています。

vbsファイルにhtmlファイルをドラッグ&ドロップすると、括弧も含めて丸括弧内の文字色を変えるスクリプトを作ろうとしています。
対象となるhtmlファイルは、使っている丸括弧は、全て全角で必ず対になっています。本文以外のコメント等で丸括弧は使っていません。
括弧の色付けはタグを挿入するようにしていますが、括弧内の括弧では違う色にしたいと思っています。

自分なりにスクリプトを書いてみましたが、InStrの引数の型が違うと怒られ、進めなくなってしまいました。何が悪いか分からなくなってしまいました。

作成したコードの主要部分を載せます。
Option Explicit
Dim m_objFso
Dim continue'置換ループを抜ける変数
Set m_objFso = CreateObject("Scripting.FileSystemObject")
'---------------------------------------------------------
Dim l_strItem
For Each l_strItem In WScript.Arguments
Call coloring( l_strItem )
Next
Set m_objFso = Nothing
'---------------------------------------------------------
Private Sub coloring( l_strItem )
Dim objFile
Dim strValue'置換対象文字列
Dim lngStart'検索位置
Dim depth'括弧の深さ
lngStart = 1
depth = 0
Set objFile = m_objFso.OpenTextFile( l_strItem )
strValue = objFile.ReadAll
objFile.Close
Do
Call replace( lngStart , strValue , depth )
If continue = 0 Then
Exit Do
End If
Loop While 1
Set objFile = m_objFso.OpenTextFile( l_strItem , 2 )
objFile.Write( strValue )
objFile.Close
Set objFile = Nothing
End Sub
'---------------------------------------------------------
Private Function replace( lngStart , strValue , depth )
Dim leftPosition '右括弧の位置
Dim rightPosition '左括弧の位置
leftPosition = InStr( lngStart , strValue , "(")
rightPosition = InStr( lngStart , strValue , ")")
If rightPosition = 0 Then
continue = 0
Exit Function
ElseIf leftPosition = 0 Then
depth = depth - 1
strValue = Replace( strValue , ")", ")</span>")
lngStart = rightPosition + Len("</span>") + 1
ElseIf leftPosition < rightPosition Then
depth = depth + 1
If depth = 1 Then
strValue = Left( strValue , leftPosition - 1) & "<span style=""color:Aquamarine"">" & mid( strValue , leftPosition )
lngStart = leftPosition + Len("<span style=""color:Aquamarine"">") + 1
Else
strValue = Left( strValue , leftPosition - 1) & "<span style=""color:GreenYellow"">" & mid( strValue , leftPosition )
lngStart = leftPosition + Len("<span style=""color:GreenYellow"">") + 1
End If
Else
depth = depth - 1
strValue = Replace( strValue , ")", ")</span>")
lngStart = rightPosition + Len("</span>") + 1
End If
continue = leftPosition Or rightPosition
End Function

また、もっといい方法があれば教えてください。

A 回答 (1件)

このスクリプトの仕様が正しいかを確認してない。



とりあえず原因だけ言うと。

*VB系は関数や変数の大文字小文字の差を同一視する。
*標準関数にReplaceが存在し、それを用いている。
*ユーザ側で作った関数の名前も何とReplace
*プログラマが標準関数のReplaceを呼ぶつもりで書いているが、このスクリプトは、ユーザーが定義した関数replaceだと思って引数を渡しているため、lngStartがそのような内容になる。

#個人的には

If rightPosition = 0 Then
Msgbox("3:" & lngStart)
continue = 0
Exit Function
ElseIf leftPosition = 0 Then
End If



If rightPosition = 0 Then
Msgbox("3:" & lngStart)
continue = 0
Exit Function
End If

If leftPosition = 0 Then
End If

としてネストを深くしないようにしたいところか
「VBSでエラー原因が分かりません」の回答画像1

この回答への補足

Msgbox("3:" & lngStart)はhimajin100000さんが動作確認用に追加したところですよね。
ElseIfはネストが深くなるんですか?

補足日時:2012/02/26 10:48
    • good
    • 0
この回答へのお礼

関数名を変えたらうまくいきました。

標準関数を呼んでいたなんて、思ってもいませんでした。でも、よく考えればreplaceなんてそのままの言葉だし、そうなりますね。

これからは自作関数作るときはオリジナリティあふれる名前にします。

ありがとうございました。

お礼日時:2012/02/26 10:54

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!