Thread: [Solved] How to run vba asynchly
View Single Post
 
Old 01-10-2016, 10:08 PM
PRA007's Avatar
PRA007 PRA007 is offline Windows 7 64bit Office 2010 32bit
Competent Performer
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default How to run vba asynchly

While I got nice answer at: https://www.msofficeforums.com/word-...data-word.html, I thought what is need of such complicated process?

After spending much time with VBA I realized that, if not proper, VBA may be wasting your time rather than saving.

This is example of time wasting VBA.

Code:
Sub DownPDFFromHLnk()
Application.ScreenUpdating = False
Dim StrTxt As String, HttpReq As Object
Set HttpReq = CreateObject("Microsoft.XMLHTTP")
With ActiveDocument.Range
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[A-Z]{2} [0-9,]{5,}"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
    End With
        Do While .Find.Found
            If .Hyperlinks.Count > 0 Then
                f = ActiveDocument.Path & Application.PathSeparator & .Text & ".pdf"
                HttpReq.Open "GET", .Hyperlinks(1).Name, False
                HttpReq.send
                    If HttpReq.Status = 200 Then
                        Set oStream = CreateObject("ADODB.Stream")
                        oStream.Open
                        oStream.Type = 1
                        oStream.Write HttpReq.responseBody
                        oStream.SaveToFile f, 2
                        oStream.Close
                        .Hyperlinks(1).Address = f
                    End If
            End If
        .Collapse wdCollapseEnd
        .Find.Execute
        Loop
End With
End Sub
I want to convert the httpreq part into function.
I tried from the above mentioned post but could not figure it out. Please help me learn Use of VBA function.
Reply With Quote