Making a New Document Active

F

felton

I have this code from an 'OK' button on a form which simply allows the user
select to create a New Blank Document, or insert other types of files. The
problem area is inserting the new blank document because I need to become the
active document. So whatever other documents are open at the time, they need
to remain open and go to the background, and the new blank document has to
become active.

Can anyone help with the correct code I need to use and where to use it?

Thanks
=======================================
Private Sub btnOK_Click()
Dim Today As String, FirstInit As String, Name As String, Dir As String,
ChangeDir As String
Dim NewDoc As Byte

ChangeDir = Me.lbxProfessionals.Value
Today = " "
FirstInit = ChangeDir
Name = Application.UserInitials
Order = " "
Dir = "G:\" & ChangeDir & "\"
MakeFileName Today, FirstInit, Order, Dir
Name = Dir & Today & "_" & Name & Order & ".doc"
Select Case Choice
Case 0
Application.ChangeFileOpenDirectory "F:\TEMPLATES"
Documents.Add Template:="Document", NewTemplate:=True
Case 1
Case 2
Case Else
End Select
With Application.Dialogs(wdDialogFileSaveAs)
.Format = wdFormatDocument
End With
If ActiveDocument.SaveFormat = wdFormatTemplate Then
ActiveDocument.SaveAs FileFormat:=wdFormatDocument
End If
ActiveDocument.SaveAs FileName:=Name, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
Call CreateFooter
Selection.WholeStory
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove
Unload Me
End
End Sub
=======================================
Thanks again
 
D

Dave Lett

Hi,
Instead of using ActiveDocument when you have several documents open, I
would suggest that you set an explicit reference to the document that you
want to modify/control. I've also replaced your Selection with a Range
object because I wanted an explicit reference to the document that you set a
document reference to.

Dim Today As String, FirstInit As String, Name As String, Dir As String,
ChangeDir As String
Dim NewDoc As Byte
Dim oDoc As Document
Dim oRng As Range

ChangeDir = Me.lbxProfessionals.Value
Today = " "
FirstInit = ChangeDir
Name = Application.UserInitials
Order = " "
Dir = "G:\" & ChangeDir & "\"
MakeFileName Today, FirstInit, Order, Dir
Name = Dir & Today & "_" & Name & Order & ".doc"
Select Case Choice
Case 0
Application.ChangeFileOpenDirectory "F:\TEMPLATES"
Set oDoc = Documents.Add(Template:="Document",
NewTemplate:=True)
Set oRng = oDoc.Range
Case 1
Case 2
Case Else
End Select
With Application.Dialogs(wdDialogFileSaveAs)
.Format = wdFormatDocument
End With
If oDoc.SaveFormat = wdFormatTemplate Then
oDoc.SaveAs FileFormat:=wdFormatDocument
End If
oDoc.SaveAs FileName:=Name, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
Call CreateFooter
With oRng
.Start = .End
.Select
End With
Unload Me
End

HTH,
Dave
 
F

felton

Hi Dave

Thank you for your reply.

Unfortunately your solution is not working either and I think I am not
having any success with either method because the new inserted template
itself was being set as the range or the Active Document. After the new
document is created from the template, it is named and saved. What if the new
named and saved document was set as the range (or the Active Document)? Does
that make sense? How would I do that?

Thanks
Nick
 

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