?
Solved

Word 2010 - can I remove VBA code from a document to allow it to be saved as docx without warning

Posted on 2014-09-23
10
Medium Priority
?
1,911 Views
Last Modified: 2014-09-23
I have a small piece of VBA code stored in a template that runs at the document_new event.
After the document is created the user will get a warning when saving as a docx (the default AND REQUIRED setting) - because of the VBA code.

So is there a way to run the piece of code then automatically remove the VBA section from this new document so it can be saved as a plain docx?
0
Comment
Question by:AndyAinscow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40338586
Application.DisplayAlerts = wdAlertsNone
'~~> Your Save Code
Application.DisplayAlerts = wdAlertsAll
0
 
LVL 44

Author Comment

by:AndyAinscow
ID: 40338589
I think I understand what you are getting at.
I don't have any code run for the saving, it is the user performing the save by clicking save on the ribbon.  So I could turn alerts off at the end of the VBA code that runs on new doc, but how would I turn the alerts on again after the document is saved - the VBA section should not exist any more.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 total points
ID: 40338592
If you have some code in a Normal template in the document_new event, it shouldn't be copied into the new document.

If there are macros in the new document, then I don't think you can automatically disable the warning box - it's there for your protection.
0
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

 
LVL 44

Author Comment

by:AndyAinscow
ID: 40338600
This is in a custom template (not normal.dot) but another template used as the basis for the new document.
Private Sub Document_New()
    ActiveDocument.BuiltInDocumentProperties("Author") = GetLoginName()
End Sub

Function GetLoginName() As String
    'code here
End Function

Open in new window


When I create a new document then try to save it I get the following:
warningThat complains that a VBA code section still exists in the document.  Basically, that is what I want to get rid of so the end user can save as a docx without code.  The VBA section does exist, I can go to it via the developer tools:VBA ribbon options.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40338700
I have created a custom template based on the above code, created a document, and saved it as a docx. I have had no problems with saving it. The macros did not exist in the new document.

This is what is in my Template - Project - Microsoft Word Objects - ThisDocument:

Private Sub Document_New()
    ActiveDocument.BuiltInDocumentProperties("Author") = GetLoginName()
End Sub

Function GetLoginName() As String
    GetLoginName = "Hithere"
End Function

Open in new window


I would upload the file for you to test, but I am unable to upload dotm files.

Incidentally, thank you. I am developing a website based on metadata, and based on your previous question I am expanding it with a few additional pages - here's an example: http://www.filecats.co.uk/metadata-extended-document-properties/microsoft-office/accessing-within-ms-office-vba/
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40338712
I am puzzled by this question. How does the code get into the document?

Code in a template is not automatically transferred to a new document if the document is created by selecting File/New and then selecting the template.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40338714
You might be able to upload the template if you change the extension to, say, .dot.
0
 
LVL 44

Author Comment

by:AndyAinscow
ID: 40338721
Aaaaaaagrh - I've just seen the 'template' isn't, for some reason word saved it as docm not dotm.
I'll get back later after really generating a template.
AndreaTest.txt
0
 
LVL 44

Author Comment

by:AndyAinscow
ID: 40338744
My fault.  That's a couple of hours time wasted.

I'd either copied the wrong document to the templates folder or selected the wrong file type to save as (docm rather than dotm) when generating the template.
Everything works, the VBA is not copied from the template to the new document.


ps. Ignore the attachment in the previous post, it is in reality a docm not a dotm extension - which is when I noticed something wasn't correct with the file extensions.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40338746
Yay! Glad it works.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Suggested Courses

764 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