How to bring word application to the front in vb.NET - vb.net

I am using winforms and when I try to open a word document programmatically it goes behind all the opened windows randomly. How can I bring it to the front? Following is my code.
Dim pAppWord As Object = CreateObject("Word.Application")
'//do my stuff
pAppWord.Visible = True
pAppWord.Application.Activewindow = True
'//pAppWord.Active() found this online but throws an error
pAppWord.WindowState = vbNormal
Thanks!!

What worked for me was
pAppWord.Activate()

This doen't seem trivial... maybe this one helps: http://social.msdn.microsoft.com/Forums/pl-PL/vblanguage/thread/fd9215cf-0893-4c61-886c-5d22fa7c2055

If you've added or opened a document (e.g. wordOb.Documents.Add) then you can simply pass wordOb.ActiveWindow.Hwnd to the Windows API function SetForegroundWindow.

Related

Error Printing: No application is associated with the specified file for this operation

I have a VB WinForms app that prints PDFs using Process.Start, and it has been working fine for ages on Windows 10. Today I go to use it and get the following error message:
"No application is associated with the specified file for this operation"
Nothing has changed to cause this, I have not changed PDF Viewer or uninstalled anything. I can open a PDF by double clicking on it no problems.
I wrote a small console app to replicate the issue and prove this had nothing to do with the WinForms app:
Sub Main()
Dim pi As New Diagnostics.ProcessStartInfo
Dim url As String = "C:\PathToPDF\.pdf"
pi.FileName = url
pi.Verb = "PrintTo"
pi.CreateNoWindow = True
pi.Arguments = """Microsoft Print to PDF"""
pi.UseShellExecute = True
Console.WriteLine(url)
Diagnostics.Process.Start(pi)
Console.ReadKey()
End Sub
The above program replicates the error. I have verified that the file exists and is accessible and tried it with double slashes and single slashes, they give the same error:
"No application is associated with the specified file for this operation".
The same application is working fine on other PCs in the office.
Has anyone else had this happen, and if so how did they fix it?
My PDF viewer is PDFXChange Viewer, and it has been working fine PDFs printed using this method for years. I have not updated the program, and it says in the about page that the last installed updated was in 2016.
Regards.
If I am right, I had the same issue. Maybe you can try it in a slightly other way. I have this in my application and it works fine:
Dim myp As New Process
myp.StartInfo.FileName = filename 'Full path to pdf
myp.Start()

Close MS Project using VSTO

I have a VSTO on MS Project. I use VB.NET. What I need is when I press the button I created on the ribbon, it will perform some codes which will update the info of some task, however, I would need to close the MS Project automatically. I tried application.FileCloseEx(), but it only closes the file, the MS Project is still loaded. I need similar to clicking the x button of the window.
Thanks,
Gilbert
If your MS Project application object is represented by "appMSProject" then it's as simple as:
appMSProject.Quit
OR say in a macro running under Project:
Application.Quit
Here's how I do it in VBA from Excel or Access. As far as I can tell the objects & methods are the same in VB.NET. Bottom line is that I create an instance of the MS Project object which starts the app & opens a file, execute some work, close the file, then destroy the MS Project object by setting it to Nothing. That has the effect of closing the app. You can also use "appMSProject.Quit" followed by setting it to Nothing. Frankly the 2nd option looks more orderly & easier to understand in code. Anyway, here's a sample of the way I do it:
Dim appMSProject As MSProject.Application
Dim prjPrj As MSProject.Project
Dim strPrjFile As String
strPrjFile = "C:\where_is_my_file\file_name.mpp"
Set appMSProject = New MSProject.Application
appMSProject.FileOpenEx Name:=strPrjFile
Set prjPrj = appMSProject.ActiveProject
'''Do something in here with the prjPrj
'Close the file, in my case w/o saving
appMSProject.FileCloseEx pjDoNotSave
'Destroy the objects
Set prjPrj = Nothing
Set appMSProject = Nothing
FYI - In this example I'm doing background work so I don't show the app. I also use "early binding".
Here's an MSDN example that does show the app with more info on early -vs- late binding - https://msdn.microsoft.com/en-us/library/office/ff865152.aspx

Send keypress to error messages on opening Word document

I am having some problems on sending key strokes to an error message while opening a document.
The document I am trying to open in a Word for Dos 5.5 document that is missing the style sheet, you can manually click ignore and the file opens, but I have 1000+ more file to do this with so i want to automate this.
I am opening this document up in either Office XP, 2003 or 2010 but i cant get the keystrokes to affect the messages.
The code I have been attempting is as follows:
Dim objWord As New Word.Application
Dim objDoc As New Word.Document
objDoc = objWord.Documents.Open(TempDir + ("\\" + fileInf.Name))
System.Threading.Thread.Sleep(5000)
SendKeys.Send("{Enter}")
System.Threading.Thread.Sleep(5000)
SendKeys.Send("{&I}")
System.Threading.Thread.Sleep(5000)
objWord.WindowState = Word.WdWindowState.wdWindowStateNormal
objWord.Visible = True
I have used the visible = true just to see what i going on, but the messages just sit there.
I think sending keyPress is not the best solution for your problem, and this is something you should avoid if you can.
You could instead try to hide Word messages. Possible ways:
Try to disable messages using Application.DisplayAlerts property before you open the file. So it would be:
objWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
Important note from MSDN :
If you set this property to wdAlertsNone or wdAlertsMessageBox,
Microsoft Word doesn't set it back to wdAlertsAll when execution
stops. You should write your code in such a way that it always sets
the DisplayAlerts property back to wdAlertsAll when it stops running.
Try to disable messages by setting other parameters of the Documents.Open method. I am thinking to the Format paramater that you could try to set to wdOpenFormatAllWord to see if it disables messages.
wdOpenFormatAllWord: A Microsoft Word format that is backward compatible with earlier versions of Microsoft Word.
When you write SendKeys.Send("{Enter}"), I guess you have System.Windows.Forms namespace imported so it is equal to System.Windows.Forms.SendKeys.Send("{Enter}").
In my first answer I have suggested, If you really need to use the SendKeys method, to use the one from Excel.Application. (because I was needing coffee..., I was totally focused on Excel although the question was about Word).
Unfortunately, the method is present In Excel but there is NO SendKeys Method in Word.Application Object.

How to programmatically open an Outlook Shortcut

I'm wondering if there is a way of programmatically opening an Outlook Shortcut from my addin.
I've created the shortcut as follows
Sub AddShortcut()
Dim myOlBar As Outlook.OutlookBarPane
Dim myolGroup As Outlook.OutlookBarGroup
Dim myOlShortcuts As Outlook.OutlookBarShortcuts
myOlBar = Application.ActiveExplorer.panes.Item("OutlookBar")
myolGroup = myOlBar.Contents.Groups.Item(1)
myOlShortcuts = myolGroup.Shortcuts
myOlShortcuts.Add("http://microsoft.com/", _
"MSHomepage", 1)
End Sub
I'm guessing I need to use InvokeMember in some way
myOlShortcuts("MSHomepage").GetType().InvokeMember(..)
But when I use GetType().GetMethods() I can't see any Click members or something similar. Any help is very much appreciated.
If you want to trigger the Outlook integrated web browser, you should grab the OutlookBarShortcut.Target. If the Target is of type string, then use the following (substituting your Target for the address Text)...
Office.CommandBarComboBox address = (Office.CommandBarComboBox)Application.ActiveExplorer().CommandBars.FindControl(26, 1740);
address.Text = "http://www.stackoverflow.com";
otherwise the type is Folder and you should assign Explorer.CurrentFolder. The only downside with this approach is that CommandBars have been deprecated with Outlook 2010 and this solution likely won't work in the next version of Office.
Another alternative is to use Web Folder behavior as discussed in this SO post. You could create a hidden Folder used just for the purposes of navigation.

Leaving a Project file open after retrieving it with GetObject

What is the right way to leave an MS Project file opened with GetObject() open and visible to the user after the end of a macro in a different app?
The information I found online suggests that setting the Application.UserControl property to True before the objects go out of scope should allow the user to continue using the opened file. However, for MS Project at least, the Application.UserControl property appears to be read-only. Is there a way to work around this?
A simplified example showing the problem:
Sub AddTasks()
Dim proj As Object
' Already have the file path from another part of the workflow
Set proj = GetObject("C:\projtest.mpp")
' perform some calculations and add new tasks to project
proj.Tasks.Add "additional task"
' Leave Project open and visible for the user
proj.Application.Visible = True
proj.Application.UserControl = True ' Gives "Type Mismatch" error
' without the UserControl line, runs ok, but Project closes after the end of the macro
End Sub
Instead of using GetObject, could you create an instance of the application and open the project file in the instance?
Sub AddTasks()
Dim msProj as Object
Set msProj = CreateObject("Project.Application")
msProj.FileOpen "C:\projtest.mpp"
'do stuff to project file here
msProj.Visible = True
End Sub
Something like the above (I can't test the above code because I don't have MSProject, but similar code works for MSWord)
For Project UserControl just indicates if the user started the application or not; it appears to be read-only because it is. I've not done what you're asking for with Project, although here is a similar example for Word trying to see and find running instances of Excel. Perhaps this helps a little:
can-vba-reach-across-instances-of-excel