Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing active document 'control' 2

Status
Not open for further replies.

colinmitton

Technical User
Feb 24, 2005
190
GB
I have set up a macro to auto run when a word 2010 template is opened, it will then ask for another word document to be inserted in to that document. Once done it will them open a different template as a separate document.

This will then leave the two correct documents on screen, I'm happy with this but I need to change one detail. The first document thats created (which has the inserted file in) goes to the 'back' the second file is on front. I really need it to be the other way round! I dont want them to auto save anywhere as it will confuse my poor users when they do to save!

Is there a way of dynamically getting control of the first document?

Code:
Sub AutoNew()
    Application.Run MacroName:="textinput"
End Sub

Sub textinput()
    With Selection.Find
       .ClearFormatting
       .Text = "***"
       .Replacement.Text = ""
       .Forward = True
       .Execute
    End With
        Dim defpath As String
            defpath = Options.DefaultFilePath(wdDocumentsPath)
            Options.DefaultFilePath(wdDocumentsPath) = "C:\2010\Rule2 Letters"
        With Dialogs(wdDialogInsertFile)
            .Name = "*.docx"
            .Show
    End With
            Options.DefaultFilePath(wdDocumentsPath) = defpath

    Documents.Add Template:="C:\2010\Word\Forms - Files\Standard Terms of Business.dotx", NewTemplate:= _
        False, DocumentType:=0
End Sub
 
Something like this ?
Documents(1).Activate

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Or, if you use document objects, you do not need to activate anything.


unknown
 
PHV,

I tried the code you suggested earlier when I looked through the VBA help file. I put it after the Documents.add string (to call the second document) but it did not work.

I think the second document that opens 'loses' control of the macro and that line of the code does not run.

In Excel I've used: Window(filename).Active - I think!!! but I don't want to pre-save and the auto numbering of the documents means they change all the time so I'm a bit confused on how to get control back?

Fumei,

Could you please expand more on your answer and maybe an example I could work from, I'm not very good with VBA. I generally just record macro's and clean them up a bit, on some of the macro's I get help here or via google! I'm sadly not a programmer.

Thanks!
 
And this ?
Code:
Sub textinput()
Dim myDoc As Document
Set myDoc = ActiveDocument
    With Selection.Find
...
myDoc.Activate
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

Thanks. Thats worked great when I open the template from 'outside' of word. i.e. I dont have word running go to a folder then open the template from there.

If I open it from with-in word with the open new templates windows it does not. The last document to open stays active?

Sorry if I did not make this clear at the beginnning, I have been testing by just opening the template from explorer, but then need to test the way that my users would do it (from with-in word) I did not realise it would react diffeently!

Any more suggestions? nearly there!
 
Having just thought about it a bit more, As they will be opening the template with-in word (I guess they will have other documents open too) would I need to assign a unique name to the 'first' document created from the template? so its easier to call back?

Not sure If I'm making sense! its getting late here in the UK :)

i.e. words open, document1 on screen (blank). I go to open the template, Word creates document2 and runs the code to insert the document to that file.
The code then opens document3 from the given template.

I need it to return to document2 (with the inserted text)!

I really hopes that makes some sense!!! I think I need sleep now :)))
 
PHV is demonstrating what I suggested, using document objects.

When you use objects you can perform actions on the objects, regardless of whether they are active or not.
Code:
Sub textinput()
Dim defpath As String
Dim ThisDoc As Document
Dim ThatDoc As Document

ThisDoc = ActiveDocument
' the following actions the ActiveDocument
With Selection.Find
   .ClearFormatting
   .Text = "***"
   .Replacement.Text = ""
   .Forward = True
   .Execute
End With

defpath = Options.DefaultFilePath(wdDocumentsPath)
Options.DefaultFilePath(wdDocumentsPath) = "C:\2010\Rule2 Letters"
   With Dialogs(wdDialogInsertFile)
      .Name = "*.docx"
      .Show
   End With
Options.DefaultFilePath(wdDocumentsPath) = defpath
' if you Execute the dialog, the file is inserted
' into the active document - ThisDoc

' this now has the incoming new document as ActiveDocument
' but it DOES NOT matter
' as you are using an OBJECT
Set ThatDoc = Documents.Add _
   (Template:="C:\2010\Word\Forms - Files\Standard Terms of Business.dotx", _
   NewTemplate:=False, DocumentType:=0)
' if you want the first document Active, then Activate it
ThisDoc.Activate
End Sub
When you state:
" just opening the template from explorer,"

are you double clicking the template file, or are you right-clicking and selecting Open?

If you are double clicking, then you are NOT opening the template. You are calling the template and it will create a clone of itself. Repeat, the template is NOT opened.

"If I open it from with-in word with the open new templates windows it does not. The last document to open stays active?"

Not exactly sure what you mean, but yes, the LAST document opened is the ActiveDocument.

Which is why is a GOOD thing to use document objects.

1. you can always refer to any given document by name, whether it is Active, or not.

2. you can perform ANY, and I really mean any, actions you want on any document you want. It does NOT matter if it is Active.

Say you have three documents already open:

yadda.doc
whatever.doc (this is ActiveDocument, although it does not matter)
HoHum.doc

You want to:

1. make a new document using Yippe.dot

2. add Table4 from yadda.doc at the end

3. get the header content of whatever.doc and put it in cell(1,2) of the table

4. add a Section break to the new doc, followed by all of the content of Section 3 of HoHum.doc.
Code:
Sub LotsOfStuff()
Dim yadda As Document
Dim what As Document
Dim Ho As Document
Dim NewGuy As Document
Dim r As Range
Dim aTable As Table

Set yadda = Documents("yadda.doc")
Set what = Documents("whatever.doc")
Set Ho = Documents("HoHum.doc")
Set NewGuy = Documents.Add Template:="U:Gee\Templates\Yipee.dot"
Set r = NewGuy.Range
r.Collapse 0
   Set aTable = NewGuy.Tables.Add(Range:=r, _
      NumRows:=yadda.Tables(1).Rows.Count, _
      NumColumns:=yadda.Tables(1).Columns.Count, _
      DefaultTableBehavior:=wdWord9TableBehavior, _
      AutoFitBehavior:=wdAutoFitFixed)

aTable.Cell(1, 2).Range = _
   what.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set r = NewGuy.Range
With r
   .Collapse 0
   .MoveEnd Unit:=wdCharacter, Count:=1
   .InsertBreak Type:=wdSectionBreakNextPage
End With
Set r = NewGuy.Range
With r
   .Collapse 0
   .Text = Ho.Sections(3).Range.Text
End With
End Sub
All without ever making any separate document Activated.

The new document will have a copy of Table4 from yadda.doc; the content of the header from whatever.doc put into cell(1,2) of that table; a section break, and then the text of Section 3 from HoHum.doc.

Note that this also uses a table object to action the table.


unknown
 
Fumei,

Thanks very much, Its explaned a great deal. I was 'calling' the template not opening it. Sorry my terminology was incorrect on that front.

Your explantion was great, I must admit I'm not programmer and I'm still looking for a decent VBA book to use with word / excel 2010 as I know I can do a lot more.

I now have the code working really well. Its also open the door to a few more problems I can solve witrh that same issue too. Thanks once more.

PHV, Thanks for steering me in the right direction.
 
Just to prove your post did the trick! using your advice I've set up this simple macro to convert the current document on screen to a PDF. No matter how many documents are open!

Code:
Sub Send_to_PDF()
Dim cDoc As Document
Set cDoc = ActiveDocument
    cDoc.ExportAsFixedFormat OutputFileName:= _
        "C:\Users\Default\Documents\rename_me.pdf", _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True
End Sub

Thanks again!
 
Good for you. Using objects (not just document objects) opens up the power and efficiency of VBA.


unknown
 
Although I have to say that, as it is coded, your Send_to_PDF procedure is not really affected by whether there are other documents open, or not. It ONLY instructs sending the ActiveDocument to PDF. It does not identify (or set) any explicit document.


unknown
 
Thanks, I understand what you mean as this was the first step in a bigger macro to open several documents and convert a couple (one on opening) to PDF

So I started as I needed to continue!

Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top