I can't fix this code that gives Else without If:
Sub aRefreshData()
If Worksheets("Control tab").Range("$I$2").Value = "OFF" Then MsgBox "Enable Connection before refresh"
Else
ActiveWorkbook.Connections("server DB").Refresh
End If
End Sub
You missed ":" after Else
Sub aRefreshData()
If Worksheets("Control tab").Range("$I$2").Value = "OFF" Then
MsgBox "Enable Connection before refresh"
Else:
ActiveWorkbook.Connections("server DB").Refresh
End If
End Sub
Related
I created a script to check the status of a web search
I added a repeat statement so the script can continue only if the search is completed or if the "SN" is invalid.
repeat 20 times
set theSearchstate to "not ready"
set checkIfSNIsCorrect to ""
set checkIfSNIsInvalid to ""
try
tell application "Google Chrome"
tell front window's active tab to set checkIfSNIsInvalid to execute javascript "document.getElementsByClassName('modal-body ng-scope')[0].innerHTML;"
## Invalid Serial
tell front window's active tab to set checkIfSNIsCorrect to execute javascript "document.getElementsByClassName('subheader ng-binding')[0].innerHTML;"
## SN and device ID
end tell
if theSearchstate is equal to "not ready" then
delay 1
else if checkIfSNIsCorrect contains serialNumber then
set theSearchstate to "Completed"
set checkIfSNIsCorrect to "SN is Correct"
exit repeat
else if checkIfSNIsInvalid contains "Serial Does Not Exists" then
set theSearchstate to "invalid S/N"
exit repeat
else if checkIfSNIsInvalid contains "serviceErrorWhileSearching" then
set theSearchstate to "Error with GCRM"
exit repeat
end if
on error
--
end try
end repeat
return theSearchstate
However this is not working at all, I tried in different way, but I can't make it work.
Any suggestion ?
The variable "serialNumber" is NOT defined:
else if checkIfSNIsCorrect contains serialNumber then
This statement will always return true, so none of the other statements will be executed:
if theSearchstate is equal to "not ready" then
I Revised Your Script
Please test and advise if it works for you.
Be sure to set the serialNumber variable to the correct value.
--- MOVE VARIABLE INIT OUTSIDE OF REPEAT ---
set theSearchstate to "not ready"
set checkIfSNIsCorrect to ""
set checkIfSNIsInvalid to ""
--- ADD THIS ---
set serialNumber to "YourSerialNumber" ## CHANGE to desired value
try
repeat 20 times
tell application "Google Chrome"
tell front window's active tab to set checkIfSNIsInvalid to ¬
execute javascript "document.getElementsByClassName('modal-body ng-scope')[0].innerHTML;"
## Invalid Serial
tell front window's active tab to set checkIfSNIsCorrect to ¬
execute javascript "document.getElementsByClassName('subheader ng-binding')[0].innerHTML;"
## SN and device ID
end tell
### REMOVED if test for "theSearchstate"
if checkIfSNIsCorrect contains serialNumber then
set theSearchstate to "Completed"
set checkIfSNIsCorrect to "SN is Correct"
exit repeat
else if checkIfSNIsInvalid contains "Serial Does Not Exists" then
set theSearchstate to "invalid S/N"
exit repeat
else if checkIfSNIsInvalid contains "serviceErrorWhileSearching" then
set theSearchstate to "Error with GCRM"
exit repeat
end if
--- MOVE DELAY TO HERE ---
--- NONE OF THE ABOVE MATCHED, SO DELAY & TRY AGAIN ---
delay 1
end repeat
on error errMsg number errNum
set msgStr to "[ERROR]" & linefeed & errNum & ": " & errMsg
set titleStr to "Check for Serial Number"
display dialog msgStr ¬
with title titleStr ¬
with icon stop
set buttonStr to button returned of result
set theSearchstate to msgStr
end try
return theSearchstate
I'm adding new record with entity framework and I need this when I have multiple inserting records and many users on diferent machines at same time (concurrency). So I'm using Catch to restart SaveChanges() process.
In example bellow you can see what I'm triyng to do:
Public Sub AdicionaAux(ByRef Attempts As Integer)
Dim inBlnRepete As Boolean = False
Try
Using ctx As New BD.Dinamica.Aplicacao
Using trans As DbContextTransaction = ctx.Database.BeginTransaction(IsolationLevel.RepeatableRead)
Try
Dim t As tbArtigos = ctx.tbArtigos.FirstOrDefault
ctx.tbArtigos.Attach(t)
ctx.Entry(t).State = EntityState.Added
ctx.SaveChanges() 'FAILS AND GO TO CATCH
Catch ex As Exception
trans.Rollback()
Throw
End Try
End Using
End Using
Catch
inBlnRepete = True 'RepeteFuncaoPorConcorrencia(ex, Tentativas)
If inBlnRepete And Attempts < 10 Then
AdicionaAux(Attempts) 'RESTART PROCESS
Else
Throw
End If
End Try
End Sub
When I'm on 6th time on catch my application crashs and IIS stops.
If I'm on Visual Studio debbuging, debugger stops.
I don't understand why this happens? Why my app crashes and IIS stops?
It's EF6 Configuration? IIS configuration? SQL configuration?
In example bellow Using sql I have no problem:
Public Sub AdicionaAux(ByRef Attempts As Integer)
Try
Dim appServidorSQL As String = ConfigurationManager.AppSettings("ServidorSQL")
Dim appInstanciaSQL As String = ConfigurationManager.AppSettings("InstanciaSQL")
Dim appBDEmpresa As String = ConfigurationManager.AppSettings("BDEmpresa")
Using connection As New SqlConnection("Server=" & appServidorSQL & "\" & appInstanciaSQL & ";User ID=sa;Initial Catalog=" & appBDEmpresa & ";")
connection.Open()
Dim command As SqlCommand = connection.CreateCommand()
Dim transaction As SqlTransaction
transaction = connection.BeginTransaction("SampleTransaction")
command.Connection = connection
command.Transaction = transaction
Try
command.CommandText = _
"Insert into tbDestinos (Codigo, Descricao, Ativo, Sistema, DataCriacao, UtilizadorCriacao) VALUES ('POR', 'Description',1,1,getdate(),'xxx')"
command.ExecuteNonQuery()
transaction.Commit()
Catch ex As Exception
transaction.Rollback()
Throw
End Try
End Using
Catch
AdicionaAux(Attempts)
End Try
End Sub
Any help?
I am looking to populate a list box when a check box is ticked and empty it when the tick is removed.
This code worked for this function in my previous modules but now I am getting an error (im guessing it is with the arguments for Range), and I would like to understand why. Additionally, the list box remains as it is when the checkbox is unticked.
Here is my code:
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
ListBox1.List = Sheets("DATA").Range("C22").Value
Else
ListBox1.ListFillRange = ""
End If
End Sub
Try this:
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
Me.ListBox1.AddItem Sheets("DATA").Range("C22").Value
Else
Me.ListBox1.Clear
End If
End Sub
Changing your code to this:
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
ListBox1.ListFillRange = "C22:C24"
Else
ListBox1.ListFillRange = ""
End If
End Sub
will change the listbox to show the contents of cells C22:C24.
So, I have a for each that goes through all of our pages' Regex.Matches for manipulation... The problem is that I can't seem to correctly create a regex for certain strings. The goal is essentially to grab:
[ControlName].Text = "Static ""Text"" Here!"
As shown below, I'm able to do so... However, I'm getting issue when it comes to concatenated strings, I'd like to stop matching after the final closing quotation mark before concatenation. (see Goals below)
Current Regex: \w+\.Text = (".*?"+?.*"?)
Currently Hits:
lblError.Text = "Error on: ""Navigation Admin"" page."
lblError.Text = "Error on:" & "Navigation Admin"
lblError.Text = "Error on:" & ""Navigation Admin"" page."
Goal:
lblError.Text = "Error on: ""Navigation Admin"" page."
lblError.Text = "Error on:" & "Navigation Admin"
lblError.Text = "Error on:" & ""Navigation Admin"" page."
I'm quite possibly underthinking this, overthinking this and/or horrible at regexes in general. Any advice/tips would be appreciated!
here is the way to match a quoted string including escaped quotes (2 consecutive quotes):
\w+\.Text = ("[^"]*(?:""[^"]*)*")
I need a macro in Outlook that extract all the email address in the outlook message then post it in excel.
The following code only extracts the very 1st email address it finds in the body.
My desired output should be:
adam.peters#sample.com
adam.dryburgh#sample.com
amy.norton#sample.com
My sample email is:
Delivery has failed to these recipients or groups:
adam.peters#sample.com The e-mail address you entered couldn't be
found. Please check the recipient's e-mail address and try to resend
the message. If the problem continues, please contact your helpdesk.
adam.dryburgh#sample.com The e-mail address you entered couldn't be
found. Please check the recipient's e-mail address and try to resend
the message. If the problem continues, please contact your helpdesk.
amy.norton#sample.com The e-mail address you entered couldn't be
found. Please check the recipient's e-mail address and try to resend
the message. If the problem continues, please contact your helpdesk.
The following organization rejected your message:
mx2.dlapiper.iphmx.com.
code:
Sub Extract_Invalid_To_Excel()
Dim olApp As Outlook.Application
Dim olExp As Outlook.Explorer
Dim olFolder As Outlook.MAPIFolder
Dim obj As Object
Dim stremBody As String
Dim stremSubject As String
Dim i As Long
Dim x As Long
Dim count As Long
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
Dim xlApp As Object 'Excel.Application
Dim xlwkbk As Object 'Excel.Workbook
Dim xlwksht As Object 'Excel.Worksheet
Dim xlRng As Object 'Excel.Range
Set olApp = Outlook.Application
Set olExp = olApp.ActiveExplorer
Set olFolder = olExp.CurrentFolder
'Open Excel
Set xlApp = GetExcelApp
xlApp.Visible = True
If xlApp Is Nothing Then GoTo ExitProc
Set xlwkbk = xlApp.workbooks.Add
Set xlwksht = xlwkbk.Sheets(1)
Set xlRng = xlwksht.Range("A1")
xlRng.Value = "Bounced email addresses"
'Set count of email objects
count = olFolder.Items.count
'counter for excel sheet
i = 0
'counter for emails
x = 1
For Each obj In olFolder.Items
xlApp.StatusBar = x & " of " & count & " emails completed"
stremBody = obj.Body
stremSubject = obj.Subject
'Check for keywords in email before extracting address
If checkEmail(stremBody) = True Then
'MsgBox ("finding email: " & stremBody)
RegEx.Pattern = "\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b"
RegEx.IgnoreCase = True
RegEx.MultiLine = True
Set olMatches = RegEx.Execute(stremBody)
For Each match In olMatches
xlwksht.cells(i + 2, 1).Value = match
i = i + 1
Next match
'TODO move or mark the email that had the address extracted
Else
'To view the items that aren't being parsed uncomment the following line
'MsgBox (stremBody)
End If
x = x + 1
Next obj
xlApp.ScreenUpdating = True
MsgBox ("Invalid Email addresses are done being extracted")
ExitProc:
Set xlRng = Nothing
Set xlwksht = Nothing
Set xlwkbk = Nothing
Set xlApp = Nothing
Set emItm = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
Function GetExcelApp() As Object
' always create new instance
On Error Resume Next
Set GetExcelApp = CreateObject("Excel.Application")
On Error GoTo 0
End Function
untested
replace
RegEx.Pattern = "\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b"
RegEx.IgnoreCase = True
RegEx.MultiLine = True
with
RegEx.Pattern = "\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b"
RegEx.IgnoreCase = True
RegEx.MultiLine = True
RegEx.Global = True
I have noticed the following line of code:
Set olApp = Outlook.Application
If you run the code in Outlook, you need to use the Application property to get an instance of the Application class. Or you need to use the New operator to create a new instance, for example:
Set ol = New Outlook.Application
or
Set objOL = CreateObject("Outlook.Application")
See How to automate Outlook from another program for more information.
You may also consider using the Word object model for working with item bodies. The WordEditor property of the Inspector class returns an instance of the Document class which represents the message body. See Chapter 17: Working with Item Bodies for more information.