Solved

Macro in MS-Word

Posted on 2014-04-10
19
506 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 15

Expert Comment

by:DrTribos
ID: 39993110
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
ID: 39993113
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 15

Accepted Solution

by:
DrTribos earned 167 total points
ID: 39993269
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39993416
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
ID: 39993487
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
ID: 39993854
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
ID: 39996018
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 15

Expert Comment

by:DrTribos
ID: 39996738
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
ID: 39996799
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39996961
can you supply your code?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39997016
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
ID: 39997018
@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
ID: 39997035
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
ID: 39997208
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 15

Expert Comment

by:DrTribos
ID: 39997243
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 15

Expert Comment

by:DrTribos
ID: 39997244
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
ID: 39997266
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
ID: 39997286
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
ID: 39997300
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.

829 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