السلام عليكم ورحمة الله وبركاتة

Hi All,

I have a small problem, I'm making a small application to change the attributes of a word documents so I created a word object and at the end of my code I called the Word._Application.Quit Method,
When I run the code on a word file for the first time, the attributes changes well.
The problem appears When I run the code on the same word file more than one time, the file attributes didn't change,

Can anyone help me :) ......

thanks in advance

Can you zip and attach the small application (project/solution) in question with any relevant word doc (is it in English?)?

thanks DdoubleD for your reply, kindly find attache the application which modify the word file attributes, you can create any word file to run the program on it.

I played around with this for a couple of hours--no success! :@

If all you are doing is changing the File Summary Properties, try using this alternative instead:

1) Download Microsoft Developer Support OLE File Property Reader 2.1 Sample
2) Add reference to the "dsofile.dll" to your project
3) Utilize this snippet of code:

private static void UpdateOleDocSummaryProperties(string fName)
        {
            //Create the OleDocumentProperties object.
            DSOFile.OleDocumentProperties dso = new DSOFile.OleDocumentProperties();

            try
            {
                //Open the file for writing...
                dso.Open(fName, false, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);

                //Set the summary properties that you want.
                dso.SummaryProperties.Title = "My Title";
                dso.SummaryProperties.Subject = "My Subject";
                dso.SummaryProperties.Company = "My Company";
                dso.SummaryProperties.Author = "My Name";

                //Save the Summary information...
                dso.Save();
            }
            catch (Exception e)
            {
                Console.WriteLine("Message: {0}\r\nStack: {1}", e.Message, e.StackTrace);
            }
            finally
            {
                //Close the file.
                dso.Close(false);
            }
        }

It is much faster too I think than the word interop stuff and without all the overhead and cryptic "crapiola". It also works with other documents so that's a plus!

Sorry I couldn't get your word interop stuff to work. You are doing everything according to the documentation that I can see, but they sort of left off the "Save" and "Quit" discussion. If you or someone else gets the interop code to work for this, please post the solution for everyone else.

thank a lot DdoubleD for your interest in my problem, i tested the code by your suggested library and he worked fine, and i also tried to solve my original code but it didn't work again :(
But, i have two issues regarding the new library,
the first one that, the Hyperlink base property not supported and
the other that, the library didn't create new files,
so i want to ask if you have a solving to these two issues,
thanks a lot in advance

thank a lot DdoubleD for your interest in my problem, i tested the code by your suggested library and he worked fine, and i also tried to solve my original code but it didn't work again :(
But, i have two issues regarding the new library,
the first one that, the Hyperlink base property not supported and
the other that, the library didn't create new files,
so i want to ask if you have a solving to these two issues,
thanks a lot in advance

When I view file properties, I don't see a Hyperlink property... What/where is this?

You can create a word .doc file simply by creating a file with that extension because word has extensive parsing to determine how to read the document, though when you save it from the word application it may no longer be easily recognizable as when you first saved it. I don't know what you are doing as far as "create new files", but I would think that if you were already doing that successfully, you could easily modify the properties afterward using the dsofile api?

Hello DdoubleD, Hope you are fine,

When I view file properties, I don't see a Hyperlink property... What/where is this?

you can find the hyperlink base property by opening a word file then select File then properties then select the summery tab then you will find the hyperlink base property there

You can create a word .doc file simply by creating a file with that extension because word has extensive parsing to determine how to read the document, though when you save it from the word application it may no longer be easily recognizable as when you first saved it. I don't know what you are doing as far as "create new files", but I would think that if you were already doing that successfully, you could easily modify the properties afterward using the dsofile api?

i can't understand you well, Can you explain more?, and What i need that i want to create new files from already existing templates and i don't want to change the properties of the templates so i want to create a new one to edit in it and by the way the saveas option also is not included in the dso library

I see the "Hyperlink base" property now... When you started the thread and I played around with the word interop code for some time, I also noticed the "author" property was the same whether modified in word or via explorer shell, so I decided to research an alternative and found dsofile... Anyway, when I looked for the "hyperlink" property last night, I only used the shell to display properties.

Bottom line is that I'm not sure how to get to that hyperlink, but I'll try to spend some more time on this later. In the meantime, go ahead and provide a list of any additional properties you are concerned with.

Also, have you gotten the interop code to create the word file from your existing templates OK? And, if you set those properties at the time you create the doc, does the doc retain the property settings you made?

thanks DdoubleD for your effort, "Thanks God"all my problems solved But the Hyperlink properties, if you can help
Regards

Back to where we started... You are correct that the DsoFile library does not support the "Hyperlink base" property. I tried to find the source code for this library, but it appears to no longer be available.

However, I did determine the reason for the original failure is due to automation client not realizing that a change has been made to the document and then, subsequently and apparently, ignoring the forced call to the _Document.Save() . I could not find a flag to manually set to indicate a change was made, so I added a modification to the document's content, followed by a call to undo the changes:

// Make a modification to the document because property changes don't 
                // seem to get recognized as changes and are thus ignored even when calling Save().
                doc.Content.InsertBefore("Temporary change...\r\n");
                // Undo the change above...
                doc.Undo(ref oMissing);

Now, the document will actually get saved with the desired property modifcations. Here is a working example-method to demonstrate this:

public static void ChangeHyperlinkBase(string docFilename, string hyperlink)
        {
            Word.Application wordApp;
            Word._Document doc;
            object oMissing = System.Reflection.Missing.Value;

            //Create an instance of Microsoft Word and make it visible.
            wordApp = new Word.Application();
            wordApp.Visible = false;

            try
            {
                object oFilename = docFilename;
                object ro = false;
                doc = wordApp.Documents.Open(
                    ref oFilename,
                    ref oMissing, ref ro, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing);

                object oDocBuiltInProps = doc.BuiltInDocumentProperties;
                Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

                // Retrieve the Hyperlink property object...
                string strIndex = "Hyperlink Base";
                object oHyperlinkBaseProp = typeDocBuiltInProps.InvokeMember("Item",
                                           BindingFlags.Default |
                                           BindingFlags.GetProperty,
                                           null, oDocBuiltInProps,
                                           new object[] { strIndex });
                // Retrieve the value of the Hyperlink property object...
                Type typeHyperlinkBaseProp = oHyperlinkBaseProp.GetType();
                string strValue = typeHyperlinkBaseProp.InvokeMember("Value",
                                           BindingFlags.Default |
                                           BindingFlags.GetProperty,
                                           null, oHyperlinkBaseProp,
                                           new object[] { }).ToString();

                // Modify the value of the Hyperlink property object...
                strValue = hyperlink;
                typeHyperlinkBaseProp.InvokeMember("Item",
                                           BindingFlags.Default |
                                           BindingFlags.SetProperty,
                                           null, oDocBuiltInProps,
                                           new object[] { strIndex, strValue });

                // Make a modification to the document because property changes don't 
                // seem to get recognized as changes and are thus ignored even when calling Save().
                doc.Content.InsertBefore("Temporary change...\r\n");
                // Undo the change above...
                doc.Undo(ref oMissing);

                // Save the modified property(s)...
                doc.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Message: {0}\r\nStack: {1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                wordApp.Documents.Close(ref oMissing, ref oMissing, ref oMissing);
                ((Word._Application)wordApp).Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }

In light of this, I would probably remove the dsofile reference and stick to the automation client but that's up to you.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.