INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Word to IE Explorer Automation Error

Word to IE Explorer Automation Error

(OP)
Well, this was working at one time, but now I am getting an automation error. I changed a bit of code and tried to compare to the original code I had and can't seem to put my finger on the issue.. if anyone can see the issue it would be a huge help!!!.. I'll post the code right up to where I am getting an error in an effort to minimize this post. Also, I have commented out my error handler in an effort to try and figure this out which is how I found "The object invoked has disconnected from its clients" error message

Just a brief description of what this is for.. I am grabbing a screenshot and some text data that I am pasting into a word document. It works great until the last section labeled "find 3 day data page". When this happens, the web page is not visible and I am pasting and saving the screenshot. When it goes to refer back to the webpage it is then that it is disconnected. Not sure if that helps figure out the issues at all!?

Thanks everyone!

CODE --> VBA

Sub Weather()
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Dim oHTML_Element As IHTMLElement
Dim sURL As String
Dim occ As ContentControl
Dim strToday
Dim strPath
Dim iRet As Integer
Dim strPrompt As String
Dim strTitle As String

'On Error GoTo Err_Clear


sURL = "http://forecast.weather.gov/zipcity.php"

Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.navigate sURL
oBrowser.FullScreen = True
oBrowser.Visible = True


Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

'time to load
WaitSeconds (2)

HTMLDoc.all.inputstring.Value = Me.txtZip

'time to load
WaitSeconds (2)

For Each oHTML_Element In HTMLDoc.getElementsByName("Go2")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next oHTML_Element

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

'Request seconds to wait for user input case by case
Dim strUser As String
'strUser = InputBox("Default page load time is 5 seconds. Change?", "Change Default Time?", 5)
strUser = "10"

        'check directory
        strPath = ActiveDocument.Path & "\Weather"
        If Len(Dir(strPath, vbDirectory)) = 0 Then
        MkDir strPath
        End If
        
        'image path
        Set occ = ActiveDocument.SelectContentControlsByTitle("ctrlCalendar").Item(1)
        strToday = strPath & "\Weather " & Format(Date, "mm-dd-yy") & ".jpg"
        strPath = strPath & "\Weather " & Format(occ.Range.Text, "mm-dd-yy") & ".jpg"
        
        'hide browser
        oBrowser.Visible = False
        
        '***SCREENSHOT TODAY'S FORECAST
        If Len(Dir(strToday, vbDirectory)) = 0 Then
            MsgBox "    Please highlight today's forecast area"
            
            'clip forecast
            oBrowser.Visible = True
            
            'time to load
            WaitSeconds (3)
            
                'print screen
                keybd_event VK_SNAPSHOT, 1, 0, 0 'Print Screen key down
                keybd_event VK_SNAPSHOT, 1, VK_KEYUP, 0 'Print key Up - Screenshot to Clipboard
            
            'time to capture area
            WaitSeconds (strUser)
            
            'save clipboard to file
            SaveClipboardasJPEG (strToday)
            
            Else
            
        End If

        
        '***CHECK FOR FILE TO IMPORT
            'If Format(Date, "mm-dd-yy") <> Format(occ.Range.Text, "mm-dd-yy") Then
                If Len(Dir(strPath, vbDirectory)) = 0 Then
                    MsgBox "       There is not a screenshot available for this date" & vbCr & _
                    "         Please import weather for this date manually" & vbCr & _
                    "                     Now closing weather import"
                    Exit Sub
                    Else
                    
                    '***IMPORT EXISTING SCREENSHOT
                    MsgBox "     A screenshot is now being imported"
                       
                    'insert image to document
                    Set occ = ActiveDocument.SelectContentControlsByTitle("ctrlPicture").Item(1)
                    
                    If occ.Type = wdContentControlPicture Then
                        If occ.Range.InlineShapes.Count > 0 Then occ.Range.InlineShapes(1).Delete
                        Dim pPicFileName As String
                        pPicFileName = strPath
                            ActiveDocument.InlineShapes.AddPicture Filename:=pPicFileName, _
                            linktofile:=True, Range:=occ.Range
                            With occ.Range.InlineShapes(1)
                            .LockAspectRatio = msoCTrue
                            .width = 504
                            End With
                        End If
                    Selection.MoveRight
                    Selection.Collapse
                    
                    End If
             'End If
                
                
'IMPORT TEMP DATA
' Prompt
strPrompt = "      Would you like to import temperature data?"

' Dialog's Title
strTitle = "Import Temperature Data"

'Display MessageBox
iRet = MsgBox(strPrompt, vbYesNo, strTitle)

' Check pressed button
If iRet = vbYes Then

'clear clipboard
ClearClipboard

    'find 3 day data page
    For Each oHTML_Element In HTMLDoc.Links
    If InStr(oHTML_Element.innerText, "3 Day History") Then
    Call oHTML_Element.Click
    Exit For
    End If
    Next oHTML_Element 

RE: Word to IE Explorer Automation Error

(OP)
Something else that might work is if I could search for the open window and get a rehandle? on it vs figuring out why it's not staying connected?? I am open to any fixes. Thanks guys!!

RE: Word to IE Explorer Automation Error

(OP)
Morning everyone... a question that I thought of is there anything that would cause VBA to "disconnect" the connection to the InternetExplorer application besides IE referencing?... I'm stepping through the code and it seems to lose the reference and random points, sometimes sooner, sometimes later. Is this something that should just be assumed to happen and come up with code to find the IE window every time I want to do something with it?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close