Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 754
  • Last Modified:

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
0
c7c4c7
Asked:
c7c4c7
  • 8
  • 7
1 Solution
 
DrTribosCommented:
Upload the original file & template?
0
 
c7c4c7Author Commented:
Here it is
rename it to docm
exteriorEstimate.docx
0
 
DrTribosCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
c7c4c7Author Commented:
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
 
DrTribosCommented:
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
 
c7c4c7Author Commented:
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
 
DrTribosCommented:
Strange that the vba would dissappear. Can you copy it across from the old document and reassign any buttons.
0
 
c7c4c7Author Commented:
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
 
DrTribosCommented:
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
 
c7c4c7Author Commented:
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
 
DrTribosCommented:
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
 
DrTribosCommented:
Forgot to mention... this code goes in the THIS DOCUMENT module.

Cheers,
0
 
c7c4c7Author Commented:
Thanks, I will work on this over the weekend.
0
 
c7c4c7Author Commented:
Excellent!, Thanks for the help.  My apologizes for leaving this open for so long
0
 
DrTribosCommented:
No worries, glad it helped :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now