Skip to content

Instantly share code, notes, and snippets.

@yu-tang
Created November 14, 2011 08:42
Show Gist options
  • Save yu-tang/1363546 to your computer and use it in GitHub Desktop.
Save yu-tang/1363546 to your computer and use it in GitHub Desktop.
ScriptControl 経由で JScript から Win API を利用する
' クラスモジュール限定。Excel なら、とりあえず ThisWorkbook モジュールあたりで。
Option Explicit
Private Declare Function StrCmpLogicalW Lib "SHLWAPI.DLL" ( _
ByVal lpStr1 As Long, _
ByVal lpStr2 As Long) As Long
Public Function StrCmpLogical(str1 As String, str2 As String) As Long
StrCmpLogical = StrCmpLogicalW(StrPtr(str1), StrPtr(str2))
End Function
' 数字混じり文字列ソート
Sub Test()
Dim sc As Object ' MSScriptControl.ScriptControl
Dim code As String
Set sc = CreateObject("MSScriptControl.ScriptControl")
sc.Language = "JScript"
sc.AddObject "Me", Me, True
code = "['X10Y1', 'X10Y10', 'X10Y2', 'X1Y1', 'X1Y10', 'X1Y2', 'X2Y1', 'X2Y10', 'X2Y2']" & _
".sort(function(s1,s2){return StrCmpLogical(s1,s2)});"
Debug.Print Replace(sc.Eval(code), ",", vbCrLf)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment