|
Hello ,
I am using VC++.Net 2003. I am trying to open word from a form.
I tried to do what you have stated in your article
but I cant find "customise component" but add or remove items, when
I click this I get the .NET tab and the Com tab and in the .Net tab
I cant find winword.dll.
I have tried adding reference to ms word 11.0 object library and tried the following code
on button click
WordApp = new Microsoft::Office::Interop::Word::ApplicationClass();
object *fileName = "c:\\test.doc";
object *readOnly = false;
object *isVisible = true;
// Here is the way to handle parameters you don't care about in .NET
object *missing = System::Reflection::Missing::Value;
// Make word visible, so you can see what's happening
WordApp->Visible = true;
// Open the document that was chosen by the dialog
Word::Document *aDoc = WordApp->Documents::Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing,ref missing,ref missing, ref isVisible);
// Activate the document so it shows up in front
aDoc->Activate();
// Add the copyright text and a line break
WordApp->Selection::TypeText("Copyright C# Corner");
WordApp->Selection::TypeParagraph();
(This is taken from C# corner)when I complie this I get
error c2039, Open is not a member function of Microsoft::Office::Interop::Word::ApplicationClass();
Can you please help I want to open word from a form and be able to write
images in picture box to word.
Minad
-- modified at 6:47 Tuesday 11th September, 2007
|
|
|
|
|
Hi there
i tried to use this control on my form. it is working good but only once. when i close the form and try again to open the form "The rpc Server is not available (Exception From HResult: 0x800706BA)"
Please guide me to the solution
Salman Sheikh
|
|
|
|
|
i got this error,too.
anybody can give a solution,thanks!
|
|
|
|
|
Try modify the LoadDocument, by adding wd.visible = true; :-
public void LoadDocument(string t_filename)
{
deactivateevents = true;
filename = t_filename;
//use preactivate to init
PreActivate();
wd.Visible = true; //<--ADD THIS LINE
try
{
wd.CommandBars.AdaptiveMenus = false;
|
|
|
|
|
I'm working on a project started by another developer. The project successfully loads a word document using the WinWordControl. However, the WinWordControl uses the Microsoft Word default, which is Portrait. I'd like to instruct the WinWordControl to set Word's Page Setup properties to Landscape. Any pointers?
Also, I tried to accomplish the default of landscape by just starting Word normally, and setting the default from portrait to landscape. Microsoft Word stores this value in normal.dot if you click the default button and say Yes to saving the change as the default in the Page Setup dialog box. However, there's only one normal.dot file on my hard drive, and clearly, the WinWordControl is not using my normal.dot. Any pointers on how to specify the default .dot that WinWordControl uses?
Thanks,
Randy
|
|
|
|
|
hy i am using in a school project your idea to incorporate a word application in a winform and i have found some bugs that i hope you could help fix or understand some of them...i am using vs2005 and office 2007. my objective is to incorporate office 2007 in a winform and use the new features content controls. my problems are:
-after using an addin to create a task pane in normal office the taskpane is shown but in the office from the winform it doesn`t show the taskpane.
-the other problem is that using the events of the microsoft.office.interop.word.application.activedocument like contentcontrolafteradd fires right but i don`t know why if i try to modify a components property it doesn`t do anything.If i tell it to show me a message box in that event handler it shows it but if i tell it textbox.text="assdfsd"; ,textbox being a textboxe in that form, it enters the event when a add a content control but when it gets to the command that modifies a component it just exits the event handler.
please if you can help me with an answer ...
|
|
|
|
|
Hi Everyone!
I used Winwordcontrol.dll to display Word docuemnt on Form. However i have a problem, right now. That is, I want to check spelling and grammar for the document which is displayed on form. And during checking, if i see the word which is incorrect, i'll hightlight it. How can i do that, please help me if possible. Please contact to me by email: vinh_2001pt@yahoo.com. Thanks a lot.
|
|
|
|
|
Hi there.
It's a great work, but I just wonder if we could load another application into this form.
If we could, please tell me how?
Any feedback will be appreciated.
Thanks,
An Nguyen
|
|
|
|
|
i am using this control in my application, it is working fine, but the problem what i am facing is that,
when i open any document in the contorl, it opens as a new unsaved docuement,
and when i save it, it asks me for SaveAs, though its already a saved doument.
i programmed in the winwordcontrol but failed to do so. I want to open a saved document.
if any one can help me on this matter.
Rakesh singh
Contactrakeshsingh@gmail.com
Rakesh Singh
|
|
|
|
|
Me too, I want to use this control to store document on database such as MS Access or MS SQL Server. If you have any solutions, please help me and send to my email: npkinh@yahoo.com
Thanks,
Kinh
-- modified at 13:14 Sunday 7th October, 2007
Good luck
|
|
|
|
|
Add me to the list of people who want a way to save existing files. For a while I was sure I could do it by handling the OnBeforeSave event like so:
wd.DocumentBeforeSave += new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(OnSave);
private void OnSave(Word.Document doc, ref bool SaveAsUI , ref bool Cancel)
{
Cancel = false;
SaveAsUI = false;
object missing = null;
object FileName = filename;
object FileFormat = Word.WdSaveFormat.wdFormatDOSText;
object LockComments = false;
object AddToRecentFiles = true;
object ReadOnlyRecommended = false;
object EmbedTrueTypeFonts = false;
object SaveNativePictureFormat = true;
object SaveFormsData = false;
object SaveAsAOCELetter = false;
object Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUSASCII;
object InsertLineBreaks = false;
object AllowSubstitutions = false;
object LineEnding = Word.WdLineEndingType.wdCRLF;
object AddBiDiMarks = false;
System.Windows.Forms.MessageBox.Show((string)FileName);
doc.SaveAs(ref FileName, ref FileFormat, ref LockComments,
ref missing, ref AddToRecentFiles, ref missing,
ref ReadOnlyRecommended, ref EmbedTrueTypeFonts,
ref SaveNativePictureFormat, ref SaveFormsData,
ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks,
ref AllowSubstitutions, ref LineEnding, ref AddBiDiMarks);
}
...except it doesn't have any effect! I still get the SaveAs dialog box with a default filename rather than the original file name. Anyone else have any luck? This code would sure be useful if you could save changes to existing files. Without that it's a glorified multiline, read-only textbox!
Mike Cooper
|
|
|
|
|
You can try this workaround:
document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
//Workaround - Save the Word Document after Opening it
document.SaveAs(ref fileName, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
Note I am using Office XP PIAs available @ http://www.microsoft.com/downloads/details.aspx?FamilyID=c41bd61e-3060-4f71-a6b4-01feba508e52&DisplayLang=en[^]
If you find any other solution please let me know
Regards
Karim
|
|
|
|
|
i have same problem
is there any good solution please help me
thank in advance
|
|
|
|
|
Hi..
This control is very useful.I am completing the project using this control.
But one error is occur while Processing the another word document in running
of this control. Parallel processing is possible or not.Please slove my
issue.
Thanks in advance.
sdivya
|
|
|
|
|
Well I've wasted 2 hours monkeying around with the code sixteen different ways trying to get this thing working with Microsoft Visual Studio .NET 2003 & Office Pro 2003....without any success whatsoever. The stock "out-of-the-box" code is completely inoperative; although I did manage a bit of handywork myself adding what appeared to be a couple missing references and got MS Word to magically launch itself from the control...but outside of the window I'd hoped to emed the Word interface in.
As is discussed elsewhere here, this might have been marginally useful deploying as a technology with MS Office 2000, but I can't see the utility in it with any subsequent verisons of Word. It certainly doesn't work as advertised, and even if it ever works - it is surely on so narrow an install base you'd have a better chance of being hit by lighting than find yourself with a PC this thing actually compiles and runs on without issue.
Respectfully submitted,
RH (ross.holder[AT]gmail.com)
Ottawa, ON
|
|
|
|
|
You're right about it being inoperative out of the box. Not sure which platform it was originally coded in. But I've managed to get it up in running on VS2005 using Office Pro2003. Following is the code for winwordcontrol.cs with my changes. In addition, if your solution is referencing the Microsoft 12.0 Object Library (it will be if the code went through VS's conversion routine), you will need to delete that reference and add a reference to the Microsoft 11.0 Object Library. Good Luck!
(Everything below this line)
/// This code is written by Matthias Haenel
/// contact: www.intercopmu.de
///
/// you can use it free of charge, but please
/// mention my name
///
/// WinWordControl utilizes MS-WinWord2000 and
/// WinWord-XP
///
/// It simulates a form element, with simple tricks.
///
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinWordControl
{
public class DocumentInstanceException : Exception
{}
public class ValidDocumentException : Exception
{}
public class WordInstanceException : Exception
{}
///
/// WinWordControl allows you to load doc-Files to your
/// own application without any loss, because it uses
/// the real WinWord.
///
public class WinWordControl : System.Windows.Forms.UserControl
{
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);
[DllImport("user32.dll")]
static extern int SetParent( int hWndChild, int hWndNewParent);
[DllImport("user32.dll", EntryPoint="SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // handle to window
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags // window-positioning options
);
[DllImport("user32.dll", EntryPoint="MoveWindow")]
static extern bool MoveWindow(
int hWnd,
int X,
int Y,
int nWidth,
int nHeight,
bool bRepaint
);
/* I was testing wheater i could fix some exploid bugs or not.
* I left this stuff in here for people who need to know how to
* interface the Win32-API
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern int GetWindowRect(int hwnd, ref RECT rc);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(
int hWnd,
int msg,
int wParam,
int lParam
);
*/
const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
private Word.Document document;
private static Word.ApplicationClass wd = null;
public static int wordWnd = 0;
public static string filename = null;
private static bool deactivateevents = false;
///
/// needed designer variable
///
private System.ComponentModel.Container components = null;
public WinWordControl()
{
InitializeComponent();
}
///
/// cleanup Ressources
///
protected override void Dispose( bool disposing )
{
CloseControl();
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
///
/// !do not alter this code! It's designer code
///
private void InitializeComponent()
{
//
// WinWordControl
//
this.Name = "WinWordControl";
this.Size = new System.Drawing.Size(440, 336);
this.Resize += new System.EventHandler(this.OnResize);
}
#endregion
///
/// Preactivation
/// It's usefull, if you need more speed in the main Program
/// so you can preload Word.
///
public void PreActivate()
{
if(wd == null) wd = new Word.ApplicationClass();
}
///
/// Close the current Document in the control --> you can
/// load a new one with LoadDocument
///
public void CloseControl()
{
/*
* this code is to reopen Word.
*/
try
{
deactivateevents = true;
object dummy=null;
document.Close(ref dummy, ref dummy, ref dummy);
document.Application.Quit(ref dummy, ref dummy, ref dummy);
deactivateevents = false;
}
catch
{
}
}
///
/// catches Word's close event
/// starts a Thread that send a ESC to the word window
///
/// <param name="doc" />
/// <param name="test" />
private void OnClose(Word.Document doc, ref bool chancel)
{
if(!deactivateevents)
{
chancel=true;
}
}
///
/// catches Word's open event
/// just close
///
/// <param name="doc" />
private void OnOpenDoc(Word.Document doc)
{
OnNewDoc(doc);
}
///
/// catches Word's newdocument event
/// just close
///
/// <param name="doc" />
private void OnNewDoc(Word.Document doc)
{
if(!deactivateevents)
{
deactivateevents=true;
object dummy = null;
doc.Close(ref dummy,ref dummy,ref dummy);
deactivateevents=false;
}
}
///
/// catches Word's quit event
/// normally it should not fire, but just to be shure
/// safely release the internal Word Instance
///
private void OnQuit()
{
//wd=null;
}
///
/// Loads a document into the control
///
/// <param name="t_filename" />path to the file (every type word can handle)
public void LoadDocument(string t_filename)
{
Object SaveasUI = false;
Object Cancel = true;
Object truy = true;
deactivateevents = true;
filename = t_filename;
if(wd == null) wd = new Word.ApplicationClass();
try
{
wd.AddIns.Add("VisualADP.dot", ref truy);
wd.CommandBars.AdaptiveMenus = false;
wd.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);
((Word.ApplicationEvents4_Event)wd).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(OnNewDoc);
/// wd.NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(OnNewDoc);
wd.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(OnOpenDoc);
wd.ApplicationEvents4_Event_Quit += new Word.ApplicationEvents4_QuitEventHandler(OnQuit);
}
/// Word.ApplicationEvents4_NewDocumentEventHandler
catch{}
if(document != null)
{
try
{
object dummy=null;
wd.Documents.Close(ref dummy, ref dummy, ref dummy);
}
catch{}
}
if( wordWnd==0 ) wordWnd = FindWindow( "Opusapp", null);
if (wordWnd!=0)
{
SetParent( wordWnd, this.Handle.ToInt32());
object fileName = filename;
object newTemplate = false;
object docType = 0;
object readOnly = true;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
try
{
if( wd == null )
{
throw new WordInstanceException();
}
if( wd.Documents == null )
{
throw new DocumentInstanceException();
}
if( wd != null && wd.Documents != null )
{
document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
}
if(document == null)
{
throw new ValidDocumentException();
}
}
catch
{
}
try
{
wd.ActiveWindow.DisplayRightRuler=false;
wd.ActiveWindow.DisplayScreenTips=false;
wd.ActiveWindow.DisplayVerticalRuler=false;
wd.ActiveWindow.DisplayRightRuler=false;
wd.ActiveWindow.ActivePane.DisplayRulers=false;
wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView; // .wdNormalView;
}
catch
{
}
int counter = wd.ActiveWindow.Application.CommandBars.Count;
for(int i = 0; i < counter;i++)
{
try
{
wd.ActiveWindow.Application.CommandBars[i].Enabled=false;
}
catch
{
}
}
try
{
wd.Visible = true;
wd.Activate();
SetWindowPos(wordWnd,this.Handle.ToInt32(),0,0,this.Bounds.Width+20,this.Bounds.Height+20, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME);
MoveWindow(wordWnd,-5,-23,this.Bounds.Width+10,this.Bounds.Height+57,true);
}
catch
{
MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
}
this.Parent.Focus();
}
deactivateevents = false;
}
///
/// restores Word.
/// If the program crashed somehow.
/// Sometimes Word saves it's temporary settings
///
public void RestoreWord()
{
try
{
int counter = wd.ActiveWindow.Application.CommandBars.Count;
for(int i = 0; i < counter;i++)
{
try
{
wd.ActiveWindow.Application.CommandBars[i].Enabled=true;
}
catch
{
}
}
}
catch{};
}
///
/// internal resize function
/// utilizes the size of the surrounding control
///
/// optimzed for Word2000 but it works pretty good with WordXP too.
///
/// <param name="sender" />
/// <param name="e" />
private void OnResize(object sender, System.EventArgs e)
{
MoveWindow(wordWnd,-5,-23,this.Bounds.Width+10,this.Bounds.Height+57,true);
}
}
}
Mike Cooper
|
|
|
|
|
I'd like to do the same using MS Project 2000. However, I can't find an appropriate DLL.
Any ideas?
|
|
|
|
|
Hi ,
great work!!! thanx alot for the source code. With this i have completed my project almost 50%.
I have a question & hope u can help me with this: i tried to run two instances of the demo application, first i loaded a document in the first instance, & when i tried to load another one in the other instance, it loaded but the document in the first instance disappeared. Plz help me as soon as possible.
I am getting an error in the following line of Code; I am not able to compile it
[wd.NewDocument += new Word.ApplicationEvents3_NewDocumentEventHandler(OnNewDoc);]
hope to hear from u soon.. thanx in advance
Yesbee
|
|
|
|
|
I try set readeonly property for winwordcontrol but it's fail
my code as:
Word.Document document;
document.ReadOnly=True;
Please help me!
Thanks much
|
|
|
|
|
Hi,
I want to set read only for document after opening.
Please help me!!
Thanks
DTAN
|
|
|
|
|
hello,
you can can do by, progamming a little in the Winwordcontrol
open the project in C#, in the class file go to the method LoadDocument
there you will find
if condition displaying
if( wd != null && wd.Documents != null )
{
document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
document.ReadOnly=true;
}
regards,
Rakesh singh
Contactrakeshsingh@Gmail.com
Rakesh Singh
|
|
|
|
|
I am doing research on how to host a document of another app (like Word) in our C# Application. I ran accross this control and apply started playing with it. I have it working and I like what it does but I am puzzled by one thing. I noticed that if I am on another window and I click on the window with the word document that the word document will become active and I can move the cursor and type but it doesn't move to the front of the pack. My form doesn't know its been clicked and there fore doesn't move up in the Z order. Has anyone faced this...and if so does anyone know how to fix it or get around it?
Also I noticed that when I have focus on the word document that the hosting window appears the inactive. Is this something that has to be lived with too?
Thanks,
Brandon
|
|
|
|
|
Hi, I want to make the opened word document as read only as well as I want to disable all the keys on the keyboard except to scroll up and down on the document.
Nikesh
|
|
|
|
|
Using the LoadDocument function will allow me to make a new document and it will cp[y a previous one. But how do I open one so that if I were to hit save, it wouldn't need to prompt me etc?
Thanx
Mike
|
|
|
|
|
i changed this control to open a PPT application but i am getting a problem that when i close the form on which i am using that control the following error occurs
"Microsoft occurs a problem to close this application"
and after that a new ppt window is opened..
i am doing the following chages in defining events of this control to open PPT
//wd.CommandBars.AdaptiveMenus = false;
//wd.DocumentBeforeClose += new ppt.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
//wd.NewDocument += new ppt.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
//wd.DocumentOpen+= new ppt.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
//wd.ApplicationEvents2_Event_Quit += new ppt.ApplicationEvents2_QuitEventHandler(OnQuit);
this line are replaced by the following
wd.CommandBars.AdaptiveMenus = false;
wd.PresentationOpen += new ppt.EApplication_PresentationOpenEventHandler(OnOpenDoc);
wd.EApplication_Event_NewPresentation += new ppt.EApplication_NewPresentationEventHandler(OnNewDoc);
IF anybody have any idea to solve this Please let me know
Thnks
|
|
|
|
|