Solved

Cannot open at Docx file created with VBA

Posted on 2014-04-24
15
644 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
[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
  • 8
  • 7
15 Comments
 
LVL 15

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 15

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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 15

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 15

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
 

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 15

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 15

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 15

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 15

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

732 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