System.Threading.ThreadstateException - vb.net

I'm developing an adding for office powerpoint application.
I'm trying to display a description of the object(Customized object) currently dropped on the powerpoint slide in design mode(Design mode of the powerpoint).
When i click on my addin the related object description will be displayed on a tabbed window as the first tabpage.
There is a button on the tab page, and when i click on it i need the description to get copied to windows clipboard.
I tried this using clipboardclass it throws the following exception,
System.Threading.ThreadstateException
{"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."}
Code for clipboard:
Clipboard.Clear()
Clipboard.SetText(lblObjectID.Text)
I searched the net for a solution and got couple of answers like,
1. Put [STAThread] in the main function
2. Thread.CurrentThread.SetApartmentState(ApartmentState.STA) Immediately before your call to SetDataObject.
But I'm not sure where to put the 1st one and the 2nd option didn't work.
Can anyone help me please.
Thanks.

WinForms are STA by default. Are you creating another thread or using a BackgroundWorker? Run this code to determine what mode you're in:
MessageBox.Show(System.Threading.Thread.CurrentThread.GetApartmentState().ToString())
Edit:
But maybe you could also try using this command before calling the clipboard functions:
Application.OleRequired()

Related

Word 2013 activate "New" screen from VSTO

I'm developing a Word AddIn, and I've run into a bit of a problem.
I need to modify the BuiltIn BuildingBlocks, and to do that, I need an active document.
Since Word 2013 no longer starts with an open document, but instead shows the "New (Latest)" screen, my AddIn creates a new temporary document, modifies the BuildingBlocks as it should, and then discards the temporary document.
The problem is that the act of opening a new document and closing it again, leaves the Word UI at the Home-tab with no active document open, instead of on the "New" screen as it normally would.
So my question is: How do I make Word switch back to the "New" screen?
Here is the before screen, showing normal Word 2013 behaviour on startup:
Here is the after screen, showing how it ends up now, because I open/close the document at startup:
Sorry about the danish screenshots, but the principle should still be clear.
I'm using VB.Net, but C# code can be used too, since it is easily translated.
There is no way to show the Startup screen again. Instead, you may consider running a new Word instance.
Be aware, the Word object model provides the ScreenUpdating property of the Application class which you may find helpful. Here is what MSDN states:
The ScreenUpdating property controls most display changes on the monitor while a procedure is running. When screen updating is turned off, toolbars remain visible and Word still allows the procedure to display or retrieve information using status bar prompts, input boxes, dialog boxes, and message boxes. You can increase the speed of some procedures by keeping screen updating turned off. You must set the ScreenUpdating property to True when the procedure finishes or when it stops after an error.
You may also create a template file with building blocks, then attach the template on top of the new document
It will solve you problem

VBA Powerpoint Macro: Run in background

Can a macro run in the background whilst the user is using the presentation?
For example, some sort of auto-correct function or auto-format function>
This is very much possible. I have created a small demo to show how it can be done.
In this demo, I have placed a command button and whenever it is clicked (in presentation mode), it displays a message.
If I really needed to do something like this for some reason, I would probably add a form, put any code that needs to run concurrently in the form, have it run as part of the form's initialization subroutine, then from the main body of code, call the form modelessly.

Changing form name causes error message "Error accessing the system registry"

I am programming the On_Click method for the button labeled "View" in the first printscreen below. The method will load a form with data corresponding with the specific address id in the row containing the "View" button. This code worked perfectly when pointing to a target form called "Addresses". However, when I decided to rename the form "Address", I started receiving the following error message when clicking on the view button:
I did some research on the web about this error message, which lead me to try to delete any outdated references in the VBA editor. But when I clicked on Tools-->References in the VBA editor, I got the following error message:
It seems that MS Access' entries in the system registry might have been corrupted. But I am not certain of this because the documentation of this on the web is sparse and inconsistent.
Here is a link to the database on a file sharing site: http://jmp.sh/b/9Uyx6J2YzWbs8zPq2h6g
If the problem is in the database, you can recreate the problem by opening the form "Main" and clicking on the button "View" for the record shown in the printscreen above, or for other records.
Can anyone show me how to get past this error message?
my advices?
You could rename your Forms!....SourceObject to "Address" instead of "Addresses"
You could copy/paste the form, delete the original, and retry
You could install a decent debugging tool like MZ-Tools for VBA, that will help you manage your errors. Check the details here
EDIT: I used to get similar bugs years ago, when I was writing specific form event procedures. As we decided to switch to a model where forms did not need to be debbuged anymore (check this here), we stopped getting this kind of message. And I think I forgot the trick we used to solve this kind of error. If I were you:
I'd try to open another access database and import all objects ...
I'd put aside/cancel the faulty onClick procedure
And I'd install MZ Tools because otherwise VBA debugging is a nightmare ...
Since access was not liking the command button in each record of the continuous form, I choose to put the view button's logic in the on click event of a text box in each row, which I configured as enabled but locked. This produces a separate link from each record of the continuous form to a unique detail page with more of the chosen record's data.
This solution works perfectly, and is enabling me to move on with my other coding.
However, it would be nice if someone else were able to show how to get the command button solution to work.

Issue with OpenFileDialog and threading

I just upgraded from VS 2005 to VS 2012. This is a new issue that I do not understand. I am using the default "Form1" class the VS automatically creates. I added a button to open a file open dialog and when I click the button I get this error:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be >made. Ensure that your Main function has STAThreadAttribute marked on it. This exception >is only raised if a debugger is attached to the process.
I have added " to Public Class Form1:
<STAThread()> Public Class Form1
But I get this...
Attribute 'STAThreadAttribute' cannot be applied to 'Form1' because the attribute is not >valid on this declaration type.
I have searched but get some info telling me that I need to set the entry point (Form1 I believe) to be Single Thread Attribute but the above code does not work.
How?
The <STAThread()> attribute cannot be added to classes like your form. It only works when it is applied to the Main function, which is the entry point of your application.
But VB.NET hides this function from you because it is rare that one needs to mess with Main in a WinForms application. It is just needed to get the plumbing set up for your app, which the compiler can manage for you. This is controlled by the "Application Framework" checkbox in the project options. If this is checked, the compiler automatically generates the Main function and the required plumbing. You can disable this option, but it makes life quite a bit harder for the average WinForms developer because you'll have to write and maintain your own Main function.
The real question here is why this is a problem at all. The compiler-generated Main function for a WinForms application is always going to have the STAThread attribute applied to it. That is just how the WinForms framework is designed to run. If that is not happening, then there is something badly wrong with your project. I would recommend scrapping it and starting over letting Visual Studio create a new WinForms project from one of the built-in templates. Everything should Just Work™.
The other option, of course, is that you're trying to display the OpenFileDialog on a separate thread (other than your main UI thread). But from your description in the question (adding a button to the form to display the dialog), it doesn't sound like this is the case. Regardless, the solution is not to do that. For example, if you're using a BackgroundWorker to do work on a non-UI thread in order to keep the UI responsive, that's great, but you'll want to do all of the UI stuff like showing an OpenFileDialog on the main UI thread before invoking the BackgroundWorker. There is a way to set a particular thread's apartment state using the SetApartmentState function, but I really don't recommend showing an OpenFileDialog on a background thread.

Controls in designer are not shown on form

I have a panel contains many controls, the designer file has its code and I can not find them on the form and I can not see them on document outline window although when trying to add a new panel with same name I get an error saying 'The name wowPanel is already in use by another component.'
What can I do to resolve this issue?
I replaced my designer file with an old version then everything goes very well.
Look for all instances in your form code where you have the name/text wowPanel
REM out these lines temporarily
REM out any subroutines if you have event subroutines for the wowpanel
Next add the new wowPanel and Name it as so "wowPanel"
Now UN-REM (un-remark) all the code statements you REMmed out earlier.
That ought to do it.