Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word to IE Explorer Automation Error

Status
Not open for further replies.

KristianDude

Technical User
Mar 18, 2016
65
US
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:
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 = "[URL unfurl="true"]http://forecast.weather.gov/zipcity.php"[/URL]

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
 
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!!

 
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top