• Create a word file (2000)

    Author
    Topic
    #403187

    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?

    Viewing 1 reply thread
    Author
    Replies
    • #808666

      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 WendellB‘s website for an introduction to Automation.

      • #808674

        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

        • #808680

          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.

          • #808724

            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?

            • #808741

              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

            • #808812

              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.

            • #808824

              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)

            • #808830

              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

            • #808834

              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

            • #808838

              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 C:Data eg. c:DataClientOne and a subfolder in this for the contract eg. C:DataClientOneContractThree. 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

            • #808840

              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.

            • #808841

              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.

            • #808839

              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 C:Data eg. c:DataClientOne and a subfolder in this for the contract eg. C:DataClientOneContractThree. 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

            • #808835

              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

            • #808833

              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

            • #808825

              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)

            • #808813

              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.

            • #808742

              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

          • #808725

            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?

        • #808681

          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.

      • #808675

        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

    • #808667

      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 WendellB‘s website for an introduction to Automation.

    Viewing 1 reply thread
    Reply To: Create a word file (2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: