본문 바로가기

자료

[VB.net/C#] Javascript 실행 (msscript.ocx)

728x90

[VB.net/C#] Javascript 실행 (msscript.ocx)


           Dim script As String
        script = Dir_OpenFile("파일.js")
        Compile(script)
   
   Private cScript As New MSScriptControl.ScriptControl

    Public Function Compile(ByVal NewScriptCode As String) As Boolean
        On Error GoTo OnErr

        With cScript
            .Language = "Javascript"
            .Reset()
        End With

        cScript.AddCode(NewScriptCode)

        Compile = True
        Exit Function
OnErr:
        Dim ErrorLine As Long
        With cScript.Error
            MessageBox.Show("스크립트 에러가 발생하였습니다." & vbNewLine &
                            .Description & " at line " & .Line & ".",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation)
            ErrorLine = .Line
            .Clear()
        End With
    End Function

    Function Dir_OpenFile(ByVal File As String)
        Dim fso, FileWriter As Object
        fso = CreateObject("scripting.FileSystemObject")
        FileWriter = fso.OpenTextFile(File)
        Dir_OpenFile = FileWriter.ReadAll
        FileWriter.Close()
    End Function
728x90