Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[vbscript] CreateObject() servers name
#8
Hello,

I have made a little code for that I want.

Antother question :
- It is an example to accept the cookies ?

Regards,
Bernard.

Code:

' Test de AntView ocx with VBScript.

' Defaults :
' 1 - Can not use the default webview2 folder ( CreateWebView() failed ).
' 2 - Error "violation memory" in line :
'    Set EdgeWebBrowser = Wscript.CreateObject( "AntViewAx.Antview", "EdgeWebBrowser_" )
'    but works.

' Using 2023-01-16 :
' - Windows 10 up to date.
' - AntWiew ocx last version.

option explicit

Const ct_cUrl = "https://www.cabinet-louis-reynaud.eu/index.php/clr-labs-3/" ' URL to use.

Public pu_cHtml : pu_cHtml = bv_DirBase() & "\_Result_html.txt"  ' TXT source file created.
Public pu_cPdf  : pu_cPdf  = bv_DirBase() & "\_Result_pdf.pdf"    ' PDF file created.

Public pu_nEvent ' Indicate when the event function is finish.
                ' 0 = In work.
                ' 1 = Finshed ok.
                ' 2 = Finished fail.

Dim Document          ' AntViewAx.AntViewDocument object.
Dim EdgeWebBrowser    ' AntViewAx.Antview object.
Dim i                  ' Count variable.

' Erase the old files.
bv_fErase( pu_cPdf  )
bv_fErase( pu_cHtml )

' Create a directory for the user datas, not works with the default folder.
bv_Makedir( bv_DirBase() & "\vb_UserDatas" )

' Error Violation memory, no exception.
On Error Resume Next
  Set EdgeWebBrowser = Wscript.CreateObject( "AntViewAx.Antview", "EdgeWebBrowser_" )
  If Err.Number <> 0 Then
      MsgBox( "Error = " & Err.Number & " Description = "  & Err.Description )
      Err.Clear
  End If
On Error GoTo 0

' CreateWebView() failed if use defaut directory.
EdgeWebBrowser.UserDataFolder = bv_DirBase() & "\vb_UserDatas"

EdgeWebBrowser.CreateWebView

For i = 1 to 60 Step 1
  if EdgeWebBrowser.WebViewCreated Then Exit For
  wscript.sleep 1000
Next

If not EdgeWebBrowser.WebViewCreated Then
  Set EdgeWebBrowser = Nothing
  MsgBox "WebViewCreated() fail !"
  wscript.Quit
End If

Set Document = Wscript.CreateObject( "AntViewAx.AntViewDocument", "Document_" )
Document.BrowserDispatch( EdgeWebBrowser.IDispatchPointer )

' Open the URL.
pu_nEvent = 0
EdgeWebBrowser.Navigate( ct_cUrl )
For i = 1 to 60 Step 1
  If pu_nEvent <> 0 Then Exit For
  wscript.sleep 1000
Next
If pu_nEvent <> 1 Then
  MsgBox "Navigate() fail !"
  EdgeWebBrowser.CloseBrowserProcess
  EdgeWebBrowser.CloseWebView
  Set EdgeWebBrowser = Nothing
  Set Document      = Nothing
  wscript.quit
End If

' Print to PDF.
pu_nEvent = 0
EdgeWebBrowser.PrintToPdf pu_cPdf, ""
For i = 1 to 60 Step 1
  If pu_nEvent <> 0 Then Exit For
  wscript.sleep 1000
Next

' Get HTML source.
pu_nEvent = 0
Document.RequestCurrentHtml()
For i = 1 to 60 Step 1
  If pu_nEvent <> 0 Then Exit For
  wscript.sleep 1000
Next

EdgeWebBrowser.CloseBrowserProcess
EdgeWebBrowser.CloseWebView
Set EdgeWebBrowser = Nothing
Set Document      = Nothing

MsgBox "End of script, look the created files."

' ***** Event functions *****

Sub Document_OnRequestCurrentHtml( cHtml )
  pu_nEvent = 1
  bv_MemoWrit pu_cHtml, cHtml
End Sub

Sub EdgeWebBrowser_OnNavigationCompleted( IsSuccess, WebErrorStatus, NavigationId )
  If IsSuccess Then
      pu_nEvent = 1
  Else
      pu_nEvent = 2
  End If
End Sub

Sub EdgeWebBrowser_OnPrintToPdfCompleted( HResult, IsSuccessful )
  If IsSuccessful Then
      pu_nEvent = 1
  Else
      pu_nEvent = 2
      MsgBox "OnPrintToPdfCompleted() fail, HResult = ] " & HResult
  End If
End Sub

' ***** Used tools *****

' Get the default directory.
Function bv_DirBase()
  bv_DirBase = Left( WScript.ScriptFullName, InStrRev( WScript.ScriptFullName, "\" ) )
End Function

' Erase a file.
Sub bv_fErase( cFile_bv )
  Dim oFso_bv
  Set oFso_bv = CreateObject( "Scripting.FileSystemObject" )
  If oFso_bv.FileExists( cFile_bv ) Then
      oFso_bv.DeleteFile cFile_bv
  End If
  Set oFso_bv = Nothing
End Sub

' Create a directory.
Sub bv_Makedir( cDir_bv )
  Dim oFso_bv
  Set oFso_bv = CreateObject( "Scripting.FileSystemObject" )
  If Not oFso_bv.FolderExists( cDir_bv ) Then
      oFso_bv.CreateFolder( cDir_bv )
  End If
  Set oFso_bv = Nothing
End Sub

' Write a string in a file.
Sub bv_MemoWrit( cFile_bv, cString_bv )
  Dim oFile_bv
  Dim oFso_bv
  Set oFso_bv = CreateObject( "Scripting.FileSystemObject" )
  If oFso_bv.FileExists( cFile_bv ) Then
      oFso_bv.DeleteFile cFile_bv
  End If
  Set oFile_bv = oFso_bv.OpenTextFile( cFile_bv, vbString, True )
  oFile_bv.Write cString_bv
  oFile_bv.Close
  Set oFso_bv = Nothing
End Sub
Reply


Messages In This Thread
RE: [vbscript] CreateObject() servers name - by Bernard Mouille - 2023-01-16, 13:00:20

Forum Jump:


Users browsing this thread: 1 Guest(s)