How to open ie.exe within an If/Else statement - vbscript

I am trying to write a piece of code so when the user clicks "yes" on my message box my yes.gif opens in IE, but if the user clicks "no" I want my no.gif to open in IE. I get a synax error on line 5 (Else statement).
My code:
Result = MsgBox("Text", 20, "Title")
If Result = vbYes Then Set objExplorer = CreateObject("InternetExplorer.Application")
Else Result = vbNo Then Set objExplorer1 = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.Visible = 1
.Document.Title = "Right Decision"
.Toolbar = False
.Statusbar = False
.Top = 500
.Left = 500
.Height = 400
.Width = 600
.Document.Body.InnerHTML = "<img src='C:\User\yes.gif'>"
End With
With objExplorer1
.Navigate "about:blank"
.Visible = 1
.Document.Title = "Wrong Decision"
.Toolbar = False
.Statusbar = False
.Top = 500
.Left = 500
.Height = 400
.Width = 600
.Document.Body.InnerHTML = "<img src='C:\User\no.gif'>"
End With

There are multiple issues with your code:
The only possible values from the MsgBox are vbYes and vbNo because you launched it with the vbYesNo flag. Since the result is binary there is no need for multiple comparisons (which don't work like that in VBScript anyway).
Your If statement uses the single-line If..Then form, meaning that the subsequent Else is invalid. And even if it weren't invalid the syntax would still be incorrect.
Starting different IE instances is pointless when a string and an image name are the only differences.
Your code tries to configure both instances, but one of them will be invalid regardless of the user's choice.
Use an If..Then..Else to define the settings that actually differ, then create the IE instance after the conditional and configure it accordingly.
Result = MsgBox("Text", vbYesNo + vbCritical, "Title")
If Result = vbyes Then
title = "Right Decision"
picture = "C:\User\yes.gif"
Else
title = "Wrong Decision"
picture = "C:\User\no.gif"
End If
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Navigate "about:blank"
.Visible = True
.Document.Title = title
.Toolbar = False
.Statusbar = False
.Top = 500
.Left = 500
.Height = 400
.Width = 600
.Document.Body.InnerHtml = "<img src='" & picture & "'>"
End With

Related

VBS password protected image

I'm not a programmer and i need help.
How to combine this two scripts into one?
Then i will convert this to EXE and give as present on Tank Style pendrive :).
pass=inputbox("Password?")
if pass="fish" then msgbox("Correct Password!") else msgbox("Incorrect Password!")
AND
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer "
.Navigate "about:blank"
.ToolBar = 0
.StatusBar = 0
.Left = 200
.Top = 200
.Width = 650
.Height = 440
.Visible = 1
.Document.Title = "Kocham cie Maciek!"
.Document.Body.InnerHTML = _
"<center>Kocham cie Maciek <3<br><br><img src='http://www.crystalclearsports.net/file/2016/07/use_love_quotes_for_him_and_inspire_romantic_vitality.jpg' height=336 width=600></center>"
"
End With
When someone type good password it show Picture1, if bad Picture2.
Try this!
Dim MyPassword, objExplorer
MyPassword = InputBox("Enter the Password and Press 'OK' ", "Password")
MyPassword = Trim(MyPassword)
If MyPassword = "" Then
Msgbox "No Password is entered"
Else
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.ToolBar = 0
.StatusBar = 0
.Left = 200
.Top = 200
.Width = 650
.Height = 440
.Visible = 1
.Document.Title = "Kocham cie Maciek!"
End With
If StrComp(MyPassword, "FISH", 1) = 0 Then
' Correct Password
Msgbox "The Password is Correct"
objExplorer.Document.Body.InnerHTML = "<center>Kocham cie Maciek <3<br><br><img src='http://www.crystalclearsports.net/file/2016/07/use_love_quotes_for_him_and_inspire_romantic_vitality.jpg' height=336 width=600></center>'"
Else
Msgbox "Incorrect Password"
objExplorer.Document.Body.InnerHTML = "<center>Kocham cie Maciek <3<br><br><img src='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' height=336 width=600></center>'"
End If
End if
you missed the second image, update the XXXX with the location of second image you would want to show!!

visual basic 6: create an object in other form

