|
Hi I'm writing an app that will have some Word automation, but I can not be certain if the user has already got an instance of word open, if they do i wish to use there unstance do my automation or else start a new instance. My problem is if they have it open my code makes a new instance and will close down the both instance when i finish can any one help.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Word=Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using core= Microsoft.Office.Core;
using System.Diagnostics;
namespace WordProcess
{
public partial class MainForm : Form
{
Word.Application wordApp;
Word._Document wordDocument;
object document;
object wordFalse = false;
object wordTrue = true;
object dynamic = 2;
object wordMissing = System.Reflection.Missing.Value;
public MainForm()
{
InitializeComponent();
}
void Button1Click(object sender, EventArgs e)
{
System.Diagnostics.Process[] wordPro = System.Diagnostics.Process.GetProcessesByName("winword");
if (wordPro.Length==0){
Process p= Process.Start("WINWORD.EXE");
p.WaitForInputIdle();
}
try{
object filePath =@"C:\test.doc";
object filePath1 =@"C:\test1.doc";
wordDocument = new Word.DocumentClass();
wordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
wordApp.Documents.Open(ref filePath, ref wordMissing, ref wordFalse, ref wordMissing,
ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing,
ref wordMissing, ref wordMissing, ref wordMissing, ref wordTrue,
ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing);
wordDocument.SaveAs(ref filePath1, ref wordMissing, ref wordFalse, ref wordMissing,
ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing,
ref wordMissing, ref wordMissing, ref wordMissing, ref wordTrue,
ref wordMissing, ref wordMissing, ref wordMissing, ref wordMissing);
wordDocument.Close(ref wordFalse, ref wordMissing, ref wordMissing);
wordApp.Quit(ref wordMissing, ref wordMissing, ref wordMissing);
}catch(MarshalDirectiveException mw){
MessageBox.Show(mw.ToString());
}catch(Exception ex){
MessageBox.Show(ex.ToString());
}
}
}
}
|
|
|
|
|
Instead of quitting if you would just kill the process that your application started, then it should work fine.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
the problem is it checks for a process and will start one fine but if there is one running the application will start a second instance of Word up that is ties to the first
|
|
|
|
|
In that case easiest way I can think of is start new winword process through your application and then kill it without harming any other word document.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
thats something I'm looking at present myself main problem is making sure i only close down the new process any ides would be great thanks.
|
|
|
|
|
AFAIK that if you use the System.Diagnostics.Process class and persist the object you can use it to close the specific instance of word that you opened.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
dan neely wrote: AFAIK
I am sorry but I dont know what this means.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
As Far As I Know.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
|
i made 3 forms . 1.. frmMain(it is the mdiContainer) (form)
2........frmEditable (form)
3....... frmTools (form)
now
i just want that when i click on the rectangle button from frmTools form a rectangle must should be drawn on frmEditable form.
namespace I_M_Editor
{
public partial class frmMain : Form
{
Bitmap m = new Bitmap(2, 2);
double Zoom = 1.0f;
// PictureBox pictureBox1=null;
IDrawableImages img = null;
// IDrawableShapes shp= null;
public frmMain()
{
InitializeComponent();
img = new Images();
}
private void frmMain_Load(object sender, EventArgs e)
{
frmToolBox frm = new frmToolBox();
frm.MdiParent = this;
frm.Show();
frmEditable frm2 = new frmEditable();
frm2.MdiParent = this;
frm2.Show();
}
protected override void OnPaint(PaintEventArgs e)
{
}
private void tsxOptionOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (DialogResult.OK == openFileDialog.ShowDialog())
{
m = (Bitmap)Bitmap.FromFile(openFileDialog.FileName);
}
}
public void Rect()
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Red);
g.DrawRectangle(p, 260, 260, 100, 100);
}
}
}
namespace I_M_Editor
{
public partial class frmEditable : Form
{
public frmEditable()
{
InitializeComponent();
}
private void frmEditable_Load(object sender, EventArgs e)
{
}
}
}
namespace I_M_Editor
{
public partial class frmToolBox : Form
{
public frmToolBox()
{
InitializeComponent();
}
private void lblRect_Click(object sender, EventArgs e)
{
((frmMain)this.MdiParent).Rect();
}
}
i want to draw it in frmEditable(form).
frmTools(form) contain tools from which i click lblRect (its a label)
,a rectangle should be drawn on frmEditable('s form).
can anybody correct it... if can then plz do so........
hghghgh
|
|
|
|
|
Your problem is not with the drawing part, you want to pass data from one form to another form I think.
i.e: You want to know that a rectangle was selected in the first form and draw a rectangle in the second form .
So the easiest way to exchange data between forms is to pass the reference of one form to the other form that is:
Form1 form1Ref;
public Form2(Form1 fRef)
{
InitializeComponent();
form1Ref = fRef;
}
After this you can reach the public member variables/function of form1 from form2. So if you keep the info of selected settings in a variable in form1 you can get them from form2.
PS: I recognized that unnecessary white spaces and unindented codes really disturb me. Why don't you tidy your code
Good luck
zafer
|
|
|
|
|
Inter-child communication should be done through the parent form. This can be done with events and/or delegates.
Set up some events in frmTools, and after creating an instance of it in frmMain, subscribe to it's events.
Now that frmMain is listening to frmTools - it can pass the instructions onto your frmEditable instance which you can find in frmMain's MdiChildren collection.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
I have a textbox that I validate the value of. On the same form, are two buttons, OK, and Cancel which return DialogResults. In the validating event ii check to see if the textbox is empty, to stop empty values being inserted into the DB.
However, if I open the form and then want to cancel it (eg: close the form) because the validating event has been fired, I cannot click the Cancel button.
Anyone know a work around for this?
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Hi,
Validating is a bit problematic when using default events. As you noticed, you cannot move the focus from the control as long as it's not validated.
However, you can resolve this in many ways. One that comes first in mind is that you create a single validation method where you actually do the validations. This method can be called whenever you want that validation occurs and it can use error providers or such to indicate problem controls. If you return a boolean from this method indicating if validation was succesfull, you can use that boolean for example in OK button so that it doesn't perform it's actions if validation has failed. However, cancel button doesn't use the return value since it's going to cancel the whole operation. Make validation events to call this method, but nevermind the return value and never set cancel to false in validation event.
Hope this helps (and was understandable),
Mika
|
|
|
|
|
Mika,
I'd rather not do it that way, do you know of another work around?, or am I stuck with handling it my self?
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Sorry to say, but nothing comes in mind since if you set Cancel to true in validating-event, you cannot move focus away from the control.
Is there some particular reason why you don't want to use centralized validation?
Mika
|
|
|
|
|
|
Well, you could have Form1 register for Form2's OnClosed event. That way Form1 would be told when Form2 was closed.
My current favourite word is: Nipple!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
If you do not need Form1 to remain responsive then you should make Form2 modal ( start it with Form2.ShowDialog() ). Then when Form2 closes the code in Form1 picks up on the next line so you can re-enable the button then. Something like:
Form1.btn.Enabled = false;
Form2.ShowDialog();
Form1.btn.Enabled = true;
If you need Form1 to remain responsive, other than for the button that opens Form2, then you should make Form2 non-modal ( Form2.Show() ), have Form1 listen for Form2's Close event and re-enable the button when Form1 responds to Form2’s Close event.
modified on Tuesday, September 9, 2008 5:23 PM
|
|
|
|
|
The answers above are all quite helpful and here is the source code to help you:
In Form1 :
private void myButton_Click(object sender, EventArgs e)
{
changeMyButton(false);
Form2 frm2 = new Form2(this);
frm2.Show();
}
public void changeMyButton(bool state)
{
myButton.Enabled = state;
}
In Form2 :
Form1 form1Ref;
public Form2(Form1 fRef)
{
InitializeComponent();
form1Ref = fRef;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.changeMyButton(true);
}
Good luck
zafer
|
|
|
|
|
This is not a good way to do it - passing references of a form to the constructor of another - yuk!
Why not just suscribe to the second form's FormClosing event? Something like...
void myButton_Click(object sender, EventArgs e)
{
myButton.Enabled = false;
Form2 frm2 = new Form2();
frm2.FormClosing += new FormClosingEventHandler(frm2_FormClosing);
frm2.Show();
}
void frm2_FormClosing(object sender, FormClosingEventArgs e)
{
myButton.Enabled = true;
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Sorry I disagree, what do you mean by "not good"?
Are you aware that in form1 code you are writing code related with form2. What if you have form3, form4 etc.
Than your form1 gets larger and larger.
Passing the reference of the main form to other forms is a practical way of form communication.
|
|
|
|
|
Form2 has no purpose to know about form1 at all. It does nothing with it at all so the reference it pointless.
Form1 however neeeds to know when form2 is closing so it should listen for that event.
That's what events and delegates are designed for. If any object needs to tell any other objects about anything at all it should raise an event so any interested (subscribed) objects can react.
zafersavas wrote: What if you have form3, form4 etc.
This applies the other way too - what if you have multiple objects that need to know that form2 is closing? You'd need to pass every object in the constructor. How can you predetermine how many there will be or what types? It makes the constructor(s) virtually unmaintainable. By using events/delegates there is no limit to the number or type of objects that can have methods invoked from the object raising the event.
That's what OOP is all about!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Well, this topic seems going deeper
DaveyM69 wrote: what if you have multiple objects that need to know that form2 is closing? You'd need to pass every object in the constructor. How can you predetermine how many there will be or what types?
absolutely no, no and no, have a look at this:
Form1 : is the main form, Form2 and Form3 are declared here.<br />
Form2(Form1 frmRef);<br />
Form3(Form1 frmRef);
2 knows 1, 1 knows 3 so 2 can know 3 through 1.
Just pass Form1 as a constructor parameter and dont think the rest. As every form is aware of Form1 and Form1 is where other forms are declared, this means "everybody knows everybody". Form1 is something like a common node for all others. So as you see you dont have to pass other forms to the constructor.
Anyway, its a way up to you. No problem if it is efficient and causing any trouble.
Kind regards
zafer
|
|
|
|
|
zafersavas wrote: Well, this topic seems going deeper
Yeah, don't want to get into this too deep - this board probably isn't the right place, and don't want to hijack the thread! You say tomato, I say... etc etc.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|