![]() |
|
#1
|
||||
|
||||
|
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 tried from the above mentioned post but could not figure it out. Please help me learn Use of VBA function. |
|
#2
|
||||
|
||||
|
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
||||
|
||||
|
I forget to mention best part of my problem here. When I use code supplied by you, I am able to work with other instances of word, while I use my code, I ma not able to do anything with any word until the process is finished.
I know that overall speed will be the same, but will save my time as no need of waiting till process to complete and Meanwhile I can work with other word doc. (even same word doc) I may be wrong when it comes to literal meaning of asynch method, but what I want is to continue working with word while lengthy process of downloading loads of pdfs from web is underway using vba. |
|
#4
|
||||
|
||||
|
That has nothing to do with converting "the httpreq part into a function", though; it's all to do with the fact the code is running a different instance of Word, leaving your other instance(s) available for use by other processes (including you).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
||||
|
||||
|
So what you suggest?
Should I continue with existing method or convert the HTTP part to function? |
|
#6
|
||||
|
||||
|
Whether you convert the code or leave it alone is up to you; converting it isn't going to make it run any faster or suddenly allow Word to process anything asynchronously.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
||||
|
||||
|
Is there any way I can convert My code such a way that, While it is running, I can work with the word?
|
|
#8
|
||||
|
||||
|
Not when it's running on the Active document in the foreground Word session. By definition, that means you're running the code against whatever document is on screen and you can't do anything with that document until the code has finished; to be able to continue working in Word you would have to run the code in a different Word instance against a different document to the one you have on screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#9
|
||||
|
||||
|
Quote:
Apology for extended discussion but I don't Have any Idea what I can do with my problem. |
|
#10
|
||||
|
||||
|
You might use code like:
Code:
Sub Demo()
Dim wdApp As New Word.Application, wdDoc As Word.Document
With wdApp
.Visible = False
Set wdDoc = .Documents.Open(Filename:="Path & Filename", AddToRecentFiles:=False, Visible:=False)
With wdDoc
'Do your document processing here
End With
.Quit
End With
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| Tags |
| vba |
|
|