Word doc SaveAs, close and other VBA qns

Scytale

New member
Local time
Today, 06:31
Joined
Dec 2, 2007
Messages
1
Hi, I have a tech background from pre PC days but no knowledge of Acess/VBA etc.
I've blundered arround an created an Access DB that in addition to storing data in tables should generate a word document from a template and then attach it to an automatically generated and sent email. I've sort of got it working using code from this BBS (can't remember who but thanks guys) and the MS site.

(My code is at end of msg)

1. I can't get it to automatically do a "save as" and close the document see comments in red - what am I doing wrong?

2. This sub(?) seems to be fired off as a separate independent process, the calling procedure then calls a sub(?) to email the word doc - is there any way, other than a message box, to pause the calling proc(?) until the word doc has been closed otherwise the document for attaching to the email will not exist in time.

3. Is there a VBA instruction to bring the Word window on top when this code is called?

4. As I said I'm a newbie blundering about - feel free to criticise my(mostlly copied code) suggest improvements!!!!

5. Sorry one more - How/where do I set up global constants eg to hold file pathfor this DB - I have searched around but can't find anything about this.

Any help greatly appreciated.

Code:
Sub genWordDoc()

' Create a Word document from template.
   Dim WordApp As Word.Application
   Dim wrdDoc As Word.Document

   Dim strPathFilePrefix As String, strFileSuffix As String, strFullFilePath As String    
  
   [COLOR="Blue"]' specify path and part of filename common to both template and output file[/COLOR] 
   strPathFilePrefix = "C:\path\Problem Report "
   strFileSuffix = "xx.dot"
   [COLOR="Blue"]' Specify location of template[/COLOR]
   strFullFilePath = strPathFilePrefix & strFileSuffix

   On Error Resume Next

   Set WordApp = GetObject(, "Word.Application")
   If Err.Number <> 0 Then
      Set WordApp = CreateObject("Word.Application")
   End If
   On Error GoTo ErrHandler  
   
   WordApp.Visible = True
   WordApp.WindowState = wdWindowStateMaximize
   Set wrdDoc = WordApp.Documents.Add(Template:=strFullFilePath, NewTemplate:=False)


   [COLOR="blue"]' Replace each bookmark with field contents[/COLOR].
   With WordApp.Selection
   
     .GoTo what:=wdGoToBookmark, Name:="DateTested"
     .TypeText Nz([Date Tested], "")

[COLOR="blue"]     '   
     ' more statements to insert data here
     '
     '   
     ' Finish by putting the cursor to the top of the template letter – may not  
     ' need this if autosave etc[/COLOR] 
     .GoTo what:=wdGoToBookmark, Name:="DateTested"
     .TypeText " "
    
   End With
    
    [COLOR="blue"]' set strFullFilePath string to hold appropriate Problem Report name eg "\\share\path\ProblemReport 123.doc"[/COLOR]
   strFullFilePath = strPathFilePrefix & [PReportNo] & ".doc"
   
 [COLOR="Red"]'  among many options I've tried - dont work but no error generated  [/COLOR]
   wrdDoc.SaveAs FileName:=strFullFilePath, FileFormat:=wdFormatDocument
   '   wrdDoc.SaveAs FileName:=strFullFilePath

[COLOR="Red"]'   May want to allow user to paste into doc at some stage but doc needs 
'   to already be correctly named by saveas above[/COLOR]
'   MsgBox "Paste in any screen prints etc, and click disk icon to save document"
'  or possibly msgbox yes no qns to prompt insert and quit?

[COLOR="red"]'  Does not close Word window![/COLOR]
   wrdDoc.Close (True)

   
[COLOR="red"]' can't remember why I've stuck this here ![/COLOR]   WordApp.Activate
   
   WordApp.Quit
    
   Set WordApp = Nothing

ErrHandler:
   Set WordApp = Nothing
      
 
End Sub
 

Users who are viewing this thread

Back
Top Bottom