DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Open 'always on top' Form in MS Word using VBA script?

  1. #1
    Join Date
    Sep 2008
    Posts
    11

    Thumbs down Open 'always on top' Form in MS Word using VBA script?

    Hey,

    I have this problem with this vba script in MS word that I use in the office. A button calls for a Form to be loaded, and sometimes the form gets hidden behind the MS word window, and the only way to get to it, is to hit alt-f4 to close that form, and restart word, because of some code i guess that sets the focus to that form and disables the other functions of word like minimize and exit, until that form is unloaded.

    What code can i put in the initialize section of the form to make it come up as always on top? I really hope you can help, I've been tryin different things but can't seem to get it.

    Thanks,

    Brad

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Welcome to DevX

    Have you tried showing it Modal?

  3. #3
    Join Date
    Sep 2008
    Posts
    11
    it is in modal mode

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    I've never been able to get anything on top of a modal form that I've shown. In fact, I seldom can even click anywhere else.

    What code are you using to show the form modal?

  5. #5
    Join Date
    Sep 2008
    Posts
    11

    Exclamation

    Code:
    Sub ShowCreateNewDoc()
        CreateNewDoc.Show
    End Sub
    
    Private Sub UserForm_Activate()
        InitializeFormElements
        'OrderNumberBox.Enabled = True
        'OrderNumberBox.SetFocus
    End Sub
    
    Private Sub InitializeFormElements()
        DocTypeCombo_Change ' To update any field enablements.
        ContinueButton.Enabled = False
        RetrieveButton.Enabled = False
        DocumentTypeLabel.Enabled = False
        DocTypeCombo.Enabled = False
        SubTypeLabel.Enabled = False
        SubTypeCombo.Enabled = False
        GuaranteeLabel.Enabled = False
        GuaranteeBox.Enabled = False
        SignatureLabel.Enabled = False
        SignatureCombo.Enabled = False
        LimitedEndorsementCheckBox.Enabled = False
        InclTSGSummarySheetCheckBox.Enabled = False
        ClientNameText.Caption = ""
        ClientRefText.Caption = ""
        OrderTypeText.Caption = ""
        BorrowerText.Caption = ""
        StateText.Caption = ""
        CountyText.Caption = ""
        TitleEntityText.Caption = ""
        JacketText.Caption = ""
        OrderNumberBox.Enabled = True
        OrderNumberBox.SetFocus
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim SysDate As Date
        
        SysDate = Date
        YearStr = Format(SysDate, "yyyy")
        MonthStr = Format(SysDate, "mm")
        DateDir = MonthStr & "-" & YearStr & "\"
        
        ReadDefaultSelections
        LoadDocTypeComboBox
         OrderNumberBox.Enabled = True
        OrderNumberBox.SetFocus
    End Sub
    Once again, the problem is that only once in a while, when trying to pull up the 'create new doc' form, the form loads behind the MS Word window, and all other functions are disabled until that form is closed (manually using alt-f4 as there is no other way to see the forms exit/cancel buttons)

    If it helps, I use dual monitors in this office. I first thought it may be something to do with that. I was able to make a string that minimized the ms word window upon createnewdoc.show and maximize upon the initialize form stage, but this created an error where some people in the office using an excel spreadsheet and using a custom filter option, the excel window would minimize itself and is a nuissance.

    I really hope there is a way to make this userform load always on top, and if it happens to get stuck behind the msword, i need some way that I can get it to the front of the window without hitting alt-f4 and restarting MS word.

    Thanks,

    Brad

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    This
    Code:
    Sub ShowCreateNewDoc()
        CreateNewDoc.Show
    End Sub
    is not showing a form modal. That is showing a form normal.

    This is showing a form modal...try it.
    Code:
    Sub ShowCreateNewDoc()
        CreateNewDoc.Show vbModal
    End Sub

  7. #7
    Join Date
    Sep 2008
    Posts
    11
    the problem still happens once in a while. the ms word window just wants to hide my forms sometimes i wish there was a string of code i could put, to refresh the form or something to make it appear on top of the ms word window if it gets hidden behind it

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    I would say that from all the time to once in a while is progress.

    Lets try another approach using a Windows API
    Code:
    Option Explicit
    
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
       ByVal hWndInsertAfter As Long, _
       ByVal x As Long, _
       ByVal y As Long, _
       ByVal cx As Long, _
       ByVal cy As Long, _
       ByVal wFlags As Long) _
       As Long
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) _
    As Long
    
    Private Sub UserForm_Activate()
    Dim hwnd As Long
    hwnd = FindWindow("OpusApp", vbNullString) 'the class name for MS Word is "OpusApp"
    Call SetWindowPos(hwnd, -1, 0, 0, 0, 0, 3)
    End Sub

  9. #9
    Join Date
    Sep 2008
    Posts
    11

    Thumbs up

    thanks, i will input the code tomorrow morning when i get back to the office..

    i appreciate the support

  10. #10
    Join Date
    Sep 2008
    Posts
    11
    i think this works, it definately calls focus to the form, but it nulls my textbox.setfocus string. the window appears in front of ms word, but out of focus (need to manually click the text box to start typing)

    ive tried putting setfocus in mutiple strings to figure out how to work it, any tips?

  11. #11
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    I see what you mean.

    I moved the code from UserForm_Activate to UserForm_Initialize() and that seem to fix the problem.

    Give that a try and let me know.

  12. #12
    Join Date
    Sep 2008
    Posts
    11
    got promoted to QC Manager..

    currently no time to dabble in this little side project.. but i see you have fixed the problem, i appreciate it alot!

    I.T. should be happy when i get a chance to upload to the server

    thank you !

  13. #13
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by buradd
    got promoted to QC Manager..
    Congratulations!

Similar Threads

  1. Replies: 4
    Last Post: 06-04-2008, 02:28 PM
  2. Open a web page in MS Word
    By ken_rgr in forum ASP.NET
    Replies: 1
    Last Post: 03-07-2008, 08:19 AM
  3. submit form & open page with 1 button
    By Gregg in forum Architecture and Design
    Replies: 1
    Last Post: 05-17-2001, 11:44 AM
  4. map areas not passing Form object to java script
    By mark hembree in forum ASP.NET
    Replies: 1
    Last Post: 01-11-2001, 11:21 AM
  5. Open Access database Form from VB 6.0?
    By Demo in forum VB Classic
    Replies: 7
    Last Post: 07-25-2000, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center