PDA

View Full Version : Solved: 2 templates act on same doc



burtburt
04-26-2008, 11:13 AM
I am working on a project that has 2 user types (Engineer and Tech). The Engineer creates the document and has tools specific to that role. There is a "Rework" section of the document in which the engineer explains how to fix or upgrade the product. However, they only want to show how to incrimentally move from the last fix to the newest one.

The Tech users will open this document and read the rework instructions. However, they want a consolidated instruction based on the product version they have in front of them. For example, if there is rework from version 1 to 2, 2 to 3, 3 to 4 in the doc and they have version 2 in front of them. They want the instructions from 2 to 4.

So, I'm planning on making a dot that has the code for the Engineer. A menu structure to create the docs, add info etc.

But I want a seperate menu structure for the tech that does not have the Engineers tools in it. I only want the tech to have the code that extracts and compiles the rework. That is, I don't want them changing the document by executing code in the Engineering tools menu by mistake.

Can I have two templates that act on the same doc? Having the Engineers install one dot and the Techs another dot? What's the best approach for this? One template with menu structures that can be turned on and off at dot install? I'm struggling with the best approach.

Any ideas? I'm hoping this is clear.

burtburt
04-26-2008, 11:24 AM
BTW. I've been reading this forum for years now and I've learned tons. I just want to say to fumei and all the other regular posters that I'm a big fan and I consider what you guys do here as great humanitarian work. You may not be directly saving lives, but your saving jobs and time every day. THANK YOU!

lucas
04-26-2008, 11:28 AM
If you had two menu's one for Engineers and one for techs would they respect each other and only use the one that is for their need or is there a problem with security that way?

Re your post @ #2: That is welcome feedback Burt. I'm sure contributors will be glad to hear that their efforts are appreciated.

burtburt
04-26-2008, 12:17 PM
I guess you could say there is a security issue. I would just rather it be clean and not give anyone an option that they shouldn't use.

I was thinking of having the tech code embedded in the docs that the Engineering dot generates. But after reading here (http://www.vbaexpress.com/forum/showthread.php?t=15066&highlight=template), that doesn't seem possible. Especially with the Tech form I created for the rework from version -> to version.

fumei
04-28-2008, 04:09 AM
"Can I have two templates that act on the same doc? "

Have to very careful with terminology here. The clean answer is...yes, you can.

The problem is the word "act".

You can have dozens of templates "act" on a document....if you want to be so inclined.

However, a document can only have ONE template attached.

Not knowing the details, one possible solution is to NOT have any code.

You would have the Engineer's code in a separate .dot file loaded as a global. It would have all the menu stuff they need.

The Tech's would have a separate .dot file loaded as a global. It would have their code. The Tech do not have the Engineer's global. They have their own.

This way there would be separate .dot files acting as code containers (which is what globals are for). So then, yes, there would be different templates "acting" on the document.

burtburt
04-28-2008, 04:01 PM
Thank you fumei. I didn't even know there was a "global" setting for templates. That explains why I was having such trouble with my "Create New" subroutine. i couldn't run it unless a document made with the .dot was already open. In fact i couldn't run any code in the .dot unless a doc with that template attached was open.

But I digress. I came up with another solution that isn't as elegant but worked (so far at least).

I created a txt file locally that held whether the user was an Engineer or Tech. Then did an if then to build the menu:

Public Sub AutoOpen()
Dim cbpop As CommandBarControl
Dim cbctl As CommandBarControl
Dim cbsub As CommandBarControl
Dim strEPEorTECH As String

Dim UserProfile As String
Dim k As Integer

UserProfile = Environ("USERPROFILE")
fpath = UserProfile & "\Application Data\Cisco\PCAMAP\UserSpecific.txt"

Open fpath For Random As 1

Close 1
Open fpath For Input As 1
strEPEorTECH = Input$(LOF(1), 1)
If strEPEorTECH <> "" Then
k = InStr(1, strEPEorTECH, "UserType=", vbTextCompare)
strEPEorTECH = Mid(strEPEorTECH, k + 9, 1)
Else
Close 1
Open fpath For Output As 1
If MsgBox("Your User Type has not been defined." & _
"Are you an Engineer?", 36, _
"User Type for PCAMAP Menu") = vbYes Then
Write #1, "UserType=1"
strEPEorTECH = "1"
Else
Write #1, "UserType=2"
strEPEorTECH = "2"
End If
End If
Close 1

If MenuItemExists("&PCAMAP") Then
Application.CommandBars("Menu Bar").Controls("&PCAMAP").Delete
End If


Application.DisplayAlerts = False
Application.ScreenUpdating = False

'Is Tech
If strEPEorTECH = 2 Then

Set cbpop = Application.CommandBars("Menu Bar").Controls.Add(Type:=msoControlPopup, Temporary:=True)
cbpop.Caption = "&PCAMAP"
cbpop.Visible = True

' Add the New PCAMAP menu item
Set cbctl = cbpop.Controls.Add(Type:=msoControlButton)
cbctl.Visible = True
' Next is required for caption
cbctl.Style = msoButtonCaption
cbctl.Caption = "Extract Rework"
' Action to perform
cbctl.OnAction = "TechUpgrade"


ElseIf strEPEorTECH = 1 Then

Set cbpop = Application.CommandBars("Menu Bar").Controls.Add(Type:=msoControlPopup, Temporary:=True)
cbpop.Caption = "&PCAMAP"
cbpop.Visible = True
bla bla bla
End If
End Sub


I'm going to research the global setting. If you have any pointers to where I can learn more, I'd love to have them. Thank you!!! I love this site:)

lucas
04-28-2008, 04:46 PM
You should really take the time to re-read Gerry's post at #5. Especially the last couple of paragraphs. He is saying that you can have two different global .dot files. One for one group and one for the other. When they load word the code in their own global template will be available for them to use on any word document they open.

If you create a .dot file with code and menu's and save it to your startup folder then you will be able to run code from it on any word document you open.....

It works the same way as an Excel addin used this way.

burtburt
04-28-2008, 05:06 PM
Maybe my set up is wierd. But I can only execute the code in the .dot when the .doc's with that .dot attached is open.

lucas
04-28-2008, 05:13 PM
I think the keywords are Startup Folder.

Go to tools--Options and then select the file locations tab

Look for the line that says "Startup"

to the right of the word startup will be the location of the directory where you should put your global template.....make sure it is not named normal.dot

put your file there and try it.

fumei
04-28-2008, 08:36 PM
Actually, the keyword is "loaded", or, if you prefer, the loaded word is "loaded".

hardy-har-har

But seriously folks....

While it is commonly thought that "globals" have to be in Startup, this is NOT correct.

Read the word..."Startup".

A global template IN Startup is loaded...on Startup.

A global template can be loaded (and unloaded) dynamically from ANY folder.

It will still be global, i.e. its procedures and toolbars will be available for any document.

"Maybe my set up is wierd. But I can only execute the code in the .dot when the .doc's with that .dot attached is open."

This is correct. This is the way it works. You can only execute code in a .dot file under two situations.

1. a document cloned from that template is open, AND the template file (.dot) is accessible.

If a template is cloned to a new document, and you send that document to someone who does NOT have access to the .dot file...the .dot file's code will NOT be accessible.

2. the .dot file is loaded as an add-in. This can either be automatically loaded when Word....Starts Up; OR loaded dynamically.

Back to your issue. I was going to suggest what you seem to have come up with. IF - and only IF - you can figure out a way to identify Engineer vs. Tech, then absolutely, going the have-it-THIS WAY-if-you-are-THIS-type-of-user is quite legitimate.

That being said, I would not do it quite as you have done. I would still use different templates.

I would do it as:

If Tech Then ' however you figure that out
Addins("TechStuff.dot").Installed = True
Else
Addins("EngineerStuff.dot").Installed = True
End If



Done.

fumei
04-28-2008, 08:45 PM
BTW: I would also unload the addin when they are done.

I should also explain myself better.

Your code builds the controls (the commandbars) every time. This is inefficient, not needed, and allows more chances of errors creeping in.

By loading the appropriate template as an add-in, ALL the code is in that template file already. You are simply telling Word "use THAT one".

burtburt
05-08-2008, 12:01 AM
Ok. I need some help here. I'm lost on how to set up the menu structure in the template. When I install the template the menu is there. But I create a new doc it disappears. Then when I close the doc it re-appears. I'm confused. How do I set this up???

burtburt
05-09-2008, 09:07 AM
Now I get it. Ok. I've been working with the .doc for so long, it was hard to get away from building the menu each time.

For those of you stuck like I was, what fumei was telling me is...

If you build a menu or toolbar in a .doc first, then save it as a template .dot, the menu and/or toolbar will remain in the .dot template. You don't need to rebuild it when you use the template again. It's already there from when it was coverted from the .doc file.

So the above AutoOpen was not needed after the template was created. In the .doc you may need to rebuild it if you change things. Tearing it down and rebuilding with the new menu items is what I was doing to ensure the new code was there when I opened .doc the next time. But for a .dot, you can open the .dot, run a update menu sub, then save it, and the new menu will be there. I could have probably done that with the .doc too but the rebuild on AutoOpen and tear down on AutoClose worked well for me at the time.

I don't understand what is happening when the template was used to create a new doc and somehow the menu disappeared. But as fumei said, rebuilding each time was prone to [picnic] errors. Maybe that's one of them.

burtburt
05-09-2008, 10:55 AM
One final jewel I found. The rebuilding of the menu (or toolbar) will cause word to promt for "Do you want to save the changes to the template ...dot?" when closing any docs built from the template. This was making me crazy. Found this thread. (http://www.vbaexpress.com/forum/showthread.php?t=14364&page=1)


Love this site...

lucas
05-09-2008, 12:24 PM
If you build a menu or toolbar in a .doc first, then save it as a template .dot, the menu and/or toolbar will remain in the .dot template.

I hope you are not creating this menu with code each time because you can do it in the template manually and then it will appear each time you create a new file with the template or open a file created from the template. It will dissappear if you move a file created with the template to a different computer where the template is not available.

Word is very forgiving about creating menu items...check here (http://slucas.virtualave.net/Wink/CustomMenuItem.htm)

burtburt
05-09-2008, 01:48 PM
Steve,
That is pretty cool. I didn't know you could build menu's like this. But my menu is a bit more complex. Can you do sub menu's like this too?

I was thinking more of just runing the build menu subroutine one time and saving the template and not doing it in AutoOpen. After it's built, the sub doesn't need to be run again.

BTW, I haven't done this yet. But I see the benefit.

lucas
05-09-2008, 04:45 PM
Steve,
That is pretty cool. I didn't know you could build menu's like this. But my menu is a bit more complex. Can you do sub menu's like this too?



Yes you can. During the customize process instead of selecting macros scroll down to the bottom and select "new menu" grab it and drag it to the menu bar and then go back and select macro's and drag as many as you want to the flyout. You can drag multiple "new menu's" too and then add your macro's to them.

fumei
05-10-2008, 09:15 PM
I am confused with your use of AutoOpen.

First of all, AutoOpen is generally speaking an old action.

Document_Open is the event normally used now.

However, templates when cloned for a new document, use Document_New.

Now the issue is this (I think). The Techs are NOT making new documents, correct? They are working on existing documents.

The Engineers make new documents.

In a way, this is not all that relevant, as if the template s that hold menus, code etc, are loaded as global, everything should be in there.

burtburt
07-10-2008, 09:31 AM
You know. I tried Steve's suggestion, but the menu would only be visible when I had the .dot file open/visible. When I closed it the menu disappeared. For a global Template / Add in, I want the menu there always.

And to Gerry's point the AutoOpen wasn't working. A quick search in the VBA help revealed AutoExec is what I wanted. It does rebuild every time. And I have run into some issues. But for now, it's my only solution. I especially had some issues with Adobe PDF Creator in Word. It seemed that when I build the menu while this is installed, the Adobe PDF Maker would show up in my .dot's menu. Reinstalling the menu fixed it but it was a wierd thing. Just like Gerry described "prone to bugs".