Link to home
Start Free TrialLog in
Avatar of c7c4c7
c7c4c7Flag for United States of America

asked on

Cannot open at Docx file created with VBA

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
Avatar of DrTribos
DrTribos
Flag of Australia image

Upload the original file & template?
Avatar of c7c4c7

ASKER

Here it is
rename it to docm
exteriorEstimate.docx
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

Avatar of c7c4c7

ASKER

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
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.
Avatar of c7c4c7

ASKER

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
Strange that the vba would dissappear. Can you copy it across from the old document and reassign any buttons.
Avatar of c7c4c7

ASKER

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
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
Avatar of c7c4c7

ASKER

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
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
Forgot to mention... this code goes in the THIS DOCUMENT module.

Cheers,
Avatar of c7c4c7

ASKER

Thanks, I will work on this over the weekend.
Avatar of c7c4c7

ASKER

Excellent!, Thanks for the help.  My apologizes for leaving this open for so long
No worries, glad it helped :-)