How to hide copy process of Visio to RichTexbox - c#

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

Related

How to run a macro from 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.

C# VSTO Excel Task Pane not showing when embedding Excel in Word

Currently I'm working on a Excel Add-In where the user can select an XML file. I read the XML and add nodes to a TreeView. This TreeView is placed within the Task Pane. When starting Excel on its own everything works, but when I add a Chart or an Embedded Excel in Word and open it in Excel, the Task Pane is not showing up.
This code eventually triggers the event for opening the TaskPane
private void ShowTaskPane_TglBtn_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
}
This code opens the Task Pane for real:
public partial class ThisAddIn
{
public UserControl1 taskPaneControl1;
private Microsoft.Office.Tools.CustomTaskPane taskPaneValue;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
taskPaneControl1 = new UserControl1();
taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "Task Pane");
taskPaneValue.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
}
private void taskPaneValue_VisibleChanged(object sender, System.EventArgs e)
{
Globals.Ribbons.Ribbon1.ShowTaskPane_TglBtn.Checked =
taskPaneValue.Visible;
}
public Microsoft.Office.Tools.CustomTaskPane TaskPane
{
get
{
return taskPaneValue;
}
}
}

C# assigning textbox contents to specific elements of an array causes some elements to be null

Hey guys so I have a userControl file which prompts to user to enter their information in which creates a text file with the information to a directory. I created multiple textboxes which have the same code. The array "info" was declared like this
private string[] oof = new string[19];
All of the textboxes are coded like this below.
private void TextBox0_TextChanged(object sender, EventArgs e) // First Name
{
info[0] = textBox0.Text;
}
private void TextBox1_TextChanged(object sender, EventArgs e) //Last Name
{
info[1] = textBox1.Text;
}
.....
The method I used to create the text file is
private void ButtonCreate_Click(object sender, EventArgs e) //Create new Profile
{
string path = #"C:\Users\J\Desktop\C#\ + profileName + ".txt";
using (StreamWriter sw = File.CreateText(path))
{
for (int i = 0; i < 13; i++)
{
sw.WriteLine(list[i]);
sw.Flush();
}
sw.Close();
sw.Dispose();
}
}
This code correctly creates the text file with the desired name to the right directory but the content of the text file contains only part of the information that the user provided when I test run it. When I debug the program, I noticed that some of the elements that were being set to the content of different textboxes were 'null' even though it was filled out by the user. It creates the text file but the file only contains part of the user's information not all of it like
first name
(last name is suppose to be here but its blank)
address
phone number
(state is suppose to be here but its blank)
city
....

Windows Form wont close properly

I am developing an add-in for outlook 2010.
Basically, I have a button on my ribbon that takes the selected email, and saves it to a text file. If the email contains a certain subject then the save is done automatically to a hard coded file path. If not, a windows form is opened asking the user to enter a filepath.
When the user has selected a path, and clicked 'OK' the save takes place and then the form closes... but then it re-opens... it seems to be creating a new instance of it or something... if I click 'Cancel' or 'X' it closes, but I can't see why it's not closing properly the first time.
Below is my code
//This is myRibbon.cs
private void btn_SaveFile_Click(object sender, RibbonControlEventArgs e)
{
//other code
if (subject = "xyz")
{
//other code
textFile.Save();
}
else
{
MyPopup popup = new MyPopup();
popup.ShowDialog();
}
}
//This is MyPopup.cs
private void btnOK_Click(object sender, EventArgs e)
{
var filePath = txtFilePath.Text;
if (!string.IsNullOrWhiteSpace(filePath))
{
SaveEmailToText(filePath);
this.Close();
}
else
{ //show message box with error }
this.Close();
}
private static void SaveEmailToText(string filePath)
{
//other code
textFile.Save();
}
I have simplified this quite a bit so its easier to read.
Any help would be greatly appreciated.
Consider to use OpenFileDialog instead of your popup form
Use your popup (or file dialog) only for getting file name
Keep email saving code in one place (otherwise you will have duplicated code)
Verify DialogResult of dialog form, before processing further
Forms are disposable - using statement will dispose them automatically
Do not close dialog form - set it's DialogResult property instead
Here is refactored code:
private void btn_SaveFile_Click(object sender, RibbonControlEventArgs e)
{
string filePath = defaultPath;
if (subject != "xyz")
{
using(MyPopup popup = new MyPopup())
{
// user can close popup - handle this case
if (popup.ShowDialog() != DialogResult.OK)
return;
filePath = popup.FilePath;
}
}
SaveEmailToText(filePath);
}
private void SaveEmailToText(string filePath)
{
//other code
textFile.Save();
}
And your popup, which should be replaced with OpenFileDialog:
private void btnOK_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(FilePath))
{
//show message box with error
DialogResult = DialogResult.Cancel;
return;
}
// you can assign default dialog result to btnOK in designer
DialogResult = DialogResult.OK;
}
public string FilePath
{
get { return txtFilePath.Text; }
}

Filepath and textboxes

Ok I am going to try this again. I got more information now. I understand I can not use open and save dialogs and there is no database. So I am still kinda lost cause I was shown how to do it with open and save dialogs before. I am going to put what I am suppose to do and then so far the code I have. The code I have I have to build off and add too. I will also show what I am suppose to add to it. I am just trying to find the best way to understand this cause right now I am not. I am still new and I know the last couple days people have been trying to help me understand and then I was told it wasnt with the open and save dialog. Here is what I am suppose to do.
•Add a textbox named txtFilePath <--- already have that
•Add a button next to the above textbox that says “Load” (name it appropriately)<-already have that
•Add a button that says “Save” (name it appropriately) <-- already have this
•When thebutton “Load” is clicked, read the file specified in the textbox
(txtFilePath: Absolute path not relative) and add the objects found
within to the listbox<--- Not understanding
•When the user clicks the “Save” button, write the selected record to
the file specified in txtFilePath (absolute path not relative) without
truncating the values currently inside<-- not understanding
Here is the one part of code I have:`
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
EditDialog newEmployeeDialog = new EditDialog();
if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
{
employeeList.Items.Add(newEmployeeDialog.StaffMember);
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (employeeList.SelectedIndex == -1)
return;
if (MessageBox.Show("Really delete this employee",
"Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
== DialogResult.Yes)
{
employeeList.Items.Remove(
employeeList.SelectedItem);
}
}
private void editButton_Click(object sender, EventArgs e)
{
if (employeeList.SelectedIndex == -1)
return;
int employeeNum = employeeList.SelectedIndex;
EditDialog newEmployeeDialog = new EditDialog();
newEmployeeDialog.StaffMember =
(Employee)employeeList.SelectedItem;
if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
{
employeeList.Items.RemoveAt(employeeNum);
employeeList.Items.Insert(employeeNum, newEmployeeDialog.StaffMember);
employeeList.SelectedIndex = employeeNum;
}
}
private void employeeList_SelectedIndexChanged(object sender, EventArgs e)
{
if (employeeList.SelectedIndex != -1)
{
Employee currentEmployee = (Employee)employeeList.SelectedItem;
firstName.Text = currentEmployee.FirstName;
lastName.Text = currentEmployee.LastName;
jobTitle.Text = currentEmployee.JobTitle;
}
else
{
firstName.Text = lastName.Text = jobTitle.Text = "";
}
}
`
Now I know you can not see the button click but I do have them mark. I know when you use open and save how it works. How I can go about this? I would use stream writer right.I understand that the user will type the path into the textbox and when the user hits load, it will load the file that they are specified. Now I am just trying to understand a code to be able to word this right.
would it be something like this:
String filePath = this.txtFilePath.Text;
since I need to name the textbox txtFilePath. I know some of you might say this is simple but when you are first learning it don't seem that simple. I have been trying something to help me understand since I do my college from home. Thank you for reading hoping to hear from you guys.
Update: Would it be something like this
Reading a file
private void Load_Click(object sender, EventArgs e)
{
StreamReader myReader = new StreamReader(C:\\")
txtFilePath.Text = my reader.read to end();
myReader.Close();
}
then there is writing a file
{
StreamWriter myWriter = new StreamWriter("C:\\test.txt", true);
myWriter.Write("Some text");
myWriter.WriteLine("Write a line");
myWriter.WriteLine("Line 2");
myWriter.Close();
}
If this is correct then I have to get it where if the file is not there for the notepad to pop up so they can add it then they can save it without deleting anything out the file or files.
Assuming the file contains a list of employee names, you should be able to load them into your listbox using something like this:
var filepath = txtFilePath.Text;
if (File.Exists(filepath))
{
var lines = File.ReadAllLines(filepath);
foreach (var line in lines)
employeeList.Items.Add(line);
}
Then assuming you want to add a new employee name to the file that the user just entered into the listbox:
var filepath = txtFilePath.Text;
if (File.Exists(filepath))
using (var sw = File.AppendText(filepath))
sw.WriteLine((string)employeeList.Text);
else
using (var sw = File.CreateText(filepath))
sw.WriteLine((string)employeeList.Text);
This hasn't been tested, but it should work nearly as-is...

Categories

Resources