• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

URGENT: open word application by impersonation

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
Soumen-Roy
Asked:
Soumen-Roy
  • 5
  • 4
  • 2
2 Solutions
 
Sinisa VukCommented:
0
 
Soumen-RoyAuthor Commented:
@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
 
CodeCruiserCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Soumen-RoyAuthor Commented:
@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
 
CodeCruiserCommented:
Ok. When you impersonate and run Word, is word running under impersonated user in task manager?
0
 
Soumen-RoyAuthor Commented:
@CodeCruiser:

No,

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

Actually that is the problem

Soumen
0
 
Sinisa VukCommented:
So... it is impersonate code problem. Can you drop some notice how you do it?
0
 
CodeCruiserCommented:
Try doing a process.start with the impersonated user credentials.
0
 
Soumen-RoyAuthor Commented:
@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
 
CodeCruiserCommented:
Then the best option, I think, is to copy the file locally.
0
 
Soumen-RoyAuthor Commented:
Thanx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now