Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2011, 04:15 AM
SaneMan SaneMan is offline Setting focus to specific word document from UserForm Windows 98/ME Setting focus to specific word document from UserForm Office 2003
Novice
Setting focus to specific word document from UserForm
 
Join Date: Jan 2011
Posts: 20
SaneMan is on a distinguished road
Default Setting focus to specific word document from UserForm

Hello



I am having some trouble with the set focus command from my userform. This userform is going to be used by a number of people in my office. They are filling in the text boxes on the userform and after they press 'ok' this pastes the values as variables into a word document the form comes from which has a list of all the different letters/correspondence we use.

At the moment when the user clicks 'ok' it sets the focus to the active document and then pastes the variables in. This works fine as long as there are no other word documents open as the active document is the word document the form comes from. However, the problem is encountered when another word document is running at the same time as it tries to paste the variables into the other word document.

I'm sure there's a way of setting the focus to a specific word document by stating the document's title somewhere in the Macro. However, even this would not be enough as the user will be naming the original word document with a specific reference number each time!

Is there any other way around this? Is there a way of setting the focus to the document that the form comes from? Or a way of making the word document a variable so the form always knows to paste the fields into this one document?

I know there's a way round it but I'm not smart enough to figure it out it seems!

Thanks a lot.
Reply With Quote
  #2  
Old 03-31-2011, 08:03 PM
macropod's Avatar
macropod macropod is offline Setting focus to specific word document from UserForm Windows 7 32bit Setting focus to specific word document from UserForm Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi SaneMan,

Assuming your Userform is supposed to update its own document only, the simple solution is to use 'ThisDocument' instead of 'ActiveDocument'. Alternatively, you could set a reference to the document when the UserForm is first opened, then use that for subsequent updates. For example:
Code:
Option Explicit
Dim DocFrm As Document
 
Private Sub UserForm_Initialize()
Set DocFrm = ThisDocument
End Sub
 
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With DocFrm
'code to update the document goes here
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-01-2011, 01:54 AM
SaneMan SaneMan is offline Setting focus to specific word document from UserForm Windows 98/ME Setting focus to specific word document from UserForm Office 2003
Novice
Setting focus to specific word document from UserForm
 
Join Date: Jan 2011
Posts: 20
SaneMan is on a distinguished road
Default

That works perfectly! Massive thanks.

One problem though... it seems to have affected how the minimize function works on the form. We don't want the user to actual see the word document and use the form for all of the letter generation functions, so in the Initialize macro we have Word.ActiveDocument.WindowState = wdWindowStateMinimize. We also have minimize buttons on the form which minimize both the form and the word document and this is Word.ActiveWindow.WindowState - wdwindowStateMinimize.

Before I inputted your macro this worked so when we open the document it immediately loaded the userform and minimized the word document to make it inactive. It also meant when you're looking at other word documents it took the focus to them but then when you clicked on the userform document tab at the bottom it would go straight back to the userform with the 'mother' word document minimised.

Now when we load the document it still mimizes the word document correctly, but as soon as you look at another word document it maximizes the userform word document. It also stops the Minimize feature on the userform working correctly - you can click the mimize button to minimize the form, but now when you click on the tab at the bottom to re-activate the word document/userform it brings up the word document but does not re-load the userform.

Any way around this?

Many thanks again. Never would have figured it out myself!

Edit - forgot an important point. When I first made the changes you suggested and opened up the userform it did not minimize the word document. So I looked through the macros and realised Word.ActiveDocument.WindowState no longer worked. So I had to change it to Word.ActiveWindow.WindowState. Somehow adding the DocFrm as a dimension has stopped the active document window state being able to be affected. I think this is the reason when I click onto another word document it stops the userform word document from being minimized..
Reply With Quote
  #4  
Old 04-01-2011, 02:16 AM
macropod's Avatar
macropod macropod is offline Setting focus to specific word document from UserForm Windows 7 32bit Setting focus to specific word document from UserForm Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi SaneMan,

Instead of using 'ActiveWindow.WindowState = wdWindowStateMinimize' I'd suggest coding the 'UserForm_Initialize' sub as:
Code:
Private Sub UserForm_Initialize()
Set DocFrm = ThisDocument
DocFrm.ActiveWindow.Visible = False
End Sub
This will keep the document hidden during use unless you change its 'Visible' property.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-01-2011, 05:47 AM
SaneMan SaneMan is offline Setting focus to specific word document from UserForm Windows 98/ME Setting focus to specific word document from UserForm Office 2003
Novice
Setting focus to specific word document from UserForm
 
Join Date: Jan 2011
Posts: 20
SaneMan is on a distinguished road
Default

Hi macropod

Many thanks for all of your help on this. Unfortunately I think because of some of the functions of the userform I am creating this code won't wont. This is because there are buttons on the form which are copying letters/correspondence from the activedocument and then pasting them into a new word document. And using this code stops the letters from appearing in a new document as I assume they are not visible along with the DocFrm.

It also means that when I press the 'minimise' button on the userform it stops it re-appearing when I select the word document from the task bar. at the moment the minimise function says 'Me.Hide' and before I tried the new codes minimising the form and then clicking the word tab at the bottom brought the form back up, but now it only brings up the word document and not the form.

Is there a way of creating a maco so that when you activate the the word document (by clicking it from the task bar once the form is minimized) it automatically shows the form? I can't think of a logical place to put a macro such as this, unless it's in the ThisDocument macro list which I don't know much about...

Thanks!
Reply With Quote
  #6  
Old 04-01-2011, 03:11 PM
macropod's Avatar
macropod macropod is offline Setting focus to specific word document from UserForm Windows 7 32bit Setting focus to specific word document from UserForm Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi SaneMan,

Without seeing your code, it's hard to say exactly what the issues are. I would have thought that, if you correctly substituted 'ThisDocument' or the 'DocFrm' calls for 'ActiveDocument', everything would have worked the same as it did before. In my testing with the original code, the only thing that causes the DocFrm document to re-appear of its own accord is maximising another document you might have opened over it, then closing that document.

As for:
Quote:
using this code stops the letters from appearing in a new document as I assume they are not visible along with the DocFrm
I have no trouble opening, displaying & editing another document whilst the one containing the userform (which I'm running in modeless state) is hidden and the useform is visible.

Equally, it shouldn't be necessary to have the document visible to update it through code (eg I have some test code to update bookmarks in the document and it works fine when the document's not visible). I do accept though that, if you hide a userform attached to an invisible document, regaining access to the userform would be problematic - you'd need the 'hide' code to activate another small form with a button to restore the original one.

Finally, regarding:
Quote:
Is there a way of creating a maco so that when you activate the the word document (by clicking it from the task bar once the form is minimized) it automatically shows the form?
I imagine you could create a class module and use the 'Public WithEvents wdApp As Word.Application' declaration to give you access to the Window Activation, etc events, but I have no experience with coding for those events.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook userform validation help aiwnjoo Outlook 0 12-08-2010 12:57 AM
Setting focus to specific word document from UserForm Checkbox on Userform result in Text in Word Dolfie_twee Word VBA 1 06-22-2010 07:54 AM
Create Hyperlinks from Word to specific location in PDF sukanyae Word 0 02-25-2010 04:08 PM
Help with forms (shifting focus) jrh312 Word 0 11-19-2009 12:59 PM
[Word 2003] Macro's and a UserForm xanuex Word VBA 0 10-19-2009 05:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft