Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Warwick, Warwickshire, England
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create a word file (2000)

    By clicking a button in Access I'd like to open word and automatcially create a file with a specific name. I've sorted out creating the correct folders and subfolders to contain the document, but don't know how to tell word to save the file automatically as a specific name - any ideas?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a word file (2000)

    You can use Automation to control Word from Access. Set a reference to the Microsoft Word 9.0 Object Library in Tools | References... (in the Visual Basic Editor.)

    Dim objWord As New Word.Application
    Dim objDoc As Word.Document
    Dim strFileName As String

    Set objDoc = objWord.Documents.Add
    ' code to fill the document goes here
    ' ...
    strFileName = ... ' fill in the appropriate path and file name
    objDoc.SaveAs strFileName

    objDoc.Close
    Set objDoc = Nothing
    objWord.Quit
    Set objWord = Nothing

    See Automation 101 on <!profile=WendellB>WendellB<!/profile>'s website for an introduction to Automation.

  3. #3
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Warwick, Warwickshire, England
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Create a word file (2000)

    Thanks Hans

    Does the As New have any significance in the first line as opposed to just As?

    I needed to be able to modify the file once I'd saved it, so used objWord.visible = true

    If I do this with the close and quit options left in, it obviously displays the document for a split second, then closes the doc, quits Word and returns to Access. Can I allow it to stay in Word until the changes have been made and word is quit manually before going back to Access?

    John

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a word file (2000)

    1. Dim ... As New ... means that you declare an object variable and set it at the same time. It is equivalent to

    Dim objWord As Word.Application
    Set objWord = CreateObject("Word.Application")

    2. If you want to edit the document, use

    objWord.Visible = True

    as you have done, and delete the lines objDoc.Close and objWord.Quit. The document will remain open in Word then until the user closes it.

  5. #5
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Warwick, Warwickshire, England
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Create a word file (2000)

    Getting there.....
    one (hopefully) last question - using objDoc.SaveAs strFileName causes the file to be saved whether it already exists or not. Is there a switch that checks and prompts for a new filename if it already exists?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a word file (2000)

    According to the online help, SaveAs overwrites existing documents without asking, so you'll have to write your own check:

    strFileName = ... ' fill in the appropriate path and file name
    If Not Dir(strFileName) = "" Then
    If MsgBox("A document " & strFileName & " already exists. Overwrite it?", _
    vbYesNo + vbQuestion) = vbNo Then
    Exit Sub
    End If
    End If
    objDoc.SaveAs strFileName

  7. #7
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Warwick, Warwickshire, England
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Create a word file (2000)

    Is there a method of forcing Word to maximise at the time of opening the document? It seems to be a bit hit and miss as to how it starts.

    Also, how would I open a specific file, rather than creating a new one - I'm going to change the routine so that if the file exists it opens it, otherwise it creates a new file with the specified filename.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a word file (2000)

    Using the same variables as above:

    1. To maximize the Word window:

    objWord.WindowState = wdWindowStateMaximize

    2. To open an existing document:

    Set objDoc = objWord.Documents.Open(strFileName)

  9. #9
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Warwick, Warwickshire, England
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Create a word file (2000)

    Thanks very much for all your help with that Hans - works great. I'll refine it further now that I have a few of the commands and have the gist of what is required to carry out each task.
    Many thanks
    John

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a word file (2000)

    If you need more, it is often easier to try out things in Word first, for example by recording a macro, and looking at the generated VBA code. When you've got it right, you can transfer the code to Access, prefixing methods and properties that belong to the Word application with objWord. For instance, if you type some text in a Word document, the macro recorder generates code like this:

    Selection.TypeText Text:="This is a sentence."
    Selection.TypeParagraph

    In Access, this becomes:

    objWord.Selection.TypeText Text:="This is a sentence."
    objWord.Selection.TypeParagraph

  11. #11
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Warwick, Warwickshire, England
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Create a word file (2000)

    Thanks for the advice regarding the Word testing.
    One slight problem I'm still having with this routine is with regard to which folder has the focus (for want of a better word).
    The start of the routine creates a client folder in Cata eg. cataClientOne and a subfolder in this for the contract eg. CataClientOneContractThree. The files I'm creating are then stored in this structure.
    During testing I was creating and deleting files, but found that I wasn't able to delete a subfolder which I had just written to via the Access/Word routines. I could delete the Word file which I had just created, but not the folder in which it was contained.
    I probably won't need to delete anything in this manner, but was interested to know what is holding the folder to make it undeleteable
    The lock is released when I close the Access database
    John

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a word file (2000)

    This may be due to the operating system, or to the way the OS and Access interact. I used to have a similar problem in Windows NT4, but don't have it any more since I switched to Windows XP. I don't know if there is an easy remedy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •