I have the following macro coding which has been saved to a Word 2003 document:
Option Explicit
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
Sub DownloadFromWeb()
'
' DownloadFromWeb Macro
' Macro created 3/16/2007 by Brian O'Gorman
'
Dim sSourceUrl As String
Dim sLocalFile As String
Dim hfile As Long
sSourceUrl = "
http://63.134.211.113/TestText.csv"
sLocalFile = "c:\Web Design\LIFEnet\TestText.cs
v"
'Label1.Caption = sSourceUrl
'Label2.Caption = sLocalFile
'Attempt to delete any cached version of
'the file. Since we're only interested in
'nuking the file, the routine is called as
'a sub. If the return value is requires
'(calling as a function), DeleteUrlCacheEntry
'returns 1 if successful, or 0 otherwise, e.g.
' If DeleteUrlCacheEntry(source
Url) = 1 Then
' Debug.Print "cached file found and deleted"
' Else
' Debug.Print "no cached file for " & sourceUrl
' End If
'Note that the remote URL is passed as this is the
'name the cached file is known by. This does NOT
'delete the file from the remote server.
Call DeleteUrlCacheEntry(sSourc
eUrl)
If DownloadFile2(sSourceUrl, sLocalFile) = True Then
hfile = FreeFile
Open sLocalFile For Input As #hfile
'Text1.Text = Input$(LOF(hfile), hfile)
Close #hfile
End If
End Sub
Private Function DownloadFile2(sSourceUrl As String, _
sLocalFile As String) As Boolean
'Download the file. BINDF_GETNEWESTVERSION forces
'the API to download from the specified source.
'Passing 0& as dwReserved causes the locally-cached
'copy to be downloaded, if available. If the API
'returns ERROR_SUCCESS (0), DownloadFile returns True.
DownloadFile2 = URLDownloadToFile(0&, _
sSourceUrl, _
sLocalFile, _
BINDF_GETNEWESTVERSION, _
0&) = ERROR_SUCCESS
End Function
Can someone answer the following:
a) How can I save this to a Word TEMPLATE so the coding runs when documents based on the template open.
b) As the template is a mailmerge one, when the macro runs and overwrites the data file for the mailmerge will Word hang?
c) Is it possible to "autorun" the macro when the document opens (this coding to be saved to the template) and code it so that the user is prompted to run the whole mailmerge or just the first record to test it.
If you think I have asked two many things for one question, pse say so and I will create a new question for (c).
Start Free Trial