Results 1 to 8 of 8
  1. #1
    Forbes's Avatar
    Forbes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2017
    Posts
    133

    Closing WINWORD Background Process


    I have a form that auto populates a word doc, then is supposed to save it and close word, it all works except closing word at the end, the doc isn't visable but the process runs in the background. I cannot use shell commands to kill the task because not all users have that privilege.

    Code:
    Private Sub cmdPrint_Click()
    
    
    
    Dim appWord As Word.Application
    Dim CloseWord As String
    Dim doc As Word.Document
    
    
    'Avoid error 429, when Word isn't open.
    
    
    On Error Resume Next
    
    
    Err.Clear
    
    
    'Set appWord object variable to running instance of Word.
    
    
    Set appWord = GetObject(, "Word.Application")
    
    
    If Err.Number <> 0 Then
    
    
    'If Word isn't open, create a new instance of Word.
    
    
    Set appWord = New Word.Application
    
    
    End If
    
    
    Set doc = appWord.Documents.Open("C:\doc.doc", , True)
    
    
    With doc
    
    
    .FormFields("txtHName").Result = Me!TXTAN
    
    
    .FormFields("txtAddOne").Result = Me!txtAA1
    
    
    .FormFields("txtAddTwo").Result = Me!txtAA2
    
    
    .FormFields("txtBo").Result = Me!txtBN
    
    
    .FormFields("txtPol").Result = Me!txtPN
    
    
    .FormFields("txtInv").Result = Me!txtIN
    
    
    .FormFields("txtProp1").Result = Me!txtBA1
    
    
    .FormFields("txtProp2").Result = Me!txtBA2
    
    
    '.FormFields("fldCountry").Result = Me!Country
    
    
    '.FormFields("fldPhone").Result = Me!Phone
    
    
    '.FormFields("fldFax").Result = Me!Fax
    
    
        
    .Visible = True
    
    
    .Activate
    
    
    ActiveDocument.SaveAs FileName:="C:\doc" & strNewFolderName & "\" & Me!txtBN & "," & "Letter.doc", _
        FileFormat:=wdFormatDocument
        
    
    
    
    
    
    
        
    End With
    
    
    Set doc = Nothing
    
    
    Set appWord = Nothing
    
    
    
    
    Exit Sub
    
    
    errHandler:
    
    
    MsgBox Err.Number & ": " & Err.Description
    
    
    
    
    End Sub
    any ideas?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    A couple of thoughts. You might need a

    .Quit

    to quit Word. In the line that saves the document, you may need to use one of your variables. I haven't automated Word but I have automated Excel a lot, and it's a known thing that if you refer to the file without the variable it creates a hidden instance of the program.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    close the app. It dont cost nuthin....

    Code:
    Private Sub cmdPrint_Click()
    Dim appWord As Word.Application
    Dim CloseWord As String
    Dim doc As Word.Document
    
    
    'Avoid error 429, when Word isn't open.
    On Error GoTo errHandler:
    
    
         'Set appWord object variable to running instance of Word.
    'Set appWord = GetObject(, "Word.Application")
    '   'If Word isn't open, create a new instance of Word.
    
    
    Set appWord = New Word.Application
    Set doc = appWord.Documents.Open("C:\doc.doc", , True)
    With doc
        .FormFields("txtHName").Result = Me!TXTAN
        .FormFields("txtAddOne").Result = Me!txtAA1
        .FormFields("txtAddTwo").Result = Me!txtAA2
        .FormFields("txtBo").Result = Me!txtBN
        .FormFields("txtPol").Result = Me!txtPN
        .FormFields("txtInv").Result = Me!txtIN
        .FormFields("txtProp1").Result = Me!txtBA1
        .FormFields("txtProp2").Result = Me!txtBA2
        '.FormFields("fldCountry").Result = Me!Country
        '.FormFields("fldPhone").Result = Me!Phone
        '.FormFields("fldFax").Result = Me!Fax
        
        .Visible = True
        .Activate
    End With
    ActiveDocument.SaveAs Filename:="C:\doc" & strNewFolderName & "\" & Me!txtBN & "," & "Letter.doc", FileFormat:=wdFormatDocument
    appWord.Quit
    
    
    Set doc = Nothing
    Set appWord = Nothing
    Exit Sub
    
    
    errHandler:
    MsgBox Err.Number & ": " & Err.Description
    End Sub

  4. #4
    Forbes's Avatar
    Forbes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2017
    Posts
    133
    Playing around with this now, it takes word off the tasklist and worked the first test, but every test after that it's asking if I want to save changes to the original word doc and I click don't save and it doesn't save the new or old. Still messing with it but figured I would pool ideas. If I stop the code manually it works so is that whats keeping the old doc open? it's interesting because I think Word is using its data protection policy to save the data entered on the first run temporarily like it's simulating being forced to close without a save prompt the first time.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Did you try using doc or appWord with your save code?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I tried executing your code .... for me, there were several un-handled errors....but I probably missed something.

    It looks like there is a variable named "strNewFolderName" that is not declared. Maybe it is a global variable??
    I added the declaration and set "strNewFolderName" equal to "Test". Word tried to save the document to Folder "C:\docTest" (which I don't have). You should check to ensure that the folder exists before trying to save a doc to it.

    Also, the calculated file name (in my test) has an error.
    This is what I got using the parameter
    Code:
    FileName:="C:\doc" & strNewFolderName & "\" & Me!txtBN & "," & "Letter.doc"
    Result:
    Code:
    C:\docTest\steve,Letter.doc
    Look close and you will see there is a comma between steve and letter. Windows will not allow you to save a name with a special character in it.

    Maybe change the comma to an underscore??
    Code:
    FileName:="C:\doc" & strNewFolderName & "\" & Me!txtBN & "_" & "Letter.doc"
    Then you would get
    Code:
    C:\docTest\steve_Letter.doc

  7. #7
    Forbes's Avatar
    Forbes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2017
    Posts
    133
    All of the directories were made up when posting the code publicly for security purposes, I don't have any problems with this script other than the fact that after running it the first time every time after it asks if I want to save the changes from the original doc that was modified, office stores that information because the appWord.Quit makes Word think it was shut down the wrong way, so the original doc that must remain not modified stores what was saved to the new doc until someone opens the doc physically and tells word that it doesn't have to save the changes that were made prior to word closing. I need to know how to tell word to not save the original doc somehow so it doesn't store it to the clipboard. Also I think your referring to periods as word allows commas in the file name.


    The Solution was replacing appWord.Quit with doc.Quit as Pbaldy suggested, it worked like a charm, Thank You

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You could also change the extension to ".dot" to make the doc into a template. You cannot overwrite a template. It forces you to save with a new name, which is taken care of by the
    "ActiveDocument.SaveAs"
    line.


    Also I think your referring to periods as word allows commas in the file name.
    Learned something new... I read (but never tried) that couldn't use special characters in file names.
    I had seen where multiple periods had been used in file names, but never commas.

    In any case, using commas or multiple periods in file names (IMHO) is very poor naming practice.



    Good luck with your project.......

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Planning Process
    By bleyden in forum Access
    Replies: 2
    Last Post: 08-09-2016, 07:31 PM
  2. Ask a process amounts
    By azhar2006 in forum Queries
    Replies: 4
    Last Post: 01-14-2014, 01:06 PM
  3. Help with Normalisation Process
    By supermessiah in forum Database Design
    Replies: 2
    Last Post: 02-26-2013, 12:36 PM
  4. Need help to simplify this process
    By shanea.kr in forum Access
    Replies: 1
    Last Post: 07-10-2012, 01:40 PM
  5. Efficient Process?
    By compooper in forum Database Design
    Replies: 1
    Last Post: 06-14-2011, 03:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums