PDA

View Full Version : ActiveDocument SaveAs without VBA Code



g@alenia
04-12-2010, 05:05 AM
Hello,

I was in need to have a routine to initialize document properties (Title, Author etc...). So I created an AutoOpen function that shows this UserForm when I open Document.doc:

img52.imageshack.us/img52/3923/form1o.jpg

When I click on Sfoglia button, it shows a SaveAs FileDialog which let me choose the path to save the new initialized Document. Here's the code:


Private Sub Button_Browse_Click()
Application.FileDialog(msoFileDialogSaveAs).Show
TextBox_SaveAs.Value = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
End Sub

Then, when I click on OK button it fills the properties of the ActiveDocument and saves it as the NewDocument.doc that I chose with the previous code. Here's the code:


Private Sub Button_OK_Click()
ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle) = TextBox_Title.Text
ActiveDocument.BuiltInDocumentProperties(wdPropertySubject) = TextBox_Subject.Text
ActiveDocument.BuiltInDocumentProperties(wdPropertyAuthor) = TextBox_Author.Text
ActiveDocument.BuiltInDocumentProperties(wdPropertyCategory) = ComboBox_Category.Value
ActiveDocument.BuiltInDocumentProperties(wdPropertyKeywords) = TextBox_Keywords.Text
ActiveDocument.BuiltInDocumentProperties(wdPropertyComments) = TextBox_Comments.Text
ActiveDocument.SaveAs (TextBox_SaveAs.Value)
UserForm_DocInit.Hide
End Sub


The NewDocument.doc is saved successfully, but it is a perfect copy of the ActiveDocument. That is the problem. I need that the vba code enclosed in ActiveDocument does not apply in the NewDocument.
In a few words, when I reopen NewDocument.doc It should not show the Initialization form anymore.

Thanks in advance


MS Office Word 2003 11.8169.8172 SP3
MS Visual Basic 6.3

PS: Sorry for my english. I hope it would be understandable.

TonyJollans
04-12-2010, 06:34 AM
You should be using a Template (.dot file), not a Document. When you create a Document based on a Template, the content from the Template is copied to the New Document that is created, and the VBA code in the Template will run, but the VBA code will not be copied to the document.

Before saving the new Document, you can attach it the Normal Template if you don't wish to have a continued association with your base Template.

g@alenia
04-12-2010, 07:34 AM
Hi tony,

I tried to do as you suggested but it still doesn't work.
I renamed the document with VBA code "Document.dot" and put it in the templates directory.
If I choose to open the template from the open file interface in word, the vba code run, but it will still persists in the new created document.
On the other hand if I choose to create a new document, using document.dot as template, the VBA code won't run.

lucas
04-12-2010, 07:43 AM
The code is not in the new document created from a template.

try moving it to a different machine.

Tony's last sentence explains how to remove the association with the template if you feel the need. You really don't need to though.

If you look in the vba editor of the new document itself you will see the relationship under references in the explorer.

The clone has access to the code in the template as long as the template is available. If not, it doesn't, ie if you move the clone to a different machine or remove the reference to the template.

g@alenia
04-12-2010, 08:05 AM
Maybe I did not explain correctly in my last post. This is what i do:

PROCEDURE 1
1. Run Word
2. File->New->Model->In this computer->Document.dot

Doing this way it creates a new Document1.doc with the correct layout of Document.dot, but no VBA code run.

Another way I tried is the following:

PROCEDURE 2
1. Open Document.dot (VBA code run)
2. Save As a new document (using the function i wrote before)
3. I have my new inititialized document.doc

This way, when I close document.doc and reopen it the vba code will run again.
No need to say that I would like to use the first procedure, as i think it is the most correct.

lucas
04-12-2010, 08:08 AM
I misread your post.


If you use file open then that means that you are actually opening the .dot file. Does it say whatever.dot at the top of the document?

If it does and your code runs then that means you have your code in a document open procedure such as this:


Private Sub Document_Open()
'code
End Sub

To make it run on the newly created document when you use the template as a template, you need to put your code in a document new procedure such as this:


Private Sub Document_New()
'code
End Sub
That way if you use the document as a template and it creates a new file, the code will run every time you use the template and none of the code will be in the newly created document.

Sorry, I didn't drink my coffee before I started posting this morning.

Hope this helps.

lucas
04-12-2010, 08:10 AM
Put your code in a document new procedure and use your Procedure 1.

You will get the results you desire.

fumei
04-12-2010, 08:11 AM
Not only is the most correct, is the ONLY correct.

#1 calls the template (.dot); it clones the content of the template (.dot)...without the VBA code of the template. The VBA code remains in the template.

#2....opens the template file itself; thus if it is saved as a document, then that document will of course have the VBA code.

#1 is the proper way to use template.

TonyJollans
04-12-2010, 09:14 AM
I renamed the document with VBA code "Document.dot"

Renaming a file does not change anything except the name - if it was a document to start with, it will still be a document (just one called something.dot instead of something.doc). Only Word can create a Template - you need to do a SaveAs as a Template.

g@alenia
04-13-2010, 01:51 AM
The first function i wrote was


Sub AutoOpen()
'code
End Sub

I tried to rename it Document_New () or DocumentNew() but it doesn't work. And now it do not run when I open document.dot with word.

TonyJollans
04-13-2010, 03:15 AM
AutoOpen macros can be in any module, but ..

Document_New procedures must be in the "ThisDocument" module.

g@alenia
04-13-2010, 04:57 AM
Great! Now it works.
Thank you guys! :)

g@alenia
04-21-2010, 01:27 AM
New problem on this routine...

1) when I save as new document, using the form, I need that the new document filelds update themselves. For example:
I put "title" in the textbox "Titolo" and when I click on OK in the form, to save as new document, the DOCPROPERTY Title field I put in document should auto-update.

2) Moreover, i have a docproperty related issue on the document.dot template. If I right-click on document.dot file and choose properties, i'm able to see the DOCPROPERTY. As correct they are blank, but when I use the template the DocProperty fields in the template shows "blablabla". It seems that that text that i used before in some tests on the template still exists, i have no idea where! So when I close the new created document and look at document.dot file properties, i see "blablabla" in the related docproperty field.

Any suggestions?

Thanks

g@alenia
04-27-2010, 04:32 AM
up

lucas
04-27-2010, 07:49 AM
It would help immensely if you could post the template so we can trouble shoot it. It is very hard to do like this, especially with the language barrier.

click on go advanced and then scroll down till you see the button that says manage attachments.

If it is a template with a .dot file extension you will have to zip it up as the forum doesn't allow uploads of .dot files.

Remove any company or personal information from it before uploading it.

fumei
04-27-2010, 09:58 AM
I agree. We need to see some real-life examples. I am having a hard time figuring out what - EXACTLY - is happening.