Solved

URGENT: open word application by impersonation

Posted on 2012-03-14
11
511 Views
Last Modified: 2012-03-20
Hallo experts,

I am using the following code to open a word document. Now my problem is in the line

 objApp = Activator.CreateInstance(objClassType);

I want to impersonate that with a domain, uid and pwd. How to do it? Means I want to open the document with some different uid & pwd.


Please help

Type objClassType;
object objApp;

public bool Open()
        {
            try
            {
                object missing = Missing.Value;

                // Get the class type and instantiate Word.
                objClassType = Type.GetTypeFromProgID("Word.Application");
                objApp = Activator.CreateInstance(objClassType);

                //Get the document collection.
                objDocs = objApp.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, objApp, null);

                //Add a new workbook.                
                Parameters = new object[12];
                Parameters[0] = sFilePathWithFileName;
                Parameters[1] = missing;
                Parameters[2] = Missing.Value;
                Parameters[3] = false;
                Parameters[4] = missing;
                Parameters[5] = missing;
                Parameters[6] = missing;
                Parameters[7] = missing;
                Parameters[8] = missing;
                Parameters[9] = false;
                Parameters[10] = missing;
                Parameters[11] = missing;
                objDoc = objDocs.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, objDocs, Parameters);

                //Return control of Word to the user.
                Parameters = new object[1];
                Parameters[0] = true;
                objApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, objApp, Parameters);

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
0
Comment
Question by:Soumen-Roy
  • 5
  • 4
  • 2
11 Comments
 
LVL 26

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 100 total points
ID: 37719106
0
 

Author Comment

by:Soumen-Roy
ID: 37719449
@sinisav:

No I tried to impersonate before open method is called. Impersonation was successful but open method can not open the document because of insufficient permission. Probably at time of opening the doc some parameter is required related with impersonation.

regards,
soumen
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37719513
Have you tried impersonating before creating the Word instance? That should make word run under that different user and be able to access the file.

Another option is to copy file locally and then open in Word.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Soumen-Roy
ID: 37723356
@CodeCruiser:

Yes I tried to impersonate before creating the word instance. But still it is not working. Yes copy file is a good option. Let me check that. But each and every time copying each and every file may lead the operation of file opening slow. Still let me check, give me some time
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37724279
Ok. When you impersonate and run Word, is word running under impersonated user in task manager?
0
 

Author Comment

by:Soumen-Roy
ID: 37727795
@CodeCruiser:

No,

When I impersonate and run Word, is word running under impersonated user in task manager

Actually that is the problem

Soumen
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 37728150
So... it is impersonate code problem. Can you drop some notice how you do it?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37730340
Try doing a process.start with the impersonated user credentials.
0
 

Author Comment

by:Soumen-Roy
ID: 37736369
@CodeCruiser:

After impersonation if I start process.start to open word doc, then the process is starting by impersonated user. So impersonation code is OK.

But instead of process.start if I use code given above, to open word application, the impersonation fails. It is the problem actually. I can not use process.start. But I have to access the word application objects to insert some text and formatting in that word document. Thats why I can not user process.start. I need word object and sub objects like region, paragraph etc.

Soumen
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 400 total points
ID: 37740212
Then the best option, I think, is to copy the file locally.
0
 

Author Closing Comment

by:Soumen-Roy
ID: 37741130
Thanx
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TimeZone, day light savings, Sql server, asp.net 6 37
parsing JSON help 1 20
Updating specific fields only in MVC/Entity controller 2 18
Input parameteres to DragOver 2 19
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question