Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8050967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:12:13+00:00 2026-06-05T07:12:13+00:00

I have code that writes text to a Word document (non-XML) and saves it

  • 0

I have code that writes text to a Word document (non-XML) and saves it to a file. Now, I’m trying to modify it so that the document is created and the user is then prompted to save the document.

This is the code for creating the Word document:

object miss = System.Reflection.Missing.Value;
object Visible = true;
object start1 = 0;
object end1 = 0;
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Document aDocument = WordApp.Documents.Add(ref miss, ref miss, ref miss, ref miss);
Range rng = aDocument.Range(ref start1, ref miss);

try
{
    rng.Font.Name = "Georgia";
    rng.InsertAfter("Report Header!");
    object filename = @"C:\Users\test.doc";

    aDocument.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
    object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
    object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
    object routeDocument = true;
    ((_Document)aDocument).Close(saveChanges, originalFormat, routeDocument); //to close the document process (winword.exe)
    //WordApp.Visible = true;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}        

This works as expected; it creates a Word document and opens it for the user. I’ve been reading this link from Microsoft about saving files using the SaveFileDialog, but I’m not entirely sure how to apply it to my situation. Since the SaveFileDialog provides a stream object to write to, should I read from the Word file and write it to the stream? If that’s the case, I’m not sure how to code it, but that seems like a roundabout way. Is there a way to pass the created Word document directly to the SaveFileDialog?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:12:13+00:00Added an answer on June 5, 2026 at 7:12 am

    The SaveFileDialog could be used simply to give, to your user, an interface to select a folder and a name. Then it’s up to you to save the file.

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "Word document|*.doc";
    saveFileDialog1.Title = "Save the Word Document";
    if(DialogResult.OK == saveFileDialog1.ShowDialog())
    {
        string docName = saveFileDialog1.FileName;
        if(docName.Length > 0)
        {
            object oDocName = (object)docName;
            aDocument.SaveAs(ref oDocName, ref miss, ref miss, ref miss, ref miss, ref miss,  
                         ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, 
                         ref miss, ref miss, ref miss, ref miss); 
            WordApp.Visible = true; 
        }
    }
    

    The method you mention in your question is documented by MSDN in this article, but in your case is more practical to let the MSWORD application do the saving operation on its file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have third-party C code that writes data to a file. I want to
I have an word file that contain my specified pattern text {pattern} and I
I have a code that search if a line begin with a specified word
I have a code to downolad a particular file that is stored at a
I have a multi threaded app that writes log to file. occasionaly, saving fails
I have a php file that writes to a txt file: <?php $answers =
the code that i am trying to write is supposed to read text from
Sometimes I have to write code that alternates between doing things and checking for
So far I have managed to write some code that should print the source
I'd like to write a function that would have some optional code to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.