i would like create a rectangle in a different picture.
For example: the code is inside testpicture, but i would like that this rect is create in testpicture2.
How is possible please ?
Thank you very much
The solution...
Private Sub GenerateRuntimeForm()
Dim ctrl As Control
Set frm = New Form1
Set cmdOK = Nothing
Set cmdCancel = Nothing
Set txtInput = Nothing
Set lblDisplay = Nothing
'clear the form
For Each ctrl In frm
ctrl.Visible = False
Next
Set cmdOK = frm.Controls.Add("VB.CommandButton", "cmdOK")
Set cmdCancel = frm.Controls.Add("VB.CommandButton", "cmdCancel")
Set txtInput = frm.Controls.Add("VB.TextBox", "txtInput")
Set lblDisplay = frm.Controls.Add("VB.Label", "lblDisplay")
With frm
'Set form's width and height
.Width = 4000
.Height = 2500
.Caption = "Run-Time Generated Form"
End With
'Setup rest of controls
With lblDisplay
.Top = 250
.Left = 250
.AutoSize = True
.FontBold = True
.Caption = IIf(allowNumericOnly, "Enter Your Age:", "Enter Your Last Name:")
End With
With txtInput
.Top = lblDisplay.Top + lblDisplay.Height + 250
.Left = 250
.Height = 295
If allowNumericOnly Then
.Width = 500
.MaxLength = 3
Else
.Width = 2500
End If
End With
With cmdOK
.Top = txtInput.Top + txtInput.Height + 375
.Width = 800
.Left = 1000
.Height = cmdGenerate(0).Height
cmdOK.Caption = "&OK"
.Enabled = False
End With
With cmdCancel
.Left = cmdOK.Left + cmdOK.Width + 100
.Top = cmdOK.Top
.Width = cmdOK.Width
.Height = cmdOK.Height
.Caption = "&Cancel"
End With
cmdOK.Visible = True
cmdCancel.Visible = True
lblDisplay.Visible = True
txtInput.Visible = True
frm.Show vbModal
End Sub

Please modify PPT Macro

I have below macro.
Could you please modify it in such ways that it will show slide number on the top and also extract notes page.
I tried all ways but couldn't get answer-:
Sub WriteToWord()
Dim aSlide As Slide, MyDoc As New Word.Document, MyRange As Word.Range
Dim aTable As Table, aShape As Shape, TablesCount As Integer, ShapesCount As Integer
Dim i As Word.Paragraph
On Error Resume Next
With MyDoc
.Application.Visible = False
.Application.ScreenUpdating = False
For Each aSlide In ActivePresentation.Slides
For Each aShape In aSlide.Shapes
Set MyRange = .Range(.Content.End - 1, .Content.End - 1)
Select Case aShape.Type
Case msoAutoShape, msoPlaceholder, msoTextBox
If aShape.TextFrame.HasText Then
aShape.TextFrame.TextRange.Copy
MyRange.Paste
With MyRange
.ParagraphFormat.Alignment = wdAlignParagraphLeft
For Each i In MyRange.Paragraphs
If i.Range.Font.Size >= 16 Then
i.Range.Font.Size = 14
Else
i.Range.Font.Size = 12
End If
Next
End With
End If
Case msoPicture
aShape.Copy
MyRange.PasteSpecial DataType:=wdPasteMetafilePicture
ShapesCount = .Shapes.Count
With .Shapes(ShapesCount)
.LockAspectRatio = msoFalse
.Width = Word.CentimetersToPoints(14)
.Height = Word.CentimetersToPoints(6)
.Left = wdShapeCenter
.ConvertToInlineShape
End With
.Content.InsertAfter Chr(13)
Case msoEmbeddedOLEObject, msoLinkedOLEObject, msoLinkedPicture, msoOLEControlObject
aShape.Copy
MyRange.PasteSpecial DataType:=wdPasteOLEObject
ShapesCount = .Shapes.Count
With .Shapes(ShapesCount)
.LockAspectRatio = msoFalse
.Width = Word.CentimetersToPoints(14)
.Height = Word.CentimetersToPoints(6)
.Left = wdShapeCenter
.ConvertToInlineShape
End With
.Content.InsertAfter Chr(13)
Case msoTable
aShape.Copy
MyRange.Paste
TablesCount = .Tables.Count
With .Tables(TablesCount)
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Range.Font.Size = 11
End With
.Content.InsertAfter Chr(13)
End Select
Next
If aSlide.SlideIndex < ActivePresentation.Slides.Count Then .Content.InsertAfter Chr(12)
.UndoClear ' Clear used memory
Next
' Change white font to black color
With .Content.Find
.ClearFormatting
.Format = True
.Font.Color = wdColorWhite
.Replacement.Font.Color = wdColorAutomatic
.Execute Replace:=wdReplaceAll
End With
MsgBox "PPT Converted to WORD completed, Please check and save document", vbInformation + vbOKOnly, "ExcelHome/ShouRou"
.Application.Visible = True
.Application.ScreenUpdating = True
End With
End Sub
Sub Auto_Open() ' Add PPTtoWord to Tool Bar when Powerpoint start
Dim MyControl As CommandBarControl
On Error Resume Next
Application.CommandBars("Standard").Controls("PPTtoWord").Delete
Set MyControl = Application.CommandBars("Standard").Controls.Add(Before:=1)
With MyControl
.Caption = "PPTtoWord"
.FaceId = 567 ' Word Icon
.Enabled = True
.Visible = True
.Width = 100
.OnAction = "WriteToWord"
.Style = msoButtonIconAndCaption
End With
End Sub
Sub Auto_Close() ' Delete PPTtoWord from Tool Bar when Powerpoint close
On Error Resume Next
Application.CommandBars("Standard").Controls("PPTtoWord").Delete
End Sub
You are running this from Word and automating PowerPoint using early binding, you need to fully qualify any PowerPoint reference.
Have you added a reference to PowerPoint library.
Change to aShape As PowerPoint.Shape
Grab the reference to the running instance of PowerPoint. PowerPoint is single instance multi-use so you can use this.
Dim PPT as PowerPoint.Application
Set PPT = CreateObject("PowerPoint.Application")
Fully qualify all references to ActivePresentation with PPT.ActivePresentation
Your macro should run then and generate something so that you can continue debugging.

WShell.Run returns 143

I am calling java code from VB script using WShell.Run. It returns a code 143. What does it mean? Where can i get the list of error codes that run method can return?
Here is the reference to System Error Codes.
ERROR_SAME_DRIVE 143 (0x8F) The system cannot join or substitute a
drive to or for a directory on the same drive.
P.S. I think that the next notes are out of the question, but just in case...
Just to note that Err object has "dummy" Description (Unknown runtime error) for most codes. If you like to get filtered list with all sensible descriptions, you can do something like this:
With CreateObject("InternetExplorer.Application")
Const DUMMY = "Unknown runtime error"
ReDim aryLines(15999)
Dim cnt, i, w, h
cnt = -1
.Navigate "about:blank"
.Document.Title = "Error Codes " & String(100, Chr(1))
.ToolBar = False
.Resizable = True
.StatusBar = False
.Width = 420
.Height = 380
With .Document.ParentWindow.Screen
w = .AvailWidth
h = .AvailHeight
End With
.Left = (w - .Width ) \ 2
.Top = (h - .Height) \ 2
Do While .Busy : WScript.Sleep 200 : Loop
On Error Resume Next
With Err
For i = 1 To 15999
.Raise i
If .Description <> DUMMY Then
cnt = cnt + 1
aryLines(cnt) = AddZero(i) & .Description
End If
.Clear
Next
End With
On Error GoTo 0
ReDim Preserve aryLines(cnt)
.Document.Body.InnerHTML = "<pre id=x>" & Join(aryLines, vbNewLine)
.Document.Body.Style.overflow = "auto"
.Document.All.X.Style.fontFamily = "Verdana, sans-serif"
.Visible = True
End With
Function AddZero(nVar)
AddZero = "<b>" & Right("00000" & nVar, 5) & "</b> "
End Function
this code returned by your java application. From MSDN
The following VBScript code does the same thing, except it specifies the window type, waits for Notepad to be shut down by the user, and saves the error code returned from Notepad when it is shut down.
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)

Displaying an image in a VBScript MsgBox

How can I display an image in a MsgBox?
The answer is: This is not possible. MsgBox can only display strings. (Documentation)
An alternative is to display your image in a small Internet Explorer window. Here's an example:
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.ToolBar = 0
.StatusBar = 0
.Left = 100
.Top = 100
.Width = 200
.Height = 200
.Visible = 1
.Document.Title = "Important image!"
.Document.Body.InnerHTML = _
"<img src='http://sstatic.net/stackoverflow/img/venn-diagram.png' height=100 width=100>"
End With
This should display the Venn diagram found in Stack Overflow's about section.
What you want is called a HyperText Application, or HTA. You use HTML to create the form.

Resources