• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Solution for Macro Error

Aswinraj

Member
Hi Colleague,

I have created a Macro which use to do some steps in Internet Forums.
Example: I need to Paste a "Word" in Particular box in Fourm (Which is used internally in our office)

When i run the Macro using Breakpoint and F8 it was working fine.. But when i run the Macro without pressing F8 ie., when i click Update Button, the macro runs to some extend and throws an error.

Do you have any idea on why is it so?
 
Hi ,

The fastest way to get a solution is to :

1. Upload your workbook with the data ( if any ) and the code in it

2. If (1) is not possible , copy + paste the code in this thread.

Narayan
 
To add to what Narayan said, let us know which specific line of code is causing trouble will help us not have to read through whole thing. :)
 
This is the Code.., also i have highlighted the Line of Error - Function FnUpdateByName

Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public strDomainUrl As String

Sub GetDomainURL(strUI As String)
    If strUI = "Aldea" Then
        If Sheet2.chkServer Then
            strDomainUrl = "https://training.aldea.eds.com/"
        Else
            'strDomainUrl = "https://am.aldea.svcs.hp.com/"
        End If
    ElseIf strUI = "Aries" Then
        If Sheet2.chkServer Then
            strDomainUrl = "https://apj-i.svcs.hp.com/training_aries/"
        Else
            'strDomainUrl = "https://am.aldea.svcs.hp.com/"
        End If
    End If
End Sub

Function FnUpdateList(ieapp As InternetExplorer, strID As String, strText As String, Optional strTrigger As String)
  
    Dim ListItems As Long
    Dim LoopList As Long
    Dim ObjListBox As Object
    Dim Obj As Object
    Application.StatusBar = "Processing " & Left(strText, 150) & "..."
    Set Obj = ieapp.Document.GetElementsByName(strID)
    Set ObjListBox = Obj(0)
    ListItems = ObjListBox.Options.Length
    For LoopList = 0 To ListItems
        If UCase(Trim(ObjListBox.Item(LoopList).Text)) = UCase(Trim(strText)) Then
            ObjListBox.selectedindex = LoopList
            Exit For
        End If
    Next
do_events ieapp
    If strTrigger <> "NO" Then ObjListBox.onchange: Sleep 5000
    do_events ieapp
End Function

Function FnWait4IE(ieapp As InternetExplorer)
    ieapp.Visible = Not (Sheet1.chkShowme)
    Sleep 500
    Do
        DoEvents
        Sleep 400
    Loop Until ieapp.ReadyState = READYSTATE_COMPLETE And ieapp.Busy = False
End Function

Function FnNavigate(ieapp As InternetExplorer, strURL As String)
    ieapp.Navigate strURL
    do_events ieapp
End Function

Function FnUpdateByID(ieapp As InternetExplorer, strID As String, strText)
    ieapp.Document.GetElementById(strID).Value = strText
    do_events ieapp
End Function

Function FnClick(ieapp As InternetExplorer, strID As String)
    Application.StatusBar = "Submitting Form..."
    do_events ieapp
    ieapp.Document.GetElementById(strID).Click
    Sleep 2000
    do_events ieapp
End Function

Function FnGetText(ieapp As InternetExplorer, strID As String, strText) As String
    FnGetText = ieapp.Document.GetElementById(strID).innertext
    do_events ieapp
End Function

Function FnUpdateByName(ieapp As InternetExplorer, strFieldName As String, strText As String)
    Dim Obj As Object
    Set Obj = ieapp.Document.GetElementsByName(strFieldName)
    Application.StatusBar = "Processing " & Left(strText, 150) & "..."
    Obj(0).Value = strText
End Function

Sub OpenRequestForm()
    Dim ObjWord As New Word.Application
    Dim ObjWDoc As Word.Document
    Dim strFileName As String
    Dim rLoop As Long
    Set ObjWDoc = ObjWord.Documents.Open(Application.GetOpenFilename(Title:="Please select the Technical Requirements Form"), , True)
    rLoop = 8
    GetContentInfo ObjWDoc, rLoop
    GetFormFieldInfo ObjWDoc, rLoop
    ObjWDoc.Close False
    ObjWord.Quit
    End Sub
  
Sub GetContentInfo(ObjWDoc As Word.Document, rLoop As Long)
    Dim Obj As Word.ContentControls
    Dim iLoop As Long
    Set Obj = ObjWDoc.Content.ContentControls
    For iLoop = 1 To Obj.Count
        Sheet3.Cells(rLoop, 8) = Obj(iLoop).Range
        rLoop = rLoop + 1
        DoEvents
    Next iLoop
End Sub

Sub GetFormFieldInfo(ObjWDoc As Word.Document, rLoop As Long)
    Dim Obj As Word.FormFields
    Dim iLoop As Long
    Set Obj = ObjWDoc.FormFields
    For iLoop = 1 To Obj.Count
        Sheet3.Cells(rLoop, 8) = Obj(iLoop).CheckBox.Value
        rLoop = rLoop + 1
        DoEvents
    Next iLoop
End Sub

Sub WoW()

End Sub
 
Since it looks like you're dealing with the IE browser, my first guess would be that the page isn't finished loading. When you run via breakpoint or manual, you're going slow enough that page can load, but when in full auto, it's too fast. See here for further guidance on waiting, or you can do the "cheap" approach and just do an Application.Wait with some amount of time.

http://stackoverflow.com/a/23300977
 
Hi Luke,

Thanks for your reply.., Which means, what changes i need to do in the coding? Can you please send me the code Piece and in which place it has to be modified...,?

So that i will do those changes and try once again.
 
And i just tried running the code now, and i faced the issue as below.
The "Text" Which user provided should be placed in Particular Box but it was not working fine.

upload_2016-6-3_13-17-20.pngupload_2016-6-3_13-17-33.png
 
WHen it gets to that point, double check that strFieldName is filled in, and a valid choice?
Does the iewindow contain an actual document with specified field name?
 
Hi Luke,

I have attached the Macro file but you can view all the codes but you cannot getinto the website since i havent provided the credentials.

Please check the code and the error which i specified in my previous reply and fix this issue please.
 

Attachments

  • Internal Notes Updation 2.xlsb
    87.6 KB · Views: 3
Hi !

This error means element does not exist (check for any typo)
or not yet loaded / available in the webpage.

All we can do as we can't reach this webpage …
 
Actually i dont know how to resolve this.., Can anyone Change the coding and send back to me so that i will check whether it is working fine., Please
 
As piloting IE is specific to each webpage,
without reaching it, we are blind and can't code anything !

Check first if any typo within text variable.
In a second time, test if element exists within webpage
with IsObject or Is Nothing statements (see VBA help) …

Reorganize your code in an easier way :
no sub function or sub procedure, all code within a single procedure.
Desactivate all specific references and use late binding with CreateObject statement and all variable object declared as Object.

Anyway, if code works in step by step mode using F8 key in VBE,
check where you need a pause within your code and as Luke yet wrote
insert an Application.Wait codeline (see VBA help) …
(Neanderthal way but working with a well logic)
 
Hi Marc,

I have attached the Excel file, now when you run the Macro, now it will take you to the respective IE Page.., Hope this will help you. Please check it and help me..,
 

Attachments

  • Internal Notes Updation 2.xlsb
    88 KB · Views: 2
Hope now you can access the IE with the Macro.., The "Internal Notes" Provided in the Excel should copy the "Text" to the "Internal Notes blog" Option in IE.

Please Please help me to get rid of this.., Struggling a lot...,
 
This is the Main Procedure..,

This is Available in mdlReqCreation Module.
Code:
Sub CreateRequestAries()
    Dim strURL As String
    Dim rLoop As Long
    Dim Sht As Excel.Worksheet
    Dim ieapp As New InternetExplorer
    Dim iewindow As New InternetExplorer
    Dim Obj As Object
    'If Sheet2.chkServer Then
        strURL = "https://eu-i.serviceportal.hp.com/training_aldea/default.aspx"
       
    'Else
     '   strURL = "https://service.hp.com/aldea"
    'End If
    FnCheck_AriesLogin ieapp, strURL
    Set Sht = Sheet1
    rLoop = 8
    Do While Len(Trim(Sht.Cells(rLoop, 1))) <> 0
        FnCheck_AriesLogin ieapp, strURL
        'Update Initial Information
        FnUpdateByName ieapp, "txtRequestId", Sht.Cells(rLoop, 1) 'Request Title:
        FnClick ieapp, "btnGo"
        FnClick ieapp, "leftSideLink2"
        FnClick ieapp, "hrefChgFields"
        Set IEWindows = New SHDocVw.ShellWindows

This is the Webpage link:
https://training.aldea.eds.com/" - This is Available in mdlFunc Module.
 
When I click too many errors in your workbook code modules on my side !

And I manually tried the webpage link on differents webbrowsers
and each doesn't reach it with a security warning message I won't bypass …

So if procedure works in step by step mode,
insert a pause codeline before the one where an error occurs.
 
Actually only one time it works when i went through the code step by step.., after that it doesnt run.., May i know what is the fist error you are facing? So that i can confirm whether i too got the same error..,
 
You cannot access the Webpage link manually since it needs some Login credentials.., so it will not work.., Macro which was created will get you through the Login..,
 
First codeline error is Do While Len(Trim(Sht.Cells(rLoop, 1))) <> 0
'cause of the references activated in the project …

Like this, this project works only with same MS Office version as yours.
A version under or upper and it crashes !

So without reaching webpage, I can't do anything !

As your issue seems to be at beginner level,
use a pause if code works in step by step mode
or see "Piloting IE in VBA" tutorials all over the web …

You cannot access the Webpage link manually since it needs some Login credentials.
Nope ! On my side webbrowsers just don't open it …
 
First codeline error is Do While Len(Trim(Sht.Cells(rLoop, 1))) <> 0
'cause of the references activated in the project …

Small side note on this conversation...
In one of the weird VB logic things, the compiler starts failing at being able to do basic operations if there's a missing/invalid reference. The most common cause is say, file is built in more advanced version, and then sent to someone with older version. Older version doesn't know about new version, and whines.

Marc, you might be able to, on your end, first go to Tools - References and check if there's any missing items. As I said, this happens to me when someone sends me a file they build in Office 2016, but I only have 2010 installed at work.
 

Yes Luke I was yet aware of that when I answered.

But I got too many errors without it and even with my local references !
The reason why it's better to code using a late binding way
and avoiding a gas factory project …
 
Back
Top