Link to home
Start Free TrialLog in
Avatar of ExpExchHelp
ExpExchHelpFlag for United States of America

asked on

Macro in MS-Word

Hello Experts:

I'm using a Word template where users complete some standard information.  

Here's the current process which works fine:
1. User opens "Template.dot" (via double-clicking)
2. MS Word opens with temp document name "Document1"
3. User complete three fields (Date, Name, Description)
4. User clicks on command button "Copy Me!"
5. User selects file location and specifies new file name.

Again, above process works as envisioned and does NOT require "tweaking".  Here's however where I need some help with.   Let's say the user named the new Word document "Test.doc".

When the newly created replica document, the macro code (see below) is also included in this document.    I don't want that (reason for that is not important here).

Instead, when the user double-clicks on the .dot file (which temporarily generates "Document1") and then clicks the command button, I want the new file (e.g., Test.doc) to NOT include any macro code.  

Thus, my question... as part of the document replication, what function can be built into the template that not only deletes the code below but also "itself"?   That is, if a new VBA function is required to delete the code below, I also need to delete the DELETE function.    Again, the "Test.doc" shouldn't contain any VBA code once saved.

How can this be accomplished?    [Attached is the current "Template.dot" file.]

Thanks,
EEH



Option Explicit

Private Sub CommandButton1_Click()

    Dim doc As Document
    Set doc = ActiveDocument
    doc.InlineShapes(1).Delete
    
    'Allows user to specify folder location and file name
    Application.Dialogs(wdDialogFileSaveAs).Show
    
End Sub

Open in new window

Template.dot
Avatar of DrTribos
DrTribos
Flag of Australia image

By design the code from the template will not go into the document.  Is it possible that your template opens as a template instead of creating a document.  

When you double click on the template, can you verify that "Document #" appears at the top of the screen?

Otherwise, it shold be possible to delete all code in the document using VBA via the "Organiser".
Avatar of ExpExchHelp

ASKER

DrTribos:

Thanks for the response...

Hmh, good question... right now, the .dot file opens "Document1".   As mentioned, the VBA code is also contained in the new document.

From what I recall (when I created the template), I opened up Word, save the document as a .dot and then added the command button + VBA.

As this process works great right now, I merely need to ensure to remove the the VBA from the document.     And, yes, "Document#" does appear in menu bar when opening double-clicking/opening "Template.dot".

Any suggestion for accomplishing this process?

EEH
ASKER CERTIFIED SOLUTION
Avatar of DrTribos
DrTribos
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chris Bottomley
Also can you advise the scope of the VBA, it is feasible to copy the code as part of the template activity but there would be a code sequence to underpin this.

Chris
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
DrTribos, Graham:

I think both of you were right...

When opening the new document (e.g., "Test.doc"), I must have always viewed the "Project (Template)"... which comes up by default.  

I now realized that I should have clicked the project for "Date".    Attached screenshots (in MS Word) confirm what you suggested.

Once I deleted (for testing purposes) the template from the same subfolder (e.g., Desktop) and then re-opened Test.doc, the template project was not included... great!

Ideally though, I don't want the project for the template to be included (as it may make other teams members make the same mistake I did).

Graham -- you suggested one line of code [doc.AttachedTemplate = "Normal"].   Where do I place this line so that the template won't be included.   [I inserted this line before "Dim doc As Document" and it threw an error.]

Again, thank you for your assistance.

EEH
In the 'thisDocument' module of the template add the sub or insert the extra line (2) if it already exists :

Private Sub Document_New()
    If lcase(ActiveDocument.AttachedTemplate) = lcase("gash.dotm") Then ActiveDocument.AttachedTemplate = "normal"
End Sub

Open in new window


replace the sequence gash.dotm with your template name and it should work

Chris
Or you can put a lock on the template project so a password is required to view. This would still allow your macros to run if needed down the track.
Chris Bottomley:

Thanks for the feedback... after having added the suggested code, the "CopyMe!" command button doesn't bring up the Browse dialog box any longer.

DrTribos:
Thank you for the additional feedback as well.   Based on some constraints on the environment, the client does NOT want any macros on their system.   Even though the new document doesn't, the mere idea that the project code from the template (accessible via the new document) can be viewed is a concern.

So, apparently, the question remains... how can I ensure the project code from the template does not show up.

EEH
can you supply your code?
As a rule, if you want to tell us about an error, please also say what the error is.

You have some code that helps to create the document. Presumably it is then sent to the macro-averse client.

If the document has no access to the template, the code can't run. Make sure, though the the template isn't stored on a separate server, or when the document is opened it will wait a long time for the server to respond. For this reason it would be better to set the attached template to Normal. Make it the last thing to happen before the document is saved.
@Chris

Were you in the RAF? I haven't seen that use of 'gash' for many years.
Graham

Indeed I was though I've been a normal human for some 17 years now.

I take it you were as well then ... Bondu ogin and all that!

Chris
Chris:   Thanks... please see original (first) post with includes "Template.dot" (incl. VBA code).

GrahamSkan -- copy... please see attached snapshot.    It's not really considered an error.  Instead I'd like to have access or hide the project VBA editor in new documents.    You indicated to save the template as "Normal".   Isnt' that the default setting for Word documents?    Potentially, can you post the modified template (or steps)?

Thanks,
EEH
Snapshot.jpg
Normal is the default template for document that are created as a, lets say "blank" document. As the documents you are creating ARE based on a template that you created the use that as the attached template.
If you change the template to normal then you loose all access to the code in your template UNLESS your template is loaded as a global template.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The normal template is the one that it is used when you ask for a blank template via File/New.

It is global, so that any code in it is accessible to any document.

If you move the document to somewhere that cannot access the template, then it won't show the reference to its original template. If the template is not on a server, then you could simply send the document to its new environment without doing anything special.

If the attached template is on a server, and the server can't be found, then it will be very slow to open because Word will allow time for the server to respond before giving up the wait and opening the document without the template.
I have been working on the assumption that when the macro code has done its job, then it is to be sent to somewhere that is macro-free.

If that isn't the case, then I might have confused you, but it begs the question of why do you want to detach the code.