Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2006

Tip: Looking for answers? Try searching our database.

Making a New Document Active

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
felton - 24 May 2006 10:50 GMT
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
Dave Lett - 24 May 2006 16:02 GMT
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
>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
[quoted text clipped - 52 lines]
> =======================================
> Thanks again
felton - 30 May 2006 02:09 GMT
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

> Hi,
> Instead of using ActiveDocument when you have several documents open, I
[quoted text clipped - 106 lines]
> > =======================================
> > Thanks again
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.