Re: Open User Form from Template and populate message

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Jason,

first, I know one exception when my sample doesn´t work: The code won´t
be executed if OL is not running already and you´re opening the template
from within the file Explorer.


The OL Object Modell is in fact something different to e.g. Word`s.
That´s no limitation, but regards the different data structure.

For attaching code to a file you´d need to customize the Mail Form in
your case. Then you can open a script editor and insert some VBS code.
This customized form you can save as an *.oft template.

I´ve searched for you in the VBA help. Please see the sample for the
Open event. It works in VBS, too.

*AFAIK* you can´t run an UserForm from within a CustomForm directly. If
I´m right, then you could add a public method to ThisOutlookSession,
which
you can call from the customized form. From within this method the
UserForm can be called.

Some more explanations to my sample:

Private Sub Application_Startup()
Set Inspectors = Application.Inspectors
End Sub

This method must run once. It´s called from OL automatically while
startup. I.e., after writing this code (or after modifying any other
code in your project) you need to re-start OL.

It´s also the reason for the exception mentioned on top: Without having
the Inspectors variable set already, you won´t get any of its event.

Private Sub Inspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If Inspector.CurrentItem.Subject = TEMPLATE_SUBJECT Then
TSGRequest.Show 1
End If
End Sub

After execution of the code in Application_Startup this event procedure
will be executed every times when an Inspector is going to be opened.
For recognizing whether it´s your template or not the CurrentItem´s
Subject will be checked. The '=' operator is case-sensitive, i.e.
TSGRequest.Show 1 is called only when the Subject is absolutely equal to
the value stored in TEMPLATE_SUBJECT.

Due to TSGRequest shall be shown modal the code execution in this event
will be stopped. I.e. the Inspector will be displayed not before
TSGRequest is unloaded.

So I´m sure if:

- macros are enabled at all (security settings)
- and you have followed my suggested steps (re-start OL after coding)

then the events will be executed. If in addition:

- the template´s subject is *equal* to TEMPLATE_SUBJECT (="TSG Dispatch
Center
<TxtBxCenter>" in your case) then the UserForm will be shown.

HTH

--
Viele Grüße / Best regards
Michael Bauer - MVP Outlook


"Tahlmorrah" <Tahlmorrah@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0DAD8E11-FB03-4B9D-897C-33F3507A327A@xxxxxxxxxxxxxxxx
> My turn to be confused. Did exactly as requested. From Outlook pressed
> Alt+F11 it opened Project1(VbaProject.OTM) with the exact code listed
below.
>
> I do not have the tools to work in VBS, so I am working in VBA.
>
> The "limitations" that I am speaking about is that in other Microsoft
Office
> products I can tie code to the document I am working on (i.e. I can
have
> different code for one Word template to the next, same for Excel, and
of
> course for Access) that only affects that document, but Outlook does
not seem
> to function the same way. Therefore I believe, maybe incorrectly, that
> Outlook is more limited than the previously listed Microsoft products.
>
> Jason
>
> "Michael Bauer" wrote:
>
> > Wow, I begin to feel like someone in the German TV Show "Hidden
> > camera"... Anyway, I try it again .-)
> >
> >
> > > The Template named TSG Dispatch.oft has the following VBA code
> > attached to it:
> > > <Microsoft Outlook Objects>
> > > <This Outlook Session>
> >
> > The code behind an *.oft template isn´t VBA, but VBS. And what I
gave to
> > you is VBA. That means, it isn´t working in VBS, i.e. also isn´t
> > working behind an *.oft template.
> >
> > Please open the VBA environment (ALT+F11) and display the Project
> > Explorer (STRG+R). The modul "ThisOutlookSession" belongs already to
the
> > project. This is the modul where the code needs to be which I have
> > included in the <ThisOutlookSession> tags. To this project you can
add
> > the additional standard modul with the Const declarations.
> >
> > To clarify that: It could be perfectly possible that there is also a
way
> > for just using VBS behind the template without any VBA - but I don´t
> > know that. If you want to try that VBS way then, please, throw my
sample
> > away and forget it - believe me, it won´t work and that´s not a
point of
> > any security settings.
> >
> > BTW: What "limitations" you´re talking about?
> >
> > --
> > Viele Grüße / Best regards
> > Michael Bauer - MVP Outlook
> >
> >
> > "Tahlmorrah" <Tahlmorrah@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> > news:2178A7B0-B204-427B-BDAB-139EFEA22797@xxxxxxxxxxxxxxxx
> > > Thank you for letting me know that I had to have those items in
> > > ThisOutlookSession. I do not consider that secure at all, but we
can
> > only
> > > work within the limitations Microsoft has given us.
> > >
> > > The Template named TSG Dispatch.oft has the following VBA code
> > attached to it:
> > > <Microsoft Outlook Objects>
> > > <This Outlook Session>
> > >
> > > Option Explicit
> > > Private WithEvents Inspectors As Outlook.Inspectors
> > >
> > > Private Sub Application_Startup()
> > > Set Inspectors = Application.Inspectors
> > > End Sub
> > >
> > > Private Sub Inspectors_NewInspector(ByVal Inspector As
> > Outlook.Inspector)
> > > If Inspector.CurrentItem.Subject = TEMPLATE_SUBJECT Then
> > > TSGRequest.Show 1
> > > End If
> > > End Sub
> > > </This Outlook Session>
> > > </Microsoft Outlook Objects>
> > >
> > > <Forms>
> > > <TSGRequest>
> > >
> > > Private Sub CmdBtnSubmit_Click()
> > > Set oMail = Application.ActiveInspector.CurrentItem
> > >
> > > ReplaceCenter = "<TxtBxCenter>"
> > > ReplaceHO = "<TxtBxHO>"
> > > ReplaceHC = "<TxtBxHC>"
> > > ReplaceTZ = "<TxtBxTZ>"
> > > ReplaceTech = "<TxtBxTech>"
> > > ReplacePhone = "<TxtBxPhone>"
> > > ReplaceSev = "<TxtBxSev>"
> > > ReplaceReason = "<TxtBxReason>"
> > > ReplaceDate = "<TxtBxDate>"
> > > ReplaceIssue = "<TxtBxIssue>"
> > >
> > > oMail.Subject = Replace(oMail.Subject, ReplaceCenter,
> > TxtBxCenter.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceCenter,
TxtBxCenter.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceHO, TxtBxHO.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceHC, TxtBxHC.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceTZ, TxtBxTZ.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceTech, TxtBxTech.Text)
> > > oMail.Body = Replace(oMail.Body, ReplacePhone,
TxtBxPhone.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceSev, TxtBxSev.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceReason,
TxtBxReason.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceDate, TxtBxDate.Text)
> > > oMail.Body = Replace(oMail.Body, ReplaceIssue,
TxtBxIssue.Text)
> > >
> > > Unload TSGRequest
> > > End Sub
> > >
> > > Private Sub CmdBtnClear_Click()
> > > TxtBxCenter = ""
> > > TxtBxHO = ""
> > > TxtBxHC = ""
> > > TxtBxTZ = ""
> > > TxtBxTech = ""
> > > TxtBxPhone = ""
> > > TxtBxSev = ""
> > > TxtBxReason = ""
> > > TxtBxDate = ""
> > > TxtBxIssue = ""
> > > End Sub
> > >
> > > Private Sub TSGRequest_Activate()
> > > TxtBxDate.Text = DateAdd("d", 1, Date)
> > >
> > > End Sub
> > >
> > > </TSGRequest>
> > > </Forms>
> > >
> > > <Modules>
> > > <Module1>
> > >
> > > Public Const TEMPLATE_SUBJECT As String = "TSG Dispatch Center
> > <TxtBxCenter>"
> > > Public Const REPLACE_SUBJECT As String = "<TxtBxCenter>"
> > >
> > > </Module1>
> > > </Modules>
> > >
> > > Problem: Userform:TSGRequest does not automatically open when
template
> > TSG
> > > Dispatch.oft is opened. Pretty sure I need a load or activate
command
> > > referancing userform:TSGRequest somewhere in the code so that
when
> > the .oft
> > > is opened it pops up, but I am unsure of the command or where it
> > should be
> > > placed.
> > >
> > > Believe or not this has been helpful. I get bits and pieces here
and
> > there
> > > that help improve my knowledge of VBA and how it relates to
Outlook,
> > and as
> > > you can see parts that help improve my code.
> > >
> > > Jason
> > >
> > >
> > > "Michael Bauer" wrote:
> > >
> > > > Hi Jason,
> > > >
> > > > sorry, I understand less and less. The sample I´ve given to you
must
> > be
> > > > executed in VBA, modul "ThisOutlookSession". You can´t move this
> > code
> > > > into your *.oft template.
> > > >
> > > > Whether you´re using VBScript in your template or not, because
> > you´re
> > > > also using an UserForm you have to enable VBA anyway.
> > > >
> > > > Or do you mean your *.oft template when talking about an
UserForm?
> > If so
> > > > then it seems that you´d need help for VBScript, not VBA. That´s
not
> > my
> > > > area.
> > > >
> > > > (IMHO VBA is more secure than using scripting in an template.
It´s
> > not
> > > > easy to change or manipulate the project file (*.otm) - but very
> > easy to
> > > > change your *.oft file.)
> > > >
> > > > --
> > > > Viele Grüße / Best regards
> > > > Michael Bauer - MVP Outlook
> > > >
> > > >
> > > > "Tahlmorrah" <Tahlmorrah@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
> > > > news:C1D9843D-59B1-4579-A144-BAF1300FEE30@xxxxxxxxxxxxxxxx
> > > > > > Not sure, what you´re saying.
> > > > >
> > > > > I do not care that my security settings ask me to enable
macros.
> > This
> > > > is
> > > > > working as intended. I only want it to prompt when this .oft
is
> > opened
> > > > as it
> > > > > is more secure. It appears that I need to add something that
is
> > local
> > > > to the
> > > > > .oft rather than ThisOutlookSession. I only used
> > ThisOutlookSession as
> > > > I do
> > > > > not know how to accomplish this so it is only contained in the
> > ..oft.
> > > > >
> > > > > All security settings are Custom based off of the Medium
setting.
> > > > >
> > > > > The intention is to make a self contained macro in the
template
> > that
> > > > only
> > > > > activates when the template opens. Having macros that activate
> > when
> > > > Outlook
> > > > > opens is not an option due to it compromising system security.
> > > > >
> > > > > Here is the desired sequence of events:
> > > > > 1. User opens Outlook and works on it throughout the day
> > > > > 2. User has need of template and opens template by clicking on
the
> > > > ..oft file
> > > > > 3. Prompted to enable macros in the .oft
> > > > > 4. Userform is displayed
> > > > > 5. Completes userform and Submits
> > > > > 6. Text on Template is changed with the entered information
> > > > > 7. User can make additional changes as necessary
> > > > > 8. E-mail is sent
> > > > > 9. User continues to use Outlook as normal
> > > > > 10. User has need of template and opens template
> > > > > 11. See step 3.
> > > > >
> > > > > > This is not what I´ve suggested.
> > > > > I understand that is not what you have suggested, but this is
the
> > only
> > > > way I
> > > > > have been able to get the userform to even show. If you can
tell
> > me
> > > > how to
> > > > > get the userform to show when the template opens, I will be
more
> > than
> > > > glad to
> > > > > change the code. This is my primary problem at this time.
> > > > >
> > > > > Jason
> > > > >
> > > > > "Michael Bauer" wrote:
> > > > >
> > > > > > > Started to get promted for macros after Outlook after
closed
> > and
> > > > > > reopen
> > > > > > > Outlook. If open template at that point, prompts for
macros
> > and
> > > > opens
> > > > > > > userform when changed
> > > > > >
> > > > > > Not sure, what you´re saying.
> > > > > >
> > > > > > As I mentioned earlier, you need to restart OL after making
> > changes
> > > > to
> > > > > > the code or run Application_Startup manually. The execution
of
> > this
> > > > > > function is important. If it´s done once and you don´t make
more
> > > > changes
> > > > > > to the code then OL can stay opened till the end of the
days.
> > > > > >
> > > > > > > Private Sub Application_Startup()
> > > > > > > Set Inspectors = Application.Inspectors
> > > > > > > TSGRequest.Show 1
> > > > > >
> > > > > > This is not what I´ve suggested.
> > > > > >
> > > > > > --
> > > > > > Viele Grüße / Best regards
> > > > > > Michael Bauer - MVP Outlook
> > > > > >
> > > > > >
> > > > > > "Tahlmorrah" <Tahlmorrah@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
> > message
> > > > > > news:97784E51-2A21-454D-AEB2-8C402C9910BB@xxxxxxxxxxxxxxxx
> > > > > > > Started to get promted for macros after Outlook after
closed
> > and
> > > > > > reopen
> > > > > > > Outlook. If open template at that point, prompts for
macros
> > and
> > > > opens
> > > > > > > userform when changed
> > > > > > >
> > > > > > > <ThisOutlookSession>
> > > > > > > Private Sub Application_Startup()
> > > > > > > Set Inspectors = Application.Inspectors
> > > > > > > TSGRequest.Show 1
> > > > > > > End Sub
> > > > > > > </ThisOutlookSession>
> > > > > > >
> > > > > > > However this is not accomplishing what I need to. Users
need
> > to
> > > > keep
> > > > > > Outlook
> > > > > > > open at all times. When this template is selected they
need
> > the
> > > > > > userform to
> > > > > > > open automatically on top of the template.
> > > > > > >
> > > > > > > Progress is slow, but its progress
> > > > > > > Thanks again
> > > > > > > Jason
> > > > > > >
> > > > > > >
> > > > > > > "Tahlmorrah" wrote:
> > > > > > >
> > > > > > > > Never hitting the breakpoint even with Outlook closed.
> > > > > > > >
> > > > > > > > Jason
> > > > > > > >

.



Relevant Pages

  • Re: Open User Form from Template and populate message
    ... so maybe the difficulty is in the method of opening. ... With Outlook running I am opening the .oft from file Explorere. ... template is opened. ... > After execution of the code in Application_Startup this event procedure ...
    (microsoft.public.outlook.program_vba)
  • Re: Ribbon Customizatin AddIn
    ... idea ultimately allows the user to edit the template. ... Private myArrayPriAs String ... Sub Onload ... Dim pUserInt As String ...
    (microsoft.public.word.vba.general)
  • Re: Load Data Into ListBox1
    ... Sub AutoNew() ... template, when you select New from the File menu and then select the ... ListBox1, then choose the row I want to insert into the word document, ... ListBox1 opens, but no data is displayed inside ...
    (microsoft.public.word.vba.general)
  • Re: How to call macros and userforms from another template
    ... > UserForm: frmAddress ... > sub MyUserFormShow() ... > To run this from another template, regardless of the location of your ... >> Dim objAtE As AutoTextEntry ...
    (microsoft.public.word.vba.general)
  • Re: Is there a way to get color to appear when you click in a cell in an MS Word 2003 table?
    ... box at the bottom of the dialog to "Document Template ". ... Private WithEvents wdApp As Word.Application ... Private Sub Document_New ... Private Sub Document_Open ...
    (microsoft.public.word.formatting.longdocs)