Le réseau pour les développeurs > Forums - Accueil > Visual C# Language > Saving open work files with VS 2008 and C#
Poser une questionPoser une question
 

TraitéeSaving open work files with VS 2008 and C#

Réponses

Toutes les réponses

  • mercredi 4 novembre 2009 20:38OmegaManMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    What you wrote is unclear, do you want to identify open Word processes/documents or do you want to open a new word document and save it?

    William Wegerson (www.OmegaCoder.Com)
  • jeudi 5 novembre 2009 10:53SWClarke Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Thanks for your reply William.

    The first one that you mentioned ie I would like to be able to identify any documents that I have open on my PC (eg Document1.doc, Document2.doc, etc), and write C# code in VS2008 to save these to a defined folder.

    At the moment, I have a program working that opens a new Word document, writes some text it in and closes the Word application, so I presume that its now a case of writing some additional code to identify the open Word processes/Docs and save these.  I have included this code as below, in its class called WordDoc.cs


    using

     

    System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    using Microsoft.Office;
    using System.Reflection;

    namespace
    WordClass
    {

     

    class WordDoc
    {

     

    public static void WordClass()

    {

    //Missing Null Values
    Object oMissing = System.Reflection.Missing.Value;

     

    //True and false objects
    Object oTrue = true;
    Object oFalse = false;

     

    //Create Word and document
    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.
    Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

     

    //Make the Word app display
    oWord.Visible = true;

     

     

    //Create a new document
    oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

     

    //Insert a paragraph at the beginning of the document
    Microsoft.Office.Interop.Word.Paragraph oPara1;
    oPara1 = oWordDoc.Content.Paragraphs.Add(
    ref oMissing);
    oPara1.Range.Text =
    "Testing writing some text into the document";

     

     

    //Save file as
    Object oSaveAsFile = (Object)"C:\\SampleDoc.doc";
    oWordDoc.SaveAs(
    ref oSaveAsFile, 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,
    ref oMissing, ref oMissing);

     

    //File close
    oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

     

    //App close
    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
    }}}




    Thanks,

    Stephen
  • jeudi 5 novembre 2009 12:37OmegaManMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    One can identify running applications via methods in the System.diagnostics namespace. Check out (Get a List of Running Processes in C# ). But you won't be able to remotely, for lack of a better term, be able to save other office documents to specified folders. Processes are not meant to piggy back and manipulate other processes, that would lead to to man security issues. (AFAIK)

    HTH GL

    William Wegerson (www.OmegaCoder.Com)
  • jeudi 5 novembre 2009 16:33SWClarke Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Thanks for the reply William.  I have now included a version of the running processes section (from the link that you sent) in my code.  Is there any work-around for identifying open Word and Office applications and save them to a specified folder or even a default folder?
  • mercredi 11 novembre 2009 02:33Harry ZhuMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Hi,

    As I learnt from your post, you are trying to get the running instance of office.

    You might want to take a look at step-by-step article about how to create a Microsoft Visual C# 2005 or Microsoft Visual C# .NET client that gets an Automation Reference to a running instance of an Office program.
    http://support.microsoft.com/default.aspx/kb/316126

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.