Private Sub cmdPrintCCUForm_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
On Error GoTo errHandler
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
'Set appWord = CreateObject("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:\Users\arrudad\Documents\AutoForms\CCUform.docx", , True)
With doc
.FormFields("fldRank").Result = Nz(Me![Rank])
.FormFields("fldFocus").Result = Nz(Me![Focus First Name] & "" & " " & "" & [Focus Last Name])
.FormFields("fldFDID").Result = Nz(Me![Focus FDID])
.FormFields("fldBattAssignUnit").Result = Nz(Me![Battalion] & "/" & " " & [Focus Assignment] & "/" & " " & [Focus Unit])
.FormFields("fldInvestigator").Result = Nz(Me![Case Investigator])
.FormFields("fldCaseNumber").Result = Nz(Me![CaseNumber])
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
If Err.Number = 429 Then
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description
End If
End Sub
Set appWord = GetObject(, "Word.Application")
'Set appWord = CreateObject("word.Application")
Set appWord = New Word.Application
Set doc = appWord.Documents.Add
What object are you trying to create? Whatever it is, I don't know why failing to create it would corrupt the database. Regardless, it never hurts to run a Compact and Repair on a regular basis, and may be even have it run automatically, either on exit or by a VBA process.
'---------------------------------------------------------------------------------------
' Procedure : IsExeRunning
' Author : James Barash(AccessD)
' Created : 3/17/2009
' Purpose : To determine if a specific program is currently running.
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: The name of the program
' Dependency: N/A
'------------------------------------------------------------------------------
'
Public Function IsExeRunning(strExeName As String) As Boolean
Dim objProcesses As Object, objProcess As Object
IsExeRunning = False
Set objProcesses = GetObject("winmgmts://" & Environ$("ComputerName") _
& "/root/cimv2").ExecQuery("select * from Win32_Process")
If Not objProcesses Is Nothing Then
For Each objProcess In objProcesses
If objProcess.name = strExeName Then
IsExeRunning = True
Exit For
End If
Next
Set objProcess = Nothing
Set objProcesses = Nothing
End If
End Function
If IsEXERunning("WORD.EXE") Then
Set appWord = GetObject(, "Word.Application")
else
Set appWord = CreateObject("word.Application")
end if
'---------------------------------------------------------------------------------------
' Procedure : jtestit
' Author : Jack
' Created : 3/17/2009
' Purpose : Test routine to ensure IsEXERunning function is working
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'------------------------------------------------------------------------------
'
Sub jtestit()
Dim smyEXE As String
smyEXE = "msaccess.exe"
Debug.Print "Is " & smyEXE & " running: " & IsExeRunning(smyEXE)
End Sub
'------------------------------------------------------------------------------------
' Procedure : cmdPrintCCUForm_Click
' Author : JDraw
' Date : 04-04-2013
' Purpose : Added the IsExeRunning function to test if WORD is runninig
' If Word Is Running then use it (getObject)
' If Word Is Not Running then create it (CreateObject)
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'--------------------------------------------------------------------------
'
Private Sub cmdPrintCCUForm_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
On Error GoTo errHandler
'**** original code
'This code has been commented
''Set appWord object variable to running instance of Word.
' Set appWord = GetObject(, "Word.Application")
'Set appWord = CreateObject("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
'**** end original code
'************ code jdraw **************
'
'This new code to test if Word is Running
'
If IsExeRunning("WORD.EXE") Then
Set appWord = GetObject(, "Word.Application")
Else
Set appWord = CreateObject("word.Application")
End If
'
' ***** end of code jdraw
Set doc = appWord.Documents.Open("C:\Users\arrudad\Documents\AutoForms\CCUform.docx", , True)
With doc
.FormFields("fldRank").result = Nz(Me![Rank])
.FormFields("fldFocus").result = Nz(Me![Focus First Name] & "" & " " & "" & [Focus Last Name])
.FormFields("fldFDID").result = Nz(Me![Focus FDID])
.FormFields("fldBattAssignUnit").result = Nz(Me![Battalion] & "/" & " " & [Focus Assignment] & "/" & " " & [Focus Unit])
.FormFields("fldInvestigator").result = Nz(Me![Case Investigator])
.FormFields("fldCaseNumber").result = Nz(Me![CaseNumber])
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
If Err.number = 429 Then
Resume Next
Else
MsgBox Err.number & ": " & Err.Description
End If
End Sub
Sorry for the general question.
It is with the code to generate a word from from access.
Code:Private Sub cmdPrintCCUForm_Click() Dim appWord As Word.Application Dim doc As Word.Document On Error GoTo errHandler 'Set appWord object variable to running instance of Word. Set appWord = GetObject(, "Word.Application") 'Set appWord = CreateObject("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:\Users\arrudad\Documents\AutoForms\CCUform.docx", , True) With doc .FormFields("fldRank").Result = Nz(Me![Rank]) .FormFields("fldFocus").Result = Nz(Me![Focus First Name] & "" & " " & "" & [Focus Last Name]) .FormFields("fldFDID").Result = Nz(Me![Focus FDID]) .FormFields("fldBattAssignUnit").Result = Nz(Me![Battalion] & "/" & " " & [Focus Assignment] & "/" & " " & [Focus Unit]) .FormFields("fldInvestigator").Result = Nz(Me![Case Investigator]) .FormFields("fldCaseNumber").Result = Nz(Me![CaseNumber]) .Activate End With Set doc = Nothing Set appWord = Nothing Exit Sub errHandler: If Err.Number = 429 Then Resume Next Else MsgBox Err.Number & ": " & Err.Description End If End Sub
The code works trapping the 429 error, but I didnt know if over time that the db would become corrupt.
If I remove thepart and replaced it withCode:Set appWord = GetObject(, "Word.Application")but get "object not set"Code:'Set appWord = CreateObject("word.Application")
I read whereshould not be used as this could create the 429 error.Code:Set appWord = New Word.Application
Not sure what to use in its place
?Code:Set doc = appWord.Documents.Add
Yes, that is what I was thinking, but was not 100% sure. Yes, I have the db to "Compact and Repair" on exit.
Private Sub cmdPrintCCUForm_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
On Error [B]Resume Next[/B]
'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 = CreateObject("word.Application")
Err.Clear
End If
Set objProcesses = GetObject("winmgmts://" & Environ$("GB03300400005") _
& "/root/cimv2").ExecQuery("SELECT * from Win32_Process")
This is the standard way of creating the Word object if no instance of Word is available. You do not need anything else to determine if Word is running. The error (of not getting the nonexistent object) is trapped and handled. After this If block, you may set the On Error to go to you your error handler.
Jiri,
I originally had this code setup the way you described with the exception of another errror handling. I did not realize that I could use 2 seperate error handlers in a code block.
Thanks for the reply and assistance.