0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

行末に特定の文字列を追加するマクロ

Posted at
'-------------------------------------------------------------------------------
'コメントの追加
'
' CopyRight Naoshi Ide 2015-
'-------------------------------------------------------------------------------
Call Main

Sub Main()

	Dim buffer, texts

	SuffixStr = InputBox("行末に追加するコメントを記入してください", "memo")
	If SuffixStr = "" Then
		Exit Sub
	End If

	'選択したテキストを取得する
	buffer = Editor.GetSelectedString()

	texts = Split(buffer, vbCrLf)

	'行ごとに処理を行う
	For Each line in texts
		tmpBuffer = SetPrefix("//", line)
		tmpBuffer = SetBlank(tmpBuffer, 78)
		tmpBuffer = SetSuffix(tmpBuffer, SuffixStr)
		output = output + tmpBuffer + vbCrLf
	Next

	Editor.InsText(output)
End Sub


Function GetStrWidth(str)
'	//半角文字を削除する
	Dim re
	Set re = new RegExp
	re.Pattern = "[\u0001-\u00FF]"
	re.Global = True
	delHalfWidth = re.Replace(str, "")
	strWidth = Len(delHalfWidth) * 2 + Len(str) - Len(delHalfWidth)
	GetStrWidth = strWidth
End Function

'末尾が指定桁数になるように空白文字を追加する
Function SetBlank(str, length)
	strLen = GetStrWidth(str)
	ln = length - strLen
	tmpBuffer = str
	For Cnt = 0 To ln
		tmpBuffer = tmpBuffer + " "
	Next
	SetBlank = tmpBuffer
End Function

'行頭にコメントを追加する
Function SetPrefix(PrefixStr, str)
	SetPrefix = PrefixStr + str
End Function

'末尾に文字列を追加する
Function SetSuffix(line, SuffixStr)
	SetSuffix = line + SuffixStr
End Function
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?