How to run a macro from C#? - c#

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.

Related

How to hide copy process of Visio to RichTexbox

all. I've created a project that displays contents from a Visio document by copying the content and pasting the content as an image on a rich textbox in a C# winform. The problem is that when the copying process begins, Visio opens up for a few seconds and then my program copies the content. While I do want that to happen, I do not want Visio visible during the process. Is there a way to accomplice what is done while hiding Visio?
Here's the code:
public partial class FrmVisio : Form
{
public FrmVisio()
{
InitializeComponent();
}
private void cboContent_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void FrmVisio_Load(object sender, EventArgs e)
{
cboContent.Items.Add("PFD-001_Control of Documented Information Process _(R4)");
cboContent.Items.Add("PFD-002_Management_Review_Process_(R1)");
cboContent.Items.Add("PFD-003 Control of Non-conformance Process Flow(R1)");
cboContent.Items.Add("PFD-004_Internal Audit Process _(R1)");
cboContent.Items.Add("PFD-008_Risk & Opportunity Process _(R1)");
cboContent.Items.Add("PFD-010_Change_Control_Process_Flow_Diagram_(R1)");
cboContent.Items.Add("PFD-011_Determine_Requirements_for_Products_Services_(R2)");
}
private void btnOpenWord_Click(object sender, EventArgs e)
{
if (cboContent.SelectedItem == null)
MessageBox.Show("Please select a document", "No Document Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
var item = cboContent.SelectedItem.ToString();
MessageBox.Show("IMPORTANT! PLEASE READ: MS Visio allows you to create shapes and diagrams. However, those things, if edited, will not be saved on the " +
"QMS unless you export the allocated document as a PDF in the same folder and overwrite the default PDF given. DO NOT CHANGE THE FILE NAMES OR THEIR LOCATIONS! Otherwise, the link to the documents and the QMS will be broken", "PLEASE READ!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Process.Start(#"C:\Program Files (x86)\IMS Global\Quality Management System Application\QMS\QMS\QMS\" + item + #".vsd");
}
}
/// <summary>
/// Show the preview of the Visio Document
/// </summary>
public void DisplayVsio()
{
//TODO: Find Better Alternative to Show Visio Documentation.
//TODO: If not possible, edit widgets in windows form.
if(cboContent.SelectedItem == null)
{
MessageBox.Show("Please select a document", "No Document Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
var item = cboContent.SelectedItem.ToString();
Visio.Application appObject = new Visio.Application();
Microsoft.Office.Interop.Visio.Document doc = appObject.Documents.Open(#"C:\Program Files (x86)\IMS Global\Quality Management System Application\QMS\QMS\QMS\" + item + #".vsd");
doc.Application.ActiveWindow.Selection.SelectAll();
doc.Application.ActiveWindow.Selection.Copy();
richTextBox1.Paste();
appObject.Quit();
}
}
private void btnShow_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
DisplayVsio();
}
}
Change:
Visio.Application appObject = new Visio.Application();
to:
Visio.Application appObject = new Visio.InvisibleApp();

How to overwrite an existing html file from textbox in C#?

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);
}
}

Novacode DocX with SaveFileDialogue C#

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();
}
}

Adding a Save As Function to a form using a listbox store of data

I am creating a Times table form in Visual Studio Express 2014 in C#.
I have created the whole form, it works to a full extent, even saving to a text file, however i wish to add a Savedialog in order for the user to choose where to save the file just like saving in word, etc.
Ive tried using File.WriteAllText(name, "test"); or variations of this however this does not work with a listbox
In my code the listbox is called results,
here is my save button code what i have tried so far:
private void save_Click(object sender, EventArgs e)
{
const string Path = "C:\\Users\\Loan\\save.txt";
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(Path);
foreach (var item in results.Items)
{
SaveFile.WriteLine(item.ToString());
}
SaveFile.Close();
MessageBox.Show("Programs saved!");
}
it works perfectly fine, however as i have aforementioned that
i wish to create a save as function for the user to browse where to
save.
The form =
http://gyazo.com/227d2aada349586cef60f2962456a71c
The full code =
http://pastebin.com/7DwLpkhP
Use the `Save file dialog' class
using (SaveFileDialog dialog = new SaveFileDialog())
{
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
dialog.FilterIndex = 2 ;
dialog.RestoreDirectory = true ;
if (dialog.ShowDialog() == DialogResult.OK)
{
// Can use dialog.FileName
string Path = dialog.FileName;
....

Prevent termination of Word Application when user closes last document

I have a simple Win Forms application that consists of a Button control, an OpenFileDialog component and a Microsoft.Office.Interop.Word.Application object:
using System;
using System.Windows.Forms;
namespace WordInterop
{
public partial class Form1 : Form
{
// From Form1.Designer.cs:
//Button openButton;
//FileOpenDialog openFileDialog;
private Microsoft.Office.Interop.Word.Application _wordApp;
public Form1()
{
InitializeComponent();
_wordApp = new Microsoft.Office.Interop.Word.Application();
}
protected override void OnClosed(EventArgs e)
{
_wordApp.Quit();
base.OnClosed(e);
}
private void _openButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK) {
// Throws COMException if Word app is not longer running
_wordApp.Documents.Open(openFileDialog.FileName);
_wordApp.Visible = true;
}
}
}
}
When the user clicks the Open button, they can selected a Word document via the OpenFileDialog. The filename of the selected document is used to open the Word Document using the Microsoft.Office.Interop.Word.Application object.
It appears that the Word application is terminated when the user closes the last document: If the user clicks the Open button, selects a document, closes the document, clicks the Open button and selects a document a second time, a COMException occurs with the message The RPC server is unavailable. If there is at least one document open, the user can open documents without a COMException occurring.
Is there a way I can prevent Word application from terminating when the user closes the last document?
I tried creating a hidden blank document by changing the Form1 constructor to the following:
public Form1()
{
InitializeComponent();
_wordApp = new Microsoft.Office.Interop.Word.Application();
_wordApp.Documents.Add(Visible: false);
}
However the blank document becomes visible when the user opens a document meaning it is still possible for user terminate the Word application.
Create a new instance of Microsoft.Office.Interop.Word.Application() on the button click.
private void _openButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK) {
_wordApp = new Microsoft.Office.Interop.Word.Application();
_wordApp.Documents.Open(openFileDialog.FileName);
_wordApp.Visible = true;
}
}

Categories

Resources