Word Problem

C

CyndyG

I have created a template with bookmarks. The code runs just fine. THe new
document displays as it should,but after I close it Word continous to run.
The only way I can stop it from running is to go into Task Manager delete the
Word.exe that is still running or bring my system down. Can someone tell me
what I am doing wrong. Here is the code that I have commented out because I
couldn't get it to end Word.exe. At one time I did have close and exit apps
but that didn't work either.

''Private Sub cmdCreateStatementOfWork_Click()
' Const AssociatePickUp As String = "Associates Pick Up"
' Const AssociatePickUp2 As String = "Associate picks up from point"
' Const ArmoredService As String = "Armored Service"
' Const ArmoredService2 As String = "Armored services picks up and notifies
MediaMovement Office(MMO)"
' Const IronMountain As String = "Iron Mountain"
' Const IronMountain2 As String = "Handle pick up and delivery and will
provide statement of work"
' Const AssociateHandles As String = "Associate Handles All Travel"
' Const AssociateHandles2 As String = "Travels with media from point of
origin"
' Const Aviation As String = "Aviation"
' Const Aviation2 As String = "General aviation"
' Const Corporate As String = "Corporate Aviation"
' Const Corporate2 As String = "Corporate acquired aircraft is in route"
' Const ArmoredServiceDestination As String = "Armored Service Destination"
' Const ArmoredServiceDestination2 As String = "Destination - Use Armored
service to meet aircraft"
' Const AssociateDelivers As String = "Associate Delivers"
' Const AssociateDelivers2 As String = "Associate delivers to destination"
'
'Dim rst As DAO.Recordset
'Dim objDoc As Object
'Dim objDoc2 As Object
'Dim strTab As String
'Dim strLFCR As String
'Dim wd As Word.Application
'Dim myDoc As Word.Document
'Dim str1 As String
'Dim str2 As String
'strTab = Chr$(9)
'strLFCR = Chr$(13)
'
'Set rst = Me.Recordset
'
'
' Set wd = New Word.Application
' wd.Documents.Add ("C:\Test\StatementofWork.dot")
'' wd.Documents.Add ("C:\Test\StatementofWork.dot")
' Set myDoc = wd.ActiveDocument
' With wd.Selection
' .GoTo wdGoToBookmark, Name:="strCaseID"
' .TypeText Text:=Nz(rst.Fields("strCaseID"))
' .GoTo wdGoToBookmark, Name:="strRequestorName"
' .TypeText Text:=Nz(rst.Fields("strRequestorName"))
' .GoTo wdGoToBookmark, Name:="strRequestorPhone"
' .TypeText Text:=Nz(rst.Fields("strRequestorPhone"))
' .GoTo wdGoToBookmark, Name:="lngPiecesMoved"
' .TypeText Text:=Nz(rst.Fields("lngPiecesMoved"))
' .GoTo wdGoToBookmark, Name:="lngUnitsMoved"
' .TypeText Text:=Nz(rst.Fields("lngUnitsMoved"))
' .GoTo wdGoToBookmark, Name:="strEncrypted"
' .TypeText Text:=Nz(rst.Fields("strEncrypted"))
' .GoTo wdGoToBookmark, Name:="strDERS"
' .TypeText Text:=Nz(rst.Fields("strDERS"))
' .GoTo wdGoToBookmark, Name:="ysnISER"
' .TypeText Text:=Nz(rst.Fields("ysnISER"))
' .GoTo wdGoToBookmark, Name:="strLeavingCity"
' .TypeText Text:=Nz(rst.Fields("strLeavingCity"))
' .GoTo wdGoToBookmark, Name:="strArrivingCity"
' .TypeText Text:=Nz(rst.Fields("strArrivingCity"))


'If Me.ysnAssociatePickUp = True Then
' str1 = AssociatePickUp & strTab & AssociateHandOff2 & strLFCR
' End If

'If Me.ysnArmoredService = True Then
' str1 = str1 & ArmoredService & strTab & ArmoredService2 & strLFCR
' End If

' If Me.ysnIronMountain = True Then
' str1 = str1 & IronMountain & strTab & IronMountain2 & strLFCR
' End If

' If Me.ysnAssociateHandles = True Then
' str1 = str1 & AssociateHandles & strTab & AssociateHandles2 & strLFCR
' End If

'If Me.ysnAviation = True Then
' str1 = str1 & Aviation & strTab & Aviation2 & strLFCR
' End If
'If Me.ysnCorporate = True Then
' str1 = str1 & Corporate & strTab & Corporate2 & strLFCR
' End If

' If Me.ysnArmoredServiceDestination = True Then
' str1 = str1 & ArmoredServiceDestination & strTab &
ArmoredServiceDestination2 & strLFCR
' 'End If

' objDoc.Bookmarks("VariableText1").Range.Text = str1

'If Me.ysnAssociateDelivers = True Then
' str2 = AssociateDelivers & strTab & AssociateDelivers2 & strLFCR
' End If

'objDoc.Bookmarks("VariableText2").Range.Text = str2

' objDoc.Parent.Visible = True
' objDoc.Parent.Activate
'Set objDoc = Nothing

'End With
'wd.Visible = True
'End Sub
 
J

John Nurick

Hi Cyndy,

I'm not sure what you're trying to achieve, but normally I'd probably do
something like this:

Dim wdApp as Word.Application
Dim docD As Word.Document
...

Set wdApp = CreateObject(,"Word.Application")
Set docD = wdApp.Documents.Add("C:\Test\StatementofWork.dot")

'Do stuff with docD
...

'If desired, save it and close it
docD.Save "C:\Test\SavedFile.doc"
docD.Close
Set DocD = Nothing

'Otherwise, close without saving
docD.Close False
Set DocD = Nothing

'If there's any possibility that other documents will have
'been opened, use something like this to make sure they close
Do While wdApp.Documents.Count > 0
wdApp.Documents(1).Close False
Loop

'Quit Word
wdApp.Quit
Set wdApp = Nothing

When you're automating Word (or Excel), it's best to avoid using the
Selection object where possible. So instead of navigating to a bookmark
and using TypeText, just use

docD.Bookmarks("MyBookmark").Range.Text = _
Nz(rst.Fields("MyField"), "NULL VALUE")

I've supplied a substitute value for Nz() because its default, zero,
didn't seem appropriate.
 
C

CyndyG

Thanks again John

John Nurick said:
Hi Cyndy,

I'm not sure what you're trying to achieve, but normally I'd probably do
something like this:

Dim wdApp as Word.Application
Dim docD As Word.Document
...

Set wdApp = CreateObject(,"Word.Application")
Set docD = wdApp.Documents.Add("C:\Test\StatementofWork.dot")

'Do stuff with docD
...

'If desired, save it and close it
docD.Save "C:\Test\SavedFile.doc"
docD.Close
Set DocD = Nothing

'Otherwise, close without saving
docD.Close False
Set DocD = Nothing

'If there's any possibility that other documents will have
'been opened, use something like this to make sure they close
Do While wdApp.Documents.Count > 0
wdApp.Documents(1).Close False
Loop

'Quit Word
wdApp.Quit
Set wdApp = Nothing

When you're automating Word (or Excel), it's best to avoid using the
Selection object where possible. So instead of navigating to a bookmark
and using TypeText, just use

docD.Bookmarks("MyBookmark").Range.Text = _
Nz(rst.Fields("MyField"), "NULL VALUE")

I've supplied a substitute value for Nz() because its default, zero,
didn't seem appropriate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top