Solved

Macro in MS-Word

Posted on 2014-04-10
19
497 Views
Last Modified: 2014-04-14
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
0
Comment
Question by:ExpExchHelp
  • 5
  • 5
  • 5
  • +1
19 Comments
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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".
0
 

Author Comment

by:ExpExchHelp
Comment Utility
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
0
 
LVL 14

Accepted Solution

by:
DrTribos earned 167 total points
Comment Utility
Ok what I did...
1 Download the template
2 Double click the template
3 Verify a new document is created: Document 1
4 Open the VBE by pressing ALT + F11
5 Open the template project (in VBE) - confirm there is code in the template
6 Open the document project (in VBE) - confirm there is NO code in the document
7 Switch back to the document
8 Edit document data
9 Press the save button
10 Switch back to the VBE and, again, confirm there is NO code in the document

Please follow these steps and see what differences you get.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
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
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 167 total points
Comment Utility
Are you certain that the code is actually in the new document? As Steve says, when Word makes a new document from a template, code from the template is not copied into the document.

Instead, the template is 'attached' the the new document, so that the code can used by the document. If you want the code to be unavailable, the Normal template should be attached in its place.
doc.AttachedTemplate = "Normal"

Open in new window

0
 

Author Comment

by:ExpExchHelp
Comment Utility
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
0
 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
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
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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.
0
 

Author Comment

by:ExpExchHelp
Comment Utility
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
can you supply your code?
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
@Chris

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

Expert Comment

by:Chris Bottomley
Comment Utility
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
0
 

Author Comment

by:ExpExchHelp
Comment Utility
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
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:DrTribos
Comment Utility
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.
0
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 166 total points
Comment Utility
My point was to see where you added the open macro code ... i.e. did you embed it in your existing code or did you truly add it separate to your existing code?

Chris
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now