Results 1 to 13 of 13
  1. #1
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How does one make the added doc the active one? (Word 2000/SR1)

    I am adding Word docs to an existing instance of Word via automation from inside a word addin, here is the code that does that:

    On Error Resume Next
    'Try an establish link to open instnace of Word
    Set objWorkpaperWord = GetObject(, "Word.Application")

    I

    ' if this fails then open a new word app
    If objWorkpaperWord Is Nothing Then
    Set objWorkpaperWord = CreateObject("Word.Application")
    End If
    objWorkpaperWord.Visible = True
    objWorkpaperWord.Documents.Add (arrFilePaths(iSelectedTitle) & "" & WorkpaperTemplates(iSelectedTitle, iSelectedCategories, iSelectedWorkPaper))
    objWorkpaperWord.ActiveWindow.Activate
    objWorkpaperWord.ActiveWindow.Caption = strDocname
    objWorkpaperWord.Activate

    Set objWorkpaperWord = Nothing


    And this is sucessfully adding the word doc to the existing instance but there is a problem in that the new added document is not active. The icon on the status bar for the new document is active and the document the caption is correct but the area that contains the caption is grayed out and I first have to select the document before it becomes active. How do I do this via VBA code?

    I have tried adding code in the template that is being added in it's ThisDocument_New event such as :
    ' Make sure this document has the focus
    ThisDocument.Activate

    But this does help.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    Jim,

    Thought I recently saw a post by Hans V. with the answer to this question, but anyway:

    Assign the new document to an object variable, as you add it; then activate the new document via the object variable, along the lines of:

    '(after you have created the objWorkPaperWord object)
    Dim objWordDoc As objWorkpaperWord.Document
    Set objWordDoc = objWorkpaperWord.Documents.Add (arrFilePaths(iSelectedTitle) & "" & WorkpaperTemplates(iSelectedTitle, iSelectedCategories, iSelectedWorkPaper))
    objWordDoc.Activate 'etc.....
    'and at the end of the procedure, don't forget:
    Set objWordDoc = Nothing

    Gary

  3. #3
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    Thanks but I have tried that and tried it again and it still does not set focus to the document I wish to have focus. One would think there would be a set focus method.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    Here's another one to try:

    objWorkpaperWord.Documents(objWorkpaperWord.Docume nts.Count).Activate

  5. #5
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    Tried that too before and tried it again but no cigar. It is a strange problem that I ma having here.

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    If those methods aren't working, then you're right something strange is going on.
    Question re your first post:
    <hr>I am adding Word docs to an existing instance of Word via automation from inside a word addin<hr>
    Not sure I see the big picture for why you are doing this - If you're using a Word add-in to add documents to an existing instance of Word, then why are you using automation in the first place?

    Gary

  7. #7
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    I have noticed one other thing happening which I don't seem to be able to stop and that is when I open one of the docs via the VB code I also get the toolbox appearing as well. Maybe it is related.

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    Jim,

    The upshot of my previous post was that if there is a way to do this project without the automation, it would probably work more reliably and be easier to debug. Automation is kind of rickety to begin with. The part about automating Word from Word just caught my attention, it sounds like something prone to cause a problem.

    Gary

  9. #9
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    LOL... yes but the requirements are that one of our Word Templates shall be able to create another Word doc, etc.

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    Fair enough, but you don't need automation to have a Word template create a Word document; you can do the whole thing directly in Word without needing to create an extra instance of Word.

    Automation is used to control one application from another application. If your entire application is completely based within Word, then not only do you not need to be using automation, but that is almost guaranteed to be the reason you're having a problem.

  11. #11
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    I am creating the new word doc from inside the Word Addin using the code shown in the first message of this posting. There are 32 templates so we want as much reusable code as possible. How else would I do this?

  12. #12
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    In its simplest form, it can be as simple as:

    <pre>Public Sub CreateNewDocument(strTemplatePathName)
    Documents.Add strTemplatePathName
    End Sub
    </pre>

    Where you are passing in the full template path and name as the argument.

    In your example code you are building the template path and name from three different arguments; you can either adjust the code illustrated here to reflect the three arguments rather than one, or alternatively you can have another preliminary function which takes the three path arguments, concatenates them, and provides as a return value the full template path and name to be passed into CreateNewDocument.

    (One thing that has not been discussed in this thread is: how are you getting the template path/name arguments in the first place? Presumably you have a custom userform that gives the user the option of which of the 32 templates they want to create a new document from, or perhaps alternatively you have a custom toolbar with buttons or menu items from which they can select a template to use. In either case, you have code running, based on the user selection, which determines the template path/name arguments to feed into the CreateNewDocument procedure - correct?)

    You might want to add code into CreateNewDocument to call a function which confirms that the template being referred to actually exists (more of an issue if the templates are being stored on individual machines rather than the network, but a good idea even in the latter case).

    And of course the CreateNewDocument procedure has to be located in a global add-in template, preferably residing in the Startup directory so that it is always available when Word starts up.

    There isn't much more to it than that....

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How does one make the added doc the active one? (Word 2000/SR1)

    Try changing your code as follows (note: this was not tested in VBE, just typed here):

    Dim docNew as Word.Document
    Set docNew = objWorkpaperWord.Documents.Add(arrFilePaths(iSelec tedTitle) & "" & WorkpaperTemplates(iSelectedTitle, iSelectedCategories, iSelectedWorkPaper))
    With docNew
    .ActiveWindow.Caption = strDocname
    .Activate
    End With

    Does that help?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •