Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
M using folloing code but I get Sysytem.windows.form AxHost.InvalidActiveXStateException error..
C#
OpenFileDialog openFileDialog1 = new OpenFileDialog();
            //openFileDialog1.InitialDirectory = Application.StartupPath + "\\Reports\\";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Filter = "Pdf Files|*.pdf";
                try
                {
                   if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                     
                        AxAcroPDF a = new AxAcroPDF();
                        a.LoadFile(Application.StartupPath + "\\Reports\\");
                        a.Show();
                                        
                    }
                }
                catch (ArgumentException  ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
Posted
Updated 14-May-17 21:07pm

Hello Dipika.

I think you want to Open the PDF-File inside Acrobat ?

That Code seems allright to me. But the message says it was not in the right [mood ;)] State.

If it is only needed to display the PDF file use the .NET builtin Webrowser and pass that file to that Control. Ist so frustrating with ActiveX Interop.

Maybe i didnt solved that Code issue but for a PDF Reader there is no need for that task anymore.

c.u. an happy coding
 
Share this answer
 
For each and every active x control, it is needed to create it first, so that all the events and handles should be initialized.

try.. :)

C#
AxAcroPDF.CreateControl()


[Solved] System.Windows.Forms.AxHost.InvalidActiveXStateException was unhandled[^]

AxHost.InvalidActiveXStateException[^]
 
Share this answer
 
Comments
Dipika Wani 4-Aug-14 1:09am    
@nirav prabtani- I used "axAcroPDF1.CreateControl();" before loading file from path but still getting same error that " Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown."
please help me. If there any other option then plz tel me.
Dipika Wani 4-Aug-14 4:36am    
please reply me some one. Its urgent.
Hello again.

C#
AxAcroPDF a = new AxAcroPDF();
                    a.LoadFile(Application.StartupPath + "\\Reports\\");
                    a.Show();


Your Variable/Instance is named a

I think you need to try this instead.
C#
a.CreateControl()
a.LoadFile(Application.StartupPath + "\\Reports\\");
a.Show();


AxAcroPDF is the Type of Control, and a is the Instance, which will be shown with the selected PDF document.

Wrong:
axAcroPDF1.CreateControl();


Right
a.CreateControl();


If the Code in the first Question doesn´t changed meanwhile.

...even if this is Answer is marked as solved. Don´t hit me with that thing... ;)

c.u.
 
Share this answer
 
I want to add something to the solution:

I get the filename from:

C#
string[] DirectoryFiles = System.IO.Directory.GetFiles(DirectoryName);


When I use this:
a.CreateControl()
a.LoadFile(DirectoryFiles[0]);
a.Show();

I get the error mentioned in this question.

The solution is to put an @ in front of the file to be loaded:

a.CreateControl()
a.LoadFile(@DirectoryFiles[0]);
a.Show();


As this is the top result when googling for this error i hope I can help people like me who forgot the @ is necessary.
 
Share this answer
 
Comments
CHill60 15-May-17 8:16am    
You do not need to use the "@" character unless you have a literal string.

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