Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been programming one way or another for over 40 years, but am quite a rookie on VB.net, so I still have trouble getting my head around things, hence my probably very simple question ...

I have the following code that works, but my Form is reappearing on top of the just opened Word document. How can I make the Word document appear on top ?

In my Form Design, TopMost is set to False ...

I am still in Debug mode, but presume that makes no difference ...

Thanks in advance ...


VB
<pre>Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop

Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then
            Form3.TextBox1.Text = "Error, you have not entered a name for this Artist or Band ..."
            Form3.TextBox2.Visible = False
            Form3.Button2.Visible = True
            Form3.AcceptButton = Button2
            Form3.Show()
            Exit Sub
        End If
        MusicFile = TextBox1.Text & ".docx"
        If System.IO.File.Exists(MusicFolder & MusicFile) Then
            Form3.TextBox1.Text = "Error, you already have an existing file for this Artist or Band ..."
            Form3.TextBox2.Text = "Do you want to open that file or go back to the previous screen ?"
            Form3.Button1.Visible = True
            Form3.Button3.Visible = True
            Form3.AcceptButton = Button1
            Form3.Show()
        Else
            If SheetOrientation = "Portrait" Then
                System.IO.File.Copy(MusicFolder & "Portrait.docx", MusicFolder & MusicFile)
            End If
            If SheetOrientation = "Landscape" Then
                System.IO.File.Copy(MusicFolder & "Landscape.docx", MusicFolder & MusicFile)
            End If
            System.IO.File.SetAttributes(MusicFolder & MusicFile, fileAttributes:=vbNormal)
            WordApp = CreateObject("Word.Application")
            WordApp.Visible = True
            WordDoc = WordApp.Documents.Add(MusicFolder & MusicFile)
            Me.Close()
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class



What I have tried:

I have tinkered and tried various changes, but I obviously dont know what I'm doing !!
Posted
Updated 24-Nov-20 4:04am
Comments
Ralf Meier 24-Nov-20 7:31am    
I don`t understand what you mean - perhaps a Problem of translation ;-)
You speak abou Form2 I think.
Where should it stay ?
How do you want to bring it to foreground if you want to use it ?
Perhaps the Form-Property WindowState is the thing you are searching ...
Gary Heath 24-Nov-20 7:40am    
Ralf ... Form1 has a ComboBox that is loaded with 3 options for the user, (1) Open an existing file, (2) Create a new Portrait File, (3) Create a new Landscape File. Form2 opens if option 2 or 3 is chosen.

Form2 just contains a TextBox for the user to enter the name of his Artist or Band and the code I have supplied is the Code that sits behind Form2. Form3 is an Error box so can be ignored.

If option 2 or 3 is selected to create a new document, I create a new Word document in the Orientation he has requested and open it for him to enter data ... this all worked fine when I was opening an Excel file, I have just realised, but now I have switched it to a Word file, Form1 pops up in front of Word and that is all I want to prevent.

This is just a small program for an old man who has brain damage I am trying to make it easy for him to do what he wants to do, it's so frustratingly annoying to run into such a stupid problem when it's basically finished !!!

I think I looked at WindowState yesterday, but I may not have, there are so many thousands of options and combinations I may be mistaken ... I'll look at it again now ...
Gary Heath 24-Nov-20 7:51am    
If I put "Form1.WindowState.Minimized" straight after the line "WordDoc = WordApp.Documents.Add(MusicFolder & MusicFile)" I get an error : "Expression is not a method" :-(
Ralf Meier 24-Nov-20 9:42am    
Hi Gary,
WindowState is a Property - you must assign the value "Minimized" to it.
With your last comment I suppose you made something wrong - but ... perhaps you modify your question with the "Improve question" widget and provide this part of code ...
Ralf Meier 24-Nov-20 9:43am    
Also ... if you answer my comment you should use the "Reply" widget from my comment. In this case I get a notification ... ;-)

Try TopLevel in combination with TopMost, Topmost is application-specific, not desktop.

Form.TopLevel Property (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
v3
Comments
Gary Heath 24-Nov-20 5:57am    
I can only find SetTopLevel and if I set that to False I lose visibility of the Form completely !!
I have added a WindowsState line, as suggested by @Ralf_Meier, and it now works :-)

Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop

Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then
            Form3.TextBox1.Text = "Error, you have not entered a name for this Artist or Band ..."
            Form3.TextBox2.Visible = False
            Form3.Button2.Visible = True
            Form3.AcceptButton = Button2
            Form3.Show()
            Exit Sub
        End If
        MusicFile = TextBox1.Text & ".docx"
        If System.IO.File.Exists(MusicFolder & MusicFile) Then
            Form3.TextBox1.Text = "Error, you already have an existing file for this Artist or Band ..."
            Form3.TextBox2.Text = "Do you want to open that file or go back to the previous screen ?"
            Form3.Button1.Visible = True
            Form3.Button3.Visible = True
            Form3.AcceptButton = Button1
            Form3.Show()
        Else
            If SheetOrientation = "Portrait" Then
                System.IO.File.Copy(MusicFolder & "Portrait.docx", MusicFolder & MusicFile)
            End If
            If SheetOrientation = "Landscape" Then
                System.IO.File.Copy(MusicFolder & "Landscape.docx", MusicFolder & MusicFile)
            End If
            System.IO.File.SetAttributes(MusicFolder & MusicFile, fileAttributes:=vbNormal)
            WordApp = CreateObject("Word.Application")
            WordApp.Visible = True
            WordDoc = WordApp.Documents.Add(MusicFolder & MusicFile)
            Form1.WindowState = FormWindowState.Minimized <<< This line is added
            Me.Close()
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class
 
Share this answer
 
Comments
Ralf Meier 24-Nov-20 10:45am    
You are welcome ...
I was not sure if this could be the Solution for you - otherwise I would have posted it as Solution by myself ... ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900