Good Day!
I want to know if it's possible to show SaveFileDialogue when saving .docx file using Novacode DocX?
Sample:
string fileName = #"D:\Users\John\Documents\DocXExample.docx";
var doc = DocX.Create(fileName);
doc.InsertParagraph("This is my first paragraph");
doc.Save();
Where shoud I put the SaveFileDialogue code?
Many Thanks!
Put saveFileDialog1.ShowDialog(); inside some button event handler which lets the user save the document. Double-click on the SaveFileDialog icon in your Visual Studio designer window as well to add the FileOk event handler and within event handler, put your code like this:
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
var doc = DocX.Create(saveFileDialog1.FileName);
doc.InsertParagraph("This is my first paragraph");
doc.Save();
}
Hope it helps!
To do this:
private void btn_approve_Click(object sender, EventArgs e)
{
saveFileDialog1.Title = "Save As";
saveFileDialog1.Filter = "DocX|*.docx";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
var doc = DocX.Create(saveFileDialog1.FileName);
doc.InsertParagraph("This is my first paragraph");
doc.Save();
}
}
Related
I have a macro (.docm) that opens an rtf file and saves it in .doc format. This macro needs to be run from a C# application. How to do it?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Создание экземпляра Word
Word.Application app = new Word.Application();
app.Visible = true;
Word.Documents doc = app.Documents;
// Открытие файлов
MessageBox.Show("add file (.docm)");
OpenFileDialog macroFile = new OpenFileDialog();
if (macroFile.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("Error");
return;
}
}
}
After you have got a file name you can use the Documents.Open method which opens the specified document and adds it to the Documents collection. The Document.SaveAs2 method allows saving the specified document with a new name or format. Some of the arguments for this method correspond to the options in the Save As dialog box (File tab). The format in which the document is saved. Can be any WdSaveFormat constant. It seems you are interested in the wdFormatDocument value.
I'm trying to create a normal HTML editor where it's functions are similar to Windows Notepad. Let's say we've written a file and wanted to save it. So the SaveFileDialog comes up and asking us where we want to save it. Then we modified it, and in Notepad you usually just use Ctrl+S to save it again. And this time, no SaveFileDialog will show up. In other words, the existing file has been overwritten. This is what I wanted to achieve. Here's the code I have so far:
private void toolStripButton2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = default(SaveFileDialog);
string location = null;
string sourcecode = textBox1.Text;
sfd = new SaveFileDialog();
location = sfd.FileName;
if (string.IsNullOrEmpty(sfd.FileName))
{
saveAsToolStripMenuItem_Click(sender, e);
}
else
{
File.Copy(sfd.FileName, Path.Combine(location, Path.GetFileName(sfd.FileName)), true);
}
}
I created simple example and I can't find solution anywhere. I want to save UI textbox data, and open it in another(same) windows form file.
Here is code example:
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
StreamWriter write = new StreamWriter(File.Create(sfd.FileName));
write.Write(txtFirstInput.Text);
write.Write(txtSecondInput.Text);
write.Close();
write.Dispose();
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
{
StreamReader read = new StreamReader(File.OpenRead(ofd.FileName));
txtFirstInput.Text = read.ReadLine();
txtSecondInput.Text = read.ReadLine();
read.Close();
read.Dispose();
}
}
The problem I get here is that all inputed data get in first textbox. How to separate it? What is the easiest and most efficient way?
Should I create and use byte buffer (and HxD) for streaming through user input?
Can someone please give me some directions or examples.
Thank you for your time
You use ReadLine to read data back, so you need to use WriteLine when writing data to disk
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
using(StreamWriter write = new StreamWriter(File.Create(sfd.FileName)))
{
write.WriteLine(txtFirstInput.Text);
write.WriteLine(txtSecondInput.Text);
}
}
}
Using Write, the data is written to disk without a line carriage return and so it is just on one single line. When you call ReadLine all the data is read and put on the first textbox.
Also notice that it is always recommended to enclose a IDisposable object like the StreamWriter inside a using statement. This approach will ensure that your Stream is correctly closed and disposed also in case of exceptions. (The same should be applied to the StreamReader below)
I have one TextBox, It has some data.
There is a Button. So When I click the button, "save as " dialog should popup to save the text TextBox Data into a file.
I have tried various ways, but getting errors n error. Here I am giving you brief idea how I am writing code, If I am wrong please make me correct.
Or is there any other way to save TextBox Data into a file od my desired path.
protected void ButtonIDSaveAs_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
fs.Close();
}
}
Thanks
Vivek
SaveDialog.OpenFile creates a new file (overwriting an existing file with the same choosen name) and returns a Stream object that could be used as the constructor parameter for a StreamWriter.
So you could simply write
if (saveFileDialog1.FileName != "")
{
using(StreamWriter sw = new StreamWriter(saveFileDialog1.OpenFile()))
{
sw.Write(TextBox1.Text);
}
}
I'm new for c#, and i need to write a programme with a button (clicking it to show a .txt file)
could someone give me some idea or may be an example code
thanks
Answer for WinForms:
Place textBox named tbBrowser and Button named bBrowse on form. Double-click button to create button Click handler. Place the following code in the handler:
String filename = #"C:\Temp\1.txt";
using (StreamReader rdr = new StreamReader(filename))
{
String content = rdr.ReadToEnd();
tbBrowser.Text = content;
}
See StreamReader documentation for reference.
Answer for ASP.NET would be completely different, but it's unlikely you are asking about that (at least word program in question makes me think so)
You haven't said if you wanted to use winforms, wpf or something else. Anyway, the code below will work for winforms - just add a textbox to your form:
private void button1_Click(object sender, EventArgs e)
{
// Create reader & open file
using (TextReader tr = new StreamReader(#"C:\myfile.txt"))
{
textBox1.Text += tr.ReadToEnd();
}
}
If you are creating windows form appln add a button to you form
double click the button
and in the method write the following code
private void button1_Click(object sender, EventArgs e)
{
string filePath = "give your path";
// or you can give your path in app.config and read from app.config if you want
// to change the path .
if (File.Exists(filePath))
{
StreamReader reader = new StreamReader(filePath);
do
{
string textLine = reader.ReadLine() + "\r\n";
}
while (reader.Peek() != -1);
reader.Close();
}
}