Solved

Cannot open at Docx file created with VBA

Posted on 2014-04-24
15
596 Views
Last Modified: 2014-07-27
I have a .docm file I use as a template and then modified using VBA, the file is saved as a .docx file using part of the info entered by the user.  The file is to be opened later and printed.

After saving the created file as a .docx file I cannot open them with Word.  It says that there is something wrong with the file.

I took a look at the raw contents using Notepad and found that there was a heading in it of XML.  So I resaved the file as .docxml, opened it and saved it as a .docx and everything works.  

I don't see what is causing the problem in my code and would appreciate some help

Thanks

In case this makes no sense ask some questions and I will try to clarify
exteriorEstimate.txt
0
Comment
Question by:c7c4c7
  • 8
  • 7
15 Comments
 
LVL 14

Expert Comment

by:DrTribos
ID: 40021551
Upload the original file & template?
0
 

Author Comment

by:c7c4c7
ID: 40021729
Here it is
rename it to docm
exteriorEstimate.docx
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 40021834
Well it works fine on my computer - but that might be due to a different version of word.

There are a few things I noticed.

1.  You really should be using a ".dotm" file instead of a ".docm" for your document...
The difference is that .dotm is a template file and automatically creates a new document based on the template - the template is kept safe from accidental change

2. Your SaveAs2 does not seem correct... try:
ActiveDocument.SaveAs2 path & "\" & Me.txtJobAddress.Value, wdFormatDocumentDefault

Open in new window

0
 

Author Comment

by:c7c4c7
ID: 40023968
Your modification to the SaveAs worked great and I can now open the files after they are saved.

However if I make modifications to the body before I close the document I get the dialogue box that I included in the uploaded jpeg.

Any ideas
dialog.jpg
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 40025057
Ah... that is because you are using a document as a template instead of a template.  You would need to either change the save as file type to macro enabled Document (not recommended unless you have a special reason for wanting macros in the document) OR change your base doc to a template. You can simply achieve the latter by choosing file save as template and make sure you choose macro enabled.
0
 

Author Comment

by:c7c4c7
ID: 40036261
Got sidetracked for a while

So I saved the document as a .dotm in the Personal templates, when I open it I get the message in the jpg and all of the VBA is gone.  I tried most of the suggestions I could find,
delete the Normal Template reregister some .dll's.  Nothing worked

Any suggestions
dotm.jpg
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 40036447
Strange that the vba would dissappear. Can you copy it across from the old document and reassign any buttons.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:c7c4c7
ID: 40044969
So I moved everything over to the template.  One of the first things that I do is take the job counter increase it and store it back into the document so that it is up to date.

When I use the document as a template it trys to save it as a a docx with the counter as the name of the document.  How can I save it back as a .dotm without hardcoding the name of the document and document type.

See the code in the attachment
vba.txt
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 40046151
I won't be able to make a demo solution until Friday as I am away.  I think you can use a document variable in your template. When you create a new document it should also have the same variable.  VBA code can be used to increment the value of the variable of both the doc and the template.  And for extra control you can have a 2nd variable to act as a flag to prevent change after initial setup
0
 

Author Comment

by:c7c4c7
ID: 40060568
I've looked around and cannot find examples of what you are talking about or I do no understand what you are suggesting.  
Can you point me in the right direction

Thanks
0
 
LVL 14

Accepted Solution

by:
DrTribos earned 500 total points
ID: 40088965
Hi there,

Sorry for long delay - when I arrived back from my trip I fell sick.  How are you going with this?

I hope it is still of use, here is some code to demonstrate what I mean.  This code would go into the template, and would run each time a NEW document is created.  Before this code is run you will need to create a custom document property, see images (must press OK at the end).

Option Explicit

Private Sub Document_New()
Dim cnt As Long

cnt = CLng(ActiveDocument.CustomDocumentProperties("Counter").Value)
MsgBox "Last Document: " & cnt
cnt = cnt + 1
ThisDocument.Save ' save the template with the update

' Use ThisDoc instead of ActiveDoc to refer to the doc where the code
' is located - i.e. the template.
ThisDocument.CustomDocumentProperties("Counter").Value = cnt

MsgBox "Creating Document: " & ActiveDocument.CustomDocumentProperties("Counter").Value

End Sub

Open in new window


Setting Up A Custom Property Setting Up Custom Property
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 40088966
Forgot to mention... this code goes in the THIS DOCUMENT module.

Cheers,
0
 

Author Comment

by:c7c4c7
ID: 40102218
Thanks, I will work on this over the weekend.
0
 

Author Closing Comment

by:c7c4c7
ID: 40222699
Excellent!, Thanks for the help.  My apologizes for leaving this open for so long
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 40223094
No worries, glad it helped :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

757 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

21 Experts available now in Live!

Get 1:1 Help Now