none
How to transfer word document to another file using Document.saveas method in windows forms application ?Could you please send me in c#? RRS feed

  • Question

  •  

     hi to all

     

             i am working on Xslt transformations.how to write the code to trasfer word doument to another file using Document.saveas method in windowsformsapplication?i wrote the code. it's  working.But After select the document it displayed one pop up message(File in use).my target is when i select the doucment,directly it will trnasfer to another file.no pop up meassage displayed.pls help me  urgent.

     Thank  you

    object FilePath = "C:\\myfile;

    object FileName = "myfile.Xml; 

    object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML;

    object LockComments = false ;

    object AddToRecentFiles = true;

    object ReadOnlyRecommended = false ;

    object EmbedTrueTypeFonts = false;

    object SaveNativePictureFormat = true ;

    object SaveFormsData = true ;

    object SaveAsAOCELetter = false ;

    object Encoding = Type .Missing;

    object InsertLineBreaks = false ;

    object AllowSubstitutions = false ;

    object LineEnding = Microsoft.Office.Interop.Word.WdLineEndingType .wdCRLF;

    object AddBiDiMarks = false ;

    object missing = string .Empty;

    Microsoft.Office.Interop.Word.Application test = new Microsoft.Office.Interop.Word.Application (); Document false); ref FileName,ref FileFormat, ref LockComments,

    ref missing, ref AddToRecentFiles, ref missing, ref ReadOnlyRecommended, ref EmbedTrueTypeFonts,

    ref SaveNativePictureFormat, ref SaveFormsData, ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions, ref LineEnding, rev AddBiDiMarks);

    System.Runtime.InteropServices.Marshal .ReleaseComObject(wordDoc);

    could you please help me.

    Thank you.

     

     

     

     

    Monday, May 16, 2011 6:27 AM

Answers

  • Hi s1_b bu

    Your scenario is not clear to us, I think. It's not possible to open two separate "instances" of a single document file. Word can process only one such file at a time. The Document.SaveAs method and saving to a new file name won't cause the behavior you describe. The message you're getting indicates that the file in question is already open and you're trying to open it again?

    Could you please provide more information about the entire scenario "SaveAs" is being used in?


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 18, 2011 6:17 AM
    Moderator

All replies

  •  hi to all

     

             i am working on Xslt transformations.how to write the code to trasfer word doument to xml file using Document.saveas method in windowsformsapplication?i wrote the code.But it's not working.pls help me  urgent.

     Thank  you

    object FilePath = "C:\\myfile;

    object FileName = "myfile.Xml; 

    object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML;

    object LockComments = false ;

    object AddToRecentFiles = true;

    object ReadOnlyRecommended = false ;

    object EmbedTrueTypeFonts = false;

    object SaveNativePictureFormat = true ;

    object SaveFormsData = true ;

    object SaveAsAOCELetter = false ;

    object Encoding = Type .Missing;

    object InsertLineBreaks = false ;

    object AllowSubstitutions = false ;

    object LineEnding = Microsoft.Office.Interop.Word.WdLineEndingType .wdCRLF;

    object AddBiDiMarks = false ;

    object missing = string .Empty;

    Microsoft.Office.Interop.Word.Application test = new Microsoft.Office.Interop.Word.Application ();

    Microsoft.Office.Interop.Word.

    Document

    wordDoc = test.Documents.Add(openFileDialog1.FileName.ToString()); wordDoc.Close(

    false);

    wordDoc.SaveAs (

    ref FileName,ref FileFormat, ref LockComments,

    ref missing, ref AddToRecentFiles, ref missing, ref ReadOnlyRecommended, ref EmbedTrueTypeFonts,

    ref SaveNativePictureFormat, ref SaveFormsData, ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions, ref LineEnding, rev AddBiDiMarks); Marshal .ReleaseComObject(wordDoc);

     

     System.Runtime.InteropServices.

    Thank you 

    • Merged by 许阳(无锡)Moderator Wednesday, May 18, 2011 5:21 AM Reason: Duplicate thread with http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/e07033b6-f6b9-4a3c-855a-fbf3c6fd12d9
    Friday, May 13, 2011 6:06 AM
  • Hi s1_b,

     

    Thanks for posting in the MSDN Forum.

     

    Would you please try test snippet? It works fine on my side.

    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Form1
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
     End Sub
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim wdApp As New Word.Application
      Dim wdDoc As Word.Document
      Dim wdFilePath As String
      Dim wdSaveAsPath As String
    
      OpenFileDialog1.Filter = "Word document (*.docx)|*.docx"
      OpenFileDialog1.ShowDialog()
      wdFilePath = OpenFileDialog1.FileName
    
      wdDoc = wdApp.Documents.Open(FileName:=wdFilePath)
      wdApp.Visible = True
    
      SaveFileDialog1.Filter = "Word document Xml formate(*.xml)|*.xml"
      SaveFileDialog1.ShowDialog()
    
      wdSaveAsPath = SaveFileDialog1.FileName
    
      wdDoc.SaveAs2(FileName:=wdSaveAsPath, FileFormat:=Word.WdSaveFormat.wdFormatXML)
    
    
    
      wdDoc.Close()
      wdApp.Quit()
    
     End Sub
    End Class
    
     

     

    I hope it can help you. If you have any questions, please feel free to let me know.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 16, 2011 5:33 AM
    Moderator
  • Hai TOm_Xu MSFT

     

                                  It is ececuted,but after it displayed file in use message box.

                               DO you want to

                              Open a Read Only copy

                               Create a local copy and merge your chanages later

                                      ok    cancel.

       My target is this msg box wiil not come.

    could you please help me .

    Thank you very much.

     

                                           

    Monday, May 16, 2011 6:56 AM
  • Hi, this works for me:

    using Office = Microsoft.Office.Core;
    using Word = Microsoft.Office.Interop.Word;
    
    
    
    private void SaveWordDocAsXML(Word.Document doc, string filename) {
    
      object fileName = filename;
      object fileFormat = Word.WdSaveFormat.wdFormatXML;
      object lockComments = false;
      object addToRecentFiles = true;
      object readOnlyRecommended = false;
      object embedTrueTypeFonts = false;
      object saveNativePictureFormat = true;
      object saveFormsData = true;
      object saveAsAOCELetter = false;
      object encoding = Office.MsoEncoding.msoEncodingUTF8;
      object insertLineBreaks = false;
      object allowSubstitutions = false;
      object lineEnding = Word.WdLineEndingType.wdCRLF;
      object addBiDiMarks = false;
      object missing = Missing.Value;
    
    
      doc.SaveAs(
        ref fileName,
        ref fileFormat,
        ref lockComments,
        ref missing,
        ref addToRecentFiles,
        ref missing, 
        ref readOnlyRecommended, 
        ref embedTrueTypeFonts,
        ref saveNativePictureFormat,
        ref saveFormsData,
        ref saveAsAOCELetter, 
        ref encoding,
        ref insertLineBreaks,
        ref allowSubstitutions, 
        ref lineEnding, 
        ref addBiDiMarks);
    
    }
    


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]
    Monday, May 16, 2011 7:06 AM
    Answerer
  • Hi, please don't crosspost!

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/e07033b6-f6b9-4a3c-855a-fbf3c6fd12d9

    Thank you.

     

     


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]
    Monday, May 16, 2011 7:09 AM
    Answerer
  • hi

      i took two buttons and one Textbox ,I wrote the code.but error came .The type or namespace name 'Core' does not exist in the namespace 'Microsoft.Office' (are you missing an assembly reference?).Could you please help me.

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.ComponentModel;

    using

     

    System.Data;

    using

     

    System.Drawing;

    using

     

    System.Linq;

    using

     

    System.Text;

    using

     

    System.Windows.Forms;

    using

     

    Microsoft.Office.Interop.Word;

    using

     

    word = Microsoft.Office.Interop.Word;

    using

     

    Office = Microsoft.Office.Core;

     

    namespace

     

    saveas

    {

     

    public partial class Form1 :

    Form

    {

     

    public

    Form1()

    {

    InitializeComponent();

    }

     

    private void button1_Click(object sender, EventArgs

    e)

    {

    openFileDialog1.Title =

    "Open File"

    ;

    openFileDialog1.Filter =

    "Word document (*.docx)|*.docx"

    ;

    openFileDialog1.FileName =

    ""

    ;

     

    openFileDialog1.ShowDialog();

     

     

     

    if (openFileDialog1.FileName != ""

    )

    {

    textBox1.Text = openFileDialog1.FileName;

    }

     

    }

     

    private void button2_Click(object sender, EventArgs

    e)

    {

     

    if ((!string

    .IsNullOrWhiteSpace(textBox1.Text)))

    {

     

    object filepath = "C:\\Myfile"

    ;

     

    object FileName = "Myfile.xml"

    ;

     

    object FileFormat =word.WdSaveFormat

    .wdFormatXML;

     

    object LockComments = false

    ;

     

    object AddToRecentFiles = true

    ;

     

    object ReadOnlyRecommended = true

    ;

     

    object EmbedTrueTypeFonts = false

    ;

     

    object SaveNativePictureFormat = true

    ;

     

    object SaveFormsData = true

    ;

     

    object SaveAsAOCELetter = false

    ;

     

    object Encoding = false

    ;

     

    object InsertLineBreaks =false

    ;

     

    object AllowSubstitutions = false

    ;

     

    object LineEnding = Microsoft.Office.Interop.Word.WdLineEndingType

    .wdCRLF;

     

    object AddBiDiMarks = false

    ;

     

    object missing = string

    .Empty;

    Microsoft.Office.Interop.Word.

    Application test = new Microsoft.Office.Interop.Word.Application

    ();

    Microsoft.Office.Interop.Word.

    Document wordDoc = test.Documents.Open(openFileDialog1.FileName, true

    );

     

    wordDoc.SaveAs2(

    ref FileName, ref FileFormat, ref

    LockComments,

     

    ref missing, ref AddToRecentFiles, ref

    missing,

     

    ref ReadOnlyRecommended, ref

    EmbedTrueTypeFonts,

     

    ref SaveNativePictureFormat, ref

    SaveFormsData,

     

    ref SaveAsAOCELetter, ref Encoding, ref

    InsertLineBreaks,

     

    ref AllowSubstitutions, ref LineEnding, ref

    AddBiDiMarks);

    System.Runtime.InteropServices.

    Marshal

    .ReleaseComObject(wordDoc);

     

     

     

     

     

     

    }

     

    }

     

     

    }

    }

     

     

     

     

     

    Monday, May 16, 2011 7:54 AM
  • You need to add a reference to the Office COM Library, as well as to the Work Object Library in your Project.

    Greets - Helmut


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]
    Monday, May 16, 2011 8:35 AM
    Answerer
  • Hi Helmut Obertanner

    How to add the  reference Work Object Library ?

    error is The name Missing does not exist in the current context?

    Please help me urgent,

    Thank you

    Monday, May 16, 2011 9:44 AM
  • Sorry, was a typo. I mean the Word COM Object Library.

    This is something you should already know when developing with the Word Object Model.

    Go to Project References.

    Add Reference.

    Select the COM Tab

    Microsoft Office XX.X Object Library

    Then, later in the .cs files you can add a using statement:

    using Word = Microsoft.Office.Interop.Word;
    using Outlook = Microsoft.Office.Interop.Outlook;
    

    then, instead of writing:

    Microsoft.Office.Interop.Word.Application wdApp =

    you can write

    Word.Application wdApp =


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]
    Monday, May 16, 2011 10:24 AM
    Answerer
  • Hi Helmut Obertanner

     it's  working.But After select the document it displayed Two pop up messages

    1.(File in use)

          Do you want to

        Open a read only copy

       Create a local copy and merge changes later

       Receive notification when the original copy is available.

    After selecting read only copy,it will display another popup msg

    2.convert file

     my target is when i select the doucment,directly it will trnasfer to another file.no pop up meassage displayed.pls help me  urgent.

     


    Monday, May 16, 2011 10:43 AM
  • Hi

    where did you write the code?i mean

    under form load or under button

     

    Thank you

    Monday, May 16, 2011 1:44 PM
  • Hi,

    I wrote the code in a Button eventHandler. The MessageBox you get is, because you open a document that is already opened somewhere.
    I'm not the Word-specialist, but I think for conversion you can open a document as read-only.

    Then you won't get a MessageBox.

    Please read the documentation how to open Word Documents:

    (ReadOnly) property

    http://msdn.microsoft.com/en-us/library/bb216319(v=office.12).aspx


    Greets - Helmut

     


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]
    Tuesday, May 17, 2011 5:36 AM
    Answerer
  • Hi 

     

     How to add the reference office com library ?

    Tuesday, May 17, 2011 8:44 AM
  • please read this post,

    I already answered that!

    Greets - Helmut


    Helmut Obertanner [http://www.obertanner.de] [http://www.outlooksharp.de]
    Wednesday, May 18, 2011 5:46 AM
    Answerer
  • Hi s1_b bu

    Your scenario is not clear to us, I think. It's not possible to open two separate "instances" of a single document file. Word can process only one such file at a time. The Document.SaveAs method and saving to a new file name won't cause the behavior you describe. The message you're getting indicates that the file in question is already open and you're trying to open it again?

    Could you please provide more information about the entire scenario "SaveAs" is being used in?


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 18, 2011 6:17 AM
    Moderator