I'm working with an Internet Explorer based application where I need to retrieve the value of an input text box that is read only. I've looked at other Stack Overflow questions and don't see anything about getting the value of a read only or hidden input box. I haven't found anything I can use on the internet either.
Here's the HTML code for the input box I'm trying to get the value from:
<div class="col-xs-10 col-sm-9 col-md-6 col-lg-5 form-field input_controls">
<div class="hidden" ng-non-bindable="">
<input name="sys_original.x_opt_im_issue_task.number" id="sys_original.x_opt_im_issue_task.number" type="hidden" value="TSK0111065" />
</div>
<input class="form-control disabled " id="sys_readonly.x_opt_im_issue_task.number" readonly="readonly" ng-non-bindable="" value="TSK0111065" />
</div>
Here's the VBScript code I'm trying to use to get the value of the input text box that isn't working:
UPDATED
Option Explicit
Dim WShell, objShell, objIE
Dim strMessage, URL, ErrMsg, URLFound, Browser
Dim EN_ID, EntNowID
Sub Main()
On Error Resume Next
Set WShell = CreateObject("WScript.Shell")
Set objShell = CreateObject("Shell.Application")
If Err.Number <> 0 Then ShowError("Failed to create objects")
On Error GoTo 0
Check_EN
SetEverythingToNothing
End Sub
'---------------------------------
Sub ShowError(strMessage)
MsgBox strMessage & vbNewline & vbNewline & "Error number: " & Err.Number & vbNewline & "Source: " & Err.Source & vbNewline & "Description: " & Err.Description
Err.Clear
SetEverythingToNothing
End Sub
'------------------------------
Sub Check_EN()
URL = "https://enterprisenow.mysite.com"
ErrMsg = "EnterpriseNOW is not open or on the incorrect page. Please check & rerun the macro."
Check_URL
ErrMsg = ""
Set EN_ID = objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number")
EntNowID = EN_ID.Value
MsgBox EntNowID
End Sub
'------------------------------
Function Check_URL()
URLFound = False
For Each Browser In objShell.Windows()
If InStr(UCase(Browser.LocationURL), UCase(URL)) > 0 Then
If InStr(UCase(Browser.FullName), "IEXPLORE.EXE") Then
If Err.Number = 0 Then
Set objIE = Browser
URLFound = True
Exit For
End If
End If
End If
Next
If URLFound = False Then
MsgBox "Unable to find URL."
SetEverythingToNothing
End If
End Function
'-----------------------------
Sub SetEverythingToNothing()
Set WShell = Nothing
Set objShell = Nothing
Set Browser = Nothing
Set objIE = Nothing
End Sub
I'm able to set the objIE object and find the URL, but I'm receiving "Run-time error '424': Object required". Is it because the input text box is hidden or read only? I'm also wondering if it has anything to do with nested div tags?
Try the following - it worked for me. What I noticed is that if I accessed:
objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number").Value
more than once, I got the error: Object required
However if I used 'Set' and created an object like so:
Set tasknumber = objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number")
I could then access the value multiple times without receiving the error.
Option Explicit
Dim objShell : Set objShell = CreateObject("shell.application")
Dim URL : URL = "https://enterprisenow.mysite.com/"
Dim URLFound : URLFound = False
Dim Browser, tasknumber, tasknumbervalue
For Each Browser In objShell.Windows()
If InStr(UCase(Browser.FullName), "IEXPLORE.EXE") Then
If InStr(UCase(Browser.LocationURL), UCase(URL)) > 0 Then
If Err.Number = 0 Then
Set tasknumber = Browser.document.getElementById("sys_readonly.x_opt_im_issue_task.number")
If (NOT IsNull(tasknumber) And NOT tasknumber Is Nothing) Then
tasknumbervalue = tasknumber.value
End If
Set tasknumber = Nothing
Exit For
End If
End If
End If
Next
MsgBox tasknumbervalue
Set Browser = Nothing
Set objShell = Nothing
Well, it's not the solution I was hoping for, but it's easier than trying to get the number from the input field.
I had a feeling that the iframe tag may be preventing me from detecting the input field ID, so I looked at the iframe and saw that the TSK number in the input field was also in the iframe tag.
Instead of trying this:
EN_ID = objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number").Value
I was able to use this:
iframeName = Split(objIE.document.getElementById("gsft_main").Title, " | ") 'Title = "TSK0111065 | Issue Task | ServiceNow"
EN_ID = iframeName(0)
Related
I am trying to access a webpage and update the user name and password, but unable to get the getElementById
I tried the similar code with google and yahoo, they work perfectly. Below is the code I'm using
Sub Openurl()
Dim IE As Object
Dim IEPage As Object
Dim IEPageElement As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate URL
' The below piece of code also does not work
Do Until IE.readyState = 4 'READYSTATE_COMPLETE in early binding
DoEvents
Loop
Set IEPage = IE.Document
Set IEPageElement = IEPage.getElementById(cwsUID)
If Not IEPageElement Is Nothing Then
IEPageElement.Value = UserName
Set IEPageElement = Nothing
Else
MsgBox "Coould not find the '" & UNElementID & "' element ID on the page!", vbCritical, "Element ID Error"
Exit Sub
End If
End Sub
Kindly let me know if there is something wrong with this code.
Below is the source code
input type="text" name="cwsUID" id="cwsUID" value="" placeholder="CWS ID" autocomplete="off" autofocus=autofocus
Although I am not VB-expert, I believe you have to use quotes:
IEPage.getElementById("cwsUID")
as cwsUID is not a variable name but id.
I was able to get the GetElementID by using
Set IE = New InternetExplorerMedium
I have a VBA code which allows me to read/place string in HTML objects (like input boxes).
This is the example of code:
Sub ReadCheck()
Dim objlement As Object
Dim i As Integer
Dim isNothing As Boolean
'
Dim shellWins As ShellWindows
Dim ie As InternetExplorer
'Set shellWins = New ShellWindows
Set shellWins = New ShellWindows
i = shellWins.Count
'On Error Resume Next
For i = 0 To i - 1
If shellWins.Item(i).Parent = "Internet Explorer" Then
If shellWins.Item(i).Document.URL = "https://intservices.mybebpage" Then
Set ie = shellWins.Item(i)
Exit For
End If
End If
Next i
On Error GoTo 0
isNothing = ie Is Nothing
Debug.Print Format(Now, "HH:MM:SS") & " IE is nothing: " & isNothing
If isNothing Then Exit Sub
'combo box
Set objlement = ie.Document.getelementsbyname("txtboxx")
objlement (0).Value = "Some text to place"
End Sub
Due to my testing, this code works fine with 99% of cases, but sometimes it returns error 91 (Object variable or With block variable not set), in the last line.
I'm pretty sure, that I can get correct object for objelement, but it looks like there were no .Value property for it.
This is wierd, becouse I know that this code works.
In my opinion the most possible reason for this is that I should use some different properties than .Value for this.
Do you have any comments for this?
Using Access 2010, I have a form for Purchase_Orders where the status changes depending on the whether the Items in the sub form have been delivered or not, and, it is influenced by the date as well.
Private Sub Form_AfterUpdate()
Dim rs As Recordset
Dim db As Database
Dim var_Delivered As String
var_Delivered = "SELECT Count(*) AS d_Count" & _
" FROM Items" & _
" WHERE PO_ID =" & Me.PO_ID.Value & _
" AND Supplier_Dnote_ID IS Null" & _
" AND Delivered_Without_Dnote =0;"
Set db = CurrentDb
Set rs = db.OpenRecordset(var_Delivered, dbOpenDynaset)
'MsgBox rs!d_Count
If rs!d_Count > 0 Then
If Me.Supply_date < Date Then
Me.Status = "Overdue"
Else
Me.Status = "Submitted"
End If
Else
Me.Status = "Delivered"
End If
db.Close
Set db = Nothing
Set rs = Nothing
End Sub
This runs after_update of the Purchase_Orders. I have a save_close button that uses the following code and doesn't return an error:
If Me.Dirty = True Then
DoCmd.Close acForm, "Purchase_Orders", acSaveYes
Else
DoCmd.Close acForm, "Purchase_Orders", acSaveNo
End If
However, I also have a Save button that doesn't close the form. This is where I get run-time error 2759 : The method you tried to invoke on an object failed. Debug Highlights the saverecord line.
Private Sub SaveOnlyBtn_Click()
If Me.Dirty = True Then
docmd.RunCommand acCmdSaveRecord
End If
End Sub
If I comment the status code out and use the save button, the record saves fine without any errors. Why do I get this error? I'm completely stumped and searching the error online hasn't helped me either.
So I found that the error did not occur when I put the code in the "on dirty" event, which then made me realise that I don't need necessarily have to run the code after the form updates, only when specific fields change. So I changed my code to a public code and called it when supply date, delivered_without_dnote, or supplier_Invoice_ID changed.
the public code is :
Public Sub delivered_status()
On Error GoTo errTrap1
If Forms!Purchase_Orders_Ex.Form!Status = "Cancelled" Then
Exit Sub
Else
DoCmd.RunCommand acCmdSaveRecord
Dim rs As Recordset
Dim db As Database
Dim var_Delivered As String
var_Delivered = "SELECT Count(*) AS d_Count" & _
" FROM Items" & _
" WHERE PO_ID =" & Forms!Purchase_Orders_Ex.Form!PO_ID.Value & _
" AND Supplier_Dnote_ID IS Null" & _
" AND Delivered_Without_Dnote =0;"
Set db = CurrentDb
Set rs = db.OpenRecordset(var_Delivered, dbOpenDynaset)
'MsgBox "Outstanding Items: " & rs!d_Count
If rs!d_Count > 0 Then
If Forms!Purchase_Orders_Ex.Form!Supply_date < Date Then
Forms!Purchase_Orders_Ex.Form!Status = "Overdue"
Else
Forms!Purchase_Orders_Ex.Form!Status = "Submitted"
End If
Else
Forms!Purchase_Orders_Ex.Form!Status = "Delivered"
End If
rs.Close
Set db = Nothing
Set rs = Nothing
End If
errTrap1:
Select Case Err.Number
Case 3314 'form not complete and other required fields are empty
Exit Sub
Case Else
If Err.Number > 0 Then
MsgBox Err.Number & ": " & Err.Description
End If
End Select
End Sub
Now, when I use either the save_close or Save_Only I do not get error 2759. I do not completely understand which part of my original method caused the error but it no longer occurs with this approach.
I've just encountered this issue and moving code out of Form_AfterUpdate fixed it for me too.
What's (vaguely) interesting is that the code in question worked fine locally, but did not work when deployed to the client. I tried importing just the amended form instead of replacing the whole access app, but I still got the same issue. I also copied the back-end database back from the server to my development machine, but still didn't get the issue locally. On top of that I did endless compact/repair and decompile/compile.
My conclusion at the end of all of that was that this was yet another weird issue emanating from the Access black-box, rather than an issue with the particular code.
I'm using VBA in access to open up a protected word template, fill in the data, and then re-protect it.... this way, if the database system goes down, the word template can still be used manually in its protected state.
I have just started using VBA and in this line:
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
I'm concerned that whilst running the code in access, that if the user opens up another word document and makes it the focus, that it will occidentally get protected instead of the other. How do I keep active focus on the document I'm writing to... or do I need to reference my document somehow using WordApp.protect (or something similar that works)
Private Sub Command0_Click()
Dim WordApp As Word.Application
Dim strDatabasePath As String
Dim strTemplatePath As String
Dim strTemplate As String
Dim strJobTitle As String
Dim strFile As String
strFile1 = "testcoc.dotx"
strFile2 = "testcoc-private.dotx"
strDatabasePath = CurrentProject.Path & "\"
strTemplatePath = "\templates\"
strTemplate = strDatabasePath & strTemplatePath & strFile2
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo ErrHandler
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplate, NewTemplate:=True
'strJobTitle = DLookup("JobTitle", "Job", "JobNum = " & [JobType])
strJobTitle = DLookup("JobTitle", "Job", "JobNum = 'J0456'")
With WordApp.Selection
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
.Goto what:=wdGoToBookmark, Name:="bm_0_4"
.TypeText strJobTitle
End With
'Reprotect the document.
'If ActiveDocument.ProtectionType = wdNoProtection Then
'ActiveDocument.Protect _
'Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
'End If
DoEvents
WordApp.Activate
Set WordApp = Nothing
Exit Sub
ErrHandler:
Set WordApp = Nothing
End Sub
Thank You
I haven't tried this but WordApp.Documents.Add Template:=strTemplate, NewTemplate:=True does return the new document. So I would do something like
Dim doc as Word.Document
Set doc = WordApp.Documents.Add(Template:=strTemplate, NewTemplate:=True)
and reference doc throughout my code instead of ActiveDocument. It seems like doing that should get help you avoid the particular situation you're concerned about.
I'm using a code i found online to fill in form fields in a Word document. When i use it on an empty document and add to it a form field, it works. However, when i use it on the form i'm trying to fill nothing happens when i execute the code. I checked the name of the fields in Word and they match the code, i don't know what's wrong.
I also checked the data type in Access and it's not the problem, i don't have access to the code right now but does anyone have any idea of what's causing this?
Edit: Here is a similar code, i don't have access to the exact same one:
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("C:\Users\" & Environ$("Username") & "\Desktop\Form.doc", , True)
With doc
.FormFields("TextEn").Result = DLookup("[End date]", "[Table1]", "[Table1]![ID Number] =" & [ID2])
.FormFields("TextSt").Result = DLookup("[Starting date]", "[Table1]", "[Table1]![ID Number] =" & [ID2])
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
You could try to validate that your dlookup is returning a value with the following:
If DCount("[End date]", "[Table1]", "[Table1]![ID Number] =" & [ID2]) > 0 then
.FormFields("TextEn").Result = DLookup("[End date]", "[Table1]", "[Table1]![ID Number] =" & [ID2])
End If
You would have to do this for each lookup before trying to set your fields.