Activating correct document after closing a document

  • Thread starter Stephen English
  • Start date
S

Stephen English

I create a document from a template (docActive) and this template has a lot
of code behind it.
The code creates another document (docTarget)
The last line of the code closes docActive and the active document becomes
the first document in the collection (i.e a document that was open before I
created docActive) whereas I want it to be docTarget.
I have tried putting docTarget.activate prior to closing docTarget but
didn't work.

Any ideas please?
Stephen
 
D

Doug Robbins - Word MVP

Show us the code, particularly the part where doctarget is created. However
you might try using docTarget.Activate before you use docActive.Close or is
that what you meant to say that you were doing when you said you had "tried
putting docTarget.activate prior to closing docTarget "

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Stephen English

Hi Doug
Sorry I meant "tried putting docTarget.activate prior to closing docActive"
Code below
Thanks
Stephen

Private Sub cmdSaveDetails_Click()
Dim docActive As Document
' This is called from a Userform on a template
' Depending on which document the user selects from a combo box, docTarget
becomes the selected document
Set docActive = ActiveDocument
CreateDocument
' Next statement must be last line of sub because this code is in it!
docActive.Close False
End Sub


Sub CreateDocument()
Dim docTarget As Document
Dim strTitle As String

Select Case Me.cboWhatToDo
Case "OHS4", "OHS5"
Set docTarget = Documents.Add(Me.txtPath &
Replace(Me.cboDocuments.Value, " ", ""))

strDocName = "WSI-" & Left(Me.cboDocuments, Len(Me.cboDocuments)
- 4) & "_" & Format(dtTemp, "ddmmyyyy") & ".doc"

docTarget.SaveAs "C:/Temp/" & strDocName ' Saved to temp so it
gets a good name for Dataworks
docTarget.Activate
docTarget.AttachedTemplate = Me.txtPath & "StandardTemplate.dot"
strTitle = "Chemical Risk Assessment - " & Me.cboDept.Value & " - " &
Me.cboDiv.Value & " - "
docTarget.Protect wdAllowOnlyFormFields
Case Else
End Select

docTarget.BuiltInDocumentProperties(wdPropertyAuthor) =
frmProperties.txtAuthor
docTarget.BuiltInDocumentProperties(wdPropertyKeywords) =
frmProperties.txtKeywords
docTarget.BuiltInDocumentProperties(wdPropertyTitle) = strTitle

strMsg = "I'm finished creating the background properties" & vbCrLf & _
"Please click OK and proceed"
MsgBox strMsg, vbOKOnly

Unload frmProperties

End Sub
 
D

Doug Robbins - Word MVP

Why don't you just close the activedocument (DocActive) before you create
the other document.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

I do not believe so.

If I create a template with a userform containing a command button which has
the following code in its Click event

Private Sub CommandButton1_Click()
Dim newdoc As Document
ActiveDocument.Close
Set newdoc = Documents.Add
newdoc.Range.Text = "This is the new document"
Me.Hide
End Sub

When I click on that button, the activedocument is closed and a new document
is created into which the words "This is the new document" are inserted.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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