PDA

View Full Version : Solved: Active Document



cro_dux
02-25-2007, 02:29 AM
Hi,
I have macro that is running from Word (1). Macro opens another word app (2) and starts to copy from excel (to second word document). After every table that has been copied macro start UserForm asking for :"Do you want to continue". If answer is "Yes" next table will be copied in first Word document (1). How to set a active document so that macro can copy in it ??
tnx


'Openning new Word
Set WordApp = New Word.Application
Documents.Add DocumentType:=wdNewBlankDocument
WordApp.Visible = False

'Paste selected in word (Here I would like to tell macro in which word to copy !!)
Selection.PasteSpecial Link:=True, DataType:=wdPasteRTF, Placement:=wdInLine, DisplayAsIcon:=False

cro_dux
02-25-2007, 03:08 AM
Hi again,
now I found out that I can activate Word I want with :
Windows("Document1.doc").Activate

My next question is : How can I get the name of the new word Document that my macro opened ?? It would be easy if the name is always Document1.doc but it isn't
tnx

mdmackillop
02-25-2007, 04:55 AM
Do you really need to open another Word application?

Sub Test()

Dim MyFirstDoc As Document
Dim MySecondDoc As Document

Set MyFirstDoc = ActiveDocument
Set MySecondDoc = Documents.Add(DocumentType:=wdNewBlankDocument)

MyFirstDoc.Activate
Selection.TypeText "My First Document"
MySecondDoc.Activate
Selection.TypeText "My Second Document"
End Sub

cro_dux
02-25-2007, 12:08 PM
Thank you... just what I needed.
Now I just need to find out how to make first Word document (the one with macro) exe so nobody can write in it. I want it ony like application for starting macro (that's why I wanted to open another word document)

mdmackillop
02-25-2007, 12:52 PM
It sounds like you should be opening your first document from a template, and that there is no need for a second document.

fumei
02-26-2007, 07:34 AM
It does indeed sound like that.

You can not make your VBA code into an EXE. It does not do that. VBA code requires an VBA compliant application to execute.

To make this an exe you would have to use VB.

However, you can do this with a template. You can keep the template file itself locked away. What exactly are you trying to achieve by "so nobody can write in it"?

fumei
02-26-2007, 07:36 AM
A further thought. If it is just CODE you want accessible, then put the code in a global template (not normal.dot), have that as a read-only file in the Startup folder.

All the code will be accessible. Then have that code make your blank documents.

The problem with using a template is that template code can be accessible from documents cloned from it.

lucas
02-26-2007, 08:10 AM
Unless it's in the document new module......am I right about this?

lucas
02-26-2007, 08:25 AM
Further research show that Gerry is right. You can access the code in a template while first opening it from the template. After it is saved however there is no code in the saved clone of the template.

fumei
02-26-2007, 08:49 AM
There may be no code in the saved cloned document, BUT...if you open that saved cloned document, you can add, change, delete all the code you want in the template file, even though it is NOT open.

You will prompted to save the changes to the .DOT file, true. But you certainly CAN alter code in the template file, (from any cloned document) even though the template file itself is not open.

lucas
02-26-2007, 08:54 AM
I see that Gerry....still learning myself..

fumei
02-26-2007, 12:06 PM
Which is why, if you want to be serious, use global templates to store your code, and make the global read-only.

NOT Word protected, but actual OS file attributes read-only. If you can, also make the folder permissions read-only.

This will stop most people doing anything. Sure they could change the code in the template - I am not sure if the folder permissions disallowed Modify if you can still modify code modules - but they could not save the changes.

Bottom line though...there is NO security that is unbreakable. Especially any Microsoft products!

Back to the poster...

ActiveDocument.Name will return the name of the Active Document, whether that is Document1, or Document34. However, it is rather a meaningless name.

It is preferable to use Document objects. Perhaps if you expanded on what you want to do.

BTW: you do NOT need to activate a document to do stuff with it.Sub Test()

Dim MyFirstDoc As Document
Dim MySecondDoc As Document

Set MyFirstDoc = ActiveDocument
Set MySecondDoc = Documents.Add(DocumentType:=wdNewBlankDocument)
' so MySecondDoc is the active document, right?

MyFirstDoc.Range.InsertAfter "My First Document"
MySecondDoc.Range.InsertAfter "My Second Document"
You can action most instructions directly on the object without activating it.

cro_dux
03-03-2007, 01:13 AM
Thanks guys you really helped me.
I will tag this as solved.
tnx