Advertisement

03.17.2007 at 09:35AM PDT, ID: 22455796
[x]
Attachment Details

Saving a macro to a Word template which runs when the document based on the template opens

Asked by bogorman in Microsoft Word

Tags: word

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.csv"
   
   '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(sourceUrl) = 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(sSourceUrl)
   
   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
 
Loading Advertisement...
 
[+][-]03.17.2007 at 09:57AM PDT, ID: 18740750

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Word
Tags: word
Sign Up Now!
Solution Provided By: GrahamSkan
Participating Experts: 3
Solution Grade: A
 
 
[+][-]03.17.2007 at 11:10AM PDT, ID: 18740956

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.18.2007 at 07:01AM PDT, ID: 18743302

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2007 at 10:00PM PDT, ID: 18746302

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.19.2007 at 08:44AM PDT, ID: 18748863

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2007 at 08:59AM PDT, ID: 18749013

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2007 at 10:36AM PDT, ID: 18749960

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2007 at 12:39PM PDT, ID: 18751008

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2007 at 06:57AM PDT, ID: 18756201

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.21.2007 at 08:38AM PDT, ID: 18764717

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32