Hi, I've been tearing my hair out of this and I just can't get it right (bit of a VBA newbie!).
I've created a form for a word template which allows the user to type/select an address and then place this data onto the word template. Ok I feel I've done most of the hard work, I can access and select addresses from the address book and it works well. The problem I have is that when the new document is created I can't get the active (focus?) document on screen to be my new template file .... Word just remains on the blank 'document 1'.
I've looked at all the following options
Windows("document2".activate
activedocument.activate and other variations.
I'd be very grateful of any assistance on how to get my new document on the screen!
Here's the code that works (without all my attempts to get away from the evil blank document 1!!!)
'Get username from Address Book
Private Sub Cmdaddbook_Click()
Do
sFirstname = Application.GetAddress _
(AddressProperties:="<PR_DISPLAY_NAME>", _
Displayselectdialog:=1)
Loop Until sFirstname <> ""
txtTo = sFirstname
Dim sUsername As String
sUsername = Application.UserName
txtfrom = sUsername
End Sub
' This section opens the template file and dumps the variables in replace of the bookmarks
Private Sub cmdok_Click()
Dim sUserTemplates As String
'Path for User Templates
sUserTemplates = Options.DefaultFilePath(wdUserTemplatesPath)
'Create memo Coversheet
Set objNewDoc = Documents.Add(sUserTemplates & "\memTemplate.doc"
Dim rng As Range
Set rng = ActiveDocument.Bookmarks("To".Range
rng.InsertAfter txtTo
Set rng = ActiveDocument.Bookmarks("From".Range
rng.InsertAfter txtfrom
Set rng = ActiveDocument.Bookmarks("Subject".Range
rng.InsertAfter txtSubject
'ActiveDocument.SaveAs FileName:="c:\temp\letters\Memo - " & txtTo & " - " & txtSubject & " - " & Format(Now(), "dd-mm-yyyy" & " " & Format(Now(), "h.m.s" & ".doc"
ActiveDocument.Activate
Selection.GoTo What:=wdGoToBookmark, name:="Content"
Unload Me
End Sub
I've created a form for a word template which allows the user to type/select an address and then place this data onto the word template. Ok I feel I've done most of the hard work, I can access and select addresses from the address book and it works well. The problem I have is that when the new document is created I can't get the active (focus?) document on screen to be my new template file .... Word just remains on the blank 'document 1'.
I've looked at all the following options
Windows("document2".activate
activedocument.activate and other variations.
I'd be very grateful of any assistance on how to get my new document on the screen!
Here's the code that works (without all my attempts to get away from the evil blank document 1!!!)
'Get username from Address Book
Private Sub Cmdaddbook_Click()
Do
sFirstname = Application.GetAddress _
(AddressProperties:="<PR_DISPLAY_NAME>", _
Displayselectdialog:=1)
Loop Until sFirstname <> ""
txtTo = sFirstname
Dim sUsername As String
sUsername = Application.UserName
txtfrom = sUsername
End Sub
' This section opens the template file and dumps the variables in replace of the bookmarks
Private Sub cmdok_Click()
Dim sUserTemplates As String
'Path for User Templates
sUserTemplates = Options.DefaultFilePath(wdUserTemplatesPath)
'Create memo Coversheet
Set objNewDoc = Documents.Add(sUserTemplates & "\memTemplate.doc"
Dim rng As Range
Set rng = ActiveDocument.Bookmarks("To".Range
rng.InsertAfter txtTo
Set rng = ActiveDocument.Bookmarks("From".Range
rng.InsertAfter txtfrom
Set rng = ActiveDocument.Bookmarks("Subject".Range
rng.InsertAfter txtSubject
'ActiveDocument.SaveAs FileName:="c:\temp\letters\Memo - " & txtTo & " - " & txtSubject & " - " & Format(Now(), "dd-mm-yyyy" & " " & Format(Now(), "h.m.s" & ".doc"
ActiveDocument.Activate
Selection.GoTo What:=wdGoToBookmark, name:="Content"
Unload Me
End Sub