[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Cannot open at Docx file created with VBA

Posted on 2014-04-24
15
Medium Priority
?
690 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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.

656 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