Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-10-2016, 10:08 PM
PRA007's Avatar
PRA007 PRA007 is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Competent Performer
How to run vba asynchly
 
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
  #2  
Old 01-11-2016, 12:48 AM
macropod's Avatar
macropod macropod is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by PRA007 View Post
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.
I'm not sure what you mean by the last remark or what part of the code is 'time wasting'. Furthermore, converting "the httpreq part into a function" isn't going to make things happen any faster or make the code run asynchronously.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-11-2016, 03:38 AM
PRA007's Avatar
PRA007 PRA007 is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Competent Performer
How to run vba asynchly
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 01-11-2016, 03:56 AM
macropod's Avatar
macropod macropod is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by PRA007 View Post
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.
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]
Reply With Quote
  #5  
Old 01-11-2016, 04:07 AM
PRA007's Avatar
PRA007 PRA007 is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Competent Performer
How to run vba asynchly
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

So what you suggest?
Should I continue with existing method or convert the HTTP part to function?
Reply With Quote
  #6  
Old 01-11-2016, 04:17 AM
macropod's Avatar
macropod macropod is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 01-11-2016, 04:21 AM
PRA007's Avatar
PRA007 PRA007 is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Competent Performer
How to run vba asynchly
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

Is there any way I can convert My code such a way that, While it is running, I can work with the word?
Reply With Quote
  #8  
Old 01-11-2016, 04:28 AM
macropod's Avatar
macropod macropod is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by PRA007 View Post
Is there any way I can convert My code such a way that, While it is running, I can work with the word?
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]
Reply With Quote
  #9  
Old 01-11-2016, 04:36 AM
PRA007's Avatar
PRA007 PRA007 is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Competent Performer
How to run vba asynchly
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
How to achieve that?
Apology for extended discussion but I don't Have any Idea what I can do with my problem.
Reply With Quote
  #10  
Old 01-11-2016, 04:49 AM
macropod's Avatar
macropod macropod is offline How to run vba asynchly Windows 7 64bit How to run vba asynchly Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
vba



Other Forums: Access Forums

All times are GMT -7. The time now is 11:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft