Embed Word and excel program in Visual Basic project

How to add Edraw Excel Viewer Component or Word Viewer ActiveX Control to your Visual Basic 6.0 project

1. From the Project Menu select Components…
2. Select control “EDWord ActiveX Control Module” in the controls table.
3. Click the OK Button.
4. The control will now appear in your toolbox.
5. Drag and drop the control on your form.
6. Right click the control then choose the View Code… item.
7. Add the NotifyCtrlReady event and set the library file to load.
Private Sub EDWord1_NotifyCtrlReady()
EDWord1.LicenseName = “”
EDWord1.LicenseCode = “”
End Sub
8. Run the project.

How to insert HTML table into Word doc using Javascript?

Use Edraw Office Viewer Component or Word Viewer Component to get the Document interface, then you can do it by the office automation.

document.OA1.CreateNew(“Word.Document”);
var objWord = document.OA1.ActiveDocument;
var range = objWord.Range(0,0);
var WTable = objWord.Tables.Add(range, 3,3);
WTable.Cell(1,1).Range.Font.Name = “Times New Roman”;

WTable.Cell(1,1).Range.Text = “examples 1”;
WTable.Cell(1,2).Range.Font.Size = 18;
WTable.Cell(1,2).Range.Bold = true;
WTable.Cell(1,2).Range.Font.Italic = true;
WTable.Cell(1,2).Range.Text = “examples 2”;
WTable.Cell(2,1).Range.ParagraphFormat.Alignment = 1; // 0= Left, 1=Center, 2=Right
WTable.Cell(2,1).Range.Font.Name = “Arial”;
WTable.Cell(2,1).Range.Font.Size = 12;
WTable.Cell(2,1).Range.Bold = false;
WTable.Cell(2,1).Range.ParagraphFormat.Alignment = 2;
WTable.Cell(3,3).Range.Font.Name = “Times New Roman”;
WTable.Cell(3,3).Range.Font.Size = 14;
WTable.Cell(3,3).Range.Bold = true;
WTable.Cell(3,3).Range.Font.Underline = true;
WTable.Cell(3,3).Range.ParagraphFormat.Alignment = 0;
WTable.Cell(3,2).Range.Text = “examples 3”

Embed MS Word in the Visual C++ Project

Embed MS Word in the Visual C++ Project

Adding the word control to a simple dialog base Application
1. Begin a new MFC AppWizard(exe) dialog base Application.
2. Open The Resources Dialogs.
3. Select the Main Dialog and right click on it.
4. From pop-up menu select “Insert ActiveX Control…”.
5. Select Edraw Viewer Component For Word from the list.
6. Add a member variable to newly created control in the dialog class.
7. This will automatically generate new cpp and .h files which including the information of
edword control.
8. Call the MFC ClassWizard to add the control event message
To resize the control according the form add a new Window Message Handler WM_SIZE and
add the following code in OnSize virtual function
if(!::IsWindow(m_edword.m_hWnd)) return;
m_edword.MoveWindow(0,0,cx,cy);
See VCEDWordDemo c++ example
Upgrade a control in VC++
You can easily upgrade a VC++ project witch is using a EDWord control of a previous version:
1. Register the new version of control using the regsvr32 utility that is located in
windows system directory.
2. From Project menu select Add to Project -> Components and Controls
3. From Components and Control Gallery dialog select the folder Registered ActiveX
Controls
4. Find and select Edraw Viewer Component For Word and click OK to all next dialogs.
5. Rebuild the project.

How to verify whether the open document was modified

If you are developing the destop application with the office viewer component, you can use the “IsDirty” method to verify whether the open document is modified. When the open file has any modification, the IsDirty method return TRUE. After you save the document, the IsDirty return False again.

But if you are developing the web application, you need to add some extra codes. Because you are opening a file from remote server and the component can’t watch the file like destop application.

A good method is set another variable in your application. When you save the document, you set the variable true. Any time you can judge the dirty by the IsDirty method and your own variable.

BOOL GetIsDirty(){
if(OA.IsDirty()==Flase) return False;
else return m_bDirty;
}
event OnDocumentOpened()
{
m_bDirty = false;
}
function UploadFile()
{
……
m_bDirty = False;
}

Embedding ms office in C# Forms

With Edraw Office Viewer Component, it’s easy to embed the ms office program in c# froms or html pages. Only drag it to your form, you can call some method to do the office automation directly such create a Word document or initialize an Excel sheet.

1. Download Edraw Office Viewer Component then install it. Or you can register the officeviewer.ocx using regsvr32 command.
  C:\\WINNT\System32\regsvr32 OfficeVidewer.ocx
2. On successful registration you would get message dialog.
3. Add Office Viewer Active-X control on your form through customize toolbox window.
4. Drag and drop component from toolbox on your form and resize as per requirement.
5. Now add reference for “Microsoft Word Object Library 11.0” from add reference on solution explorer. Here I am using Word 2003, so object library version is 11.0.
6. You can see all added reference on solution explorer as shown in figure. Two reference has been added for Edraw Office Viewer  Component (AxOfficeViewer and OfficeViewer) and three libraries for Office & Word (Microsoft.Office.Core and Office and Word).
7. Define a variable for word document in public section of form as
   public Word.Document oDoc;
8. Add following code for button click event
private void button1_Click(object sender, System.EventArgs e)
{
     //Remove Office Viewer Component Title Bar and create a new Word document
     axOfficeViewer1.Titlebar = false;
     axOfficeViewer1.CreateNew(“Word.Document”);
     axOfficeViewer1.Activate();
     //Invoke Word properties
     oDoc = (Word.Document)axOfficeViewer1.ActiveDocument;
     oDoc.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;
     oDoc.ActiveWindow.DisplayRulers = false;
     oDoc.ActiveWindow.DisplayScreenTips = false;
     oDoc.ActiveWindow.DisplayHorizontalScrollBar = false;
     oDoc.ActiveWindow.DisplayVerticalRuler = false;
     oDoc.ActiveWindow.DisplayVerticalScrollBar = true;
}
10. Now you can add text, format it, send mail do all functionalities.
11. You can control all word functionalities to be made available or unavailable programmatically through code with all available objects, properties & Methods of word.

 

(Fixed) CloseDoc error in the V7.5.0.331

We are experiencing a problem with the Office Viewer component. We are using it embedded in a browser window (IE7). It is exhibiting some strange behavior under certain conditions.

These are the steps to reproduce:

If you open the browser window that has an instance of the office viewer on it, and use javascript to have the Office viewer open a word document
Have another instance of WORD open in “stand alone” mode and minimize it to the windows toolbar
While the first browser window is opening and loading the WORD document, press on the WORD toolbar icon, so it maximizes and displays on the desktop

If you get the timing exactly right, so you open the minimized document just before the browser embedded version of WORD paints itself, what you get is the two WORD documents are switched. The browser window displays the document that was in the minimized copy of WORD, and the stand alone version of WORD displays the document that should have been displayed in the browser instance.

The automation interface is still manipulating the correct document, even though the document is opened in the wrong container. For example, if you print the document using javascript in the browser, the document that gets printed is the one in the other window.

Have you ever come across this phenomenon?

I tried again, making absolutely sure that it was 7.5.0.331 installed, as you suggested. It is still happening. It is important that you follow the sequence precisely….if you open word before you open the OfficeViewer demo app then everything is OK. If you follow the following steps, it should occur:

1) Reboot your machine

2) Start the OfficeViewer HTML demo app by double clicking on officeviewer.htm in the samples folder. Enable the OCX by choosing: “allow blocked content”

3) Open an existing WORD document in the OfficeViewer App

4) Open a stand alone instance of word, and type a sentence in it.

5) Choose the “close doc” option on the OfficeViewer.htm menu. When the doc closes, instead of seeing an “empty” instance of WORD, you see the “blank” page. This is how you can tell it is going to happen. If you try the next test below, after hitting “Close doc” you will still see there is still an instance of word present with the toolbar, but no document..

6) Close the browser window (using the x). the stand alone instance of WORD will close, along with the browser window.

Clearly, in the above example, what has happened is the OfficeViewer has gotten mixed up, and is internally pointing to the stand alone instance of WORD, instead of the instance of word it was housing. So when the browser window closed, it shut down WORD as well.

If you try the following sequence, you will not see the stand alone instance of Office close:

Test 2:

1) Reboot your machine

2) Open a stand alone instance of word and minimize it to the task bar

3) Start the OfficeViewer HTML demo app by double clicking on officeviewer.htm in the samples folder. Enable the OCX by cohoosing: “allow blocked content”

4) Open an existing WORD document in the OffiveViewer App

5) Open a stand alone instance of word, and type a sentence in it.

6) Choose the “close doc” option on the OfficeViewer.htm menu. When the doc closes, unlike in the test above, you will still see WORD open with its toolbar, but no document.

7) Close the browser window (using the x). the browser window will close, but the stand alone instance of WORD will stay open

Clearly, having an instance of WORD open before you instantiate the OfficeViewer seems to keep the instance pointers correct. In fact, in some situations, “windowless” WINWORD.EXE processes are left running by OfficeViewer, and if one of those is running, then the problem will not occur. In fact, one of the possible work arounds I have considered is to actually start a copy of WINWORD.EXE if there is not one currently running…

Test 3:

After doing test 2, close both instances of WORD, and re-try test 1, without doing the reboot. Everything works correctly. As far as I can tell, once it works “correctly” it won’t happen again.

This is very reproducible on all of our corporate machines. Would it be possible for you to try again, following those instructions exactly? If you are absolutely sure it is not happening on your environments, then I will have to start looking at our machine configs….but I could literally spend weeks on it, and if it turns out that it in fact is happening on your machines as well, then it would be totally wasted time. So I would really appreciate it if you could try again, following that precise sequence above…it could same me hundreds of hours of effort!

Print MS Word Document to the special printer

When you use the PrintOut method to print the word document with the  component, you maybe get the follow error.

This error can appear if a default printer has not been designated or if the application is unable to locate an existing default printer. To correct this problem, try one of the following in Microsoft Windows:
If a printer is not available in the Print dialog box, add a printer.
If the application cannot find an existing printer that is already installed, set the printer as the default printer.
If a default printer is installed but the application is unable to use it, uninstall the printer driver, and then install the latest version of the printer driver.
If the printer is on a print server, make sure the printer is available, the network is functioning, the server is not stalled, the printer is not out of paper, or the printer is not suspended by the administrator. Printing issues associated with a network printer are best handled by your local network administrator.
For more information about setting up and troubleshooting printer connections, see Windows Help and Support. (Click the Start button, and then click Help and Support.)

Word cannot print. There is no printer installed.

There are some solutions for it.

1. Call the ShowDialog 4 to use the default printer.

2. Word:
    CWordApplication app(m_wnd.get_Application()); 
    CString strOldPrinter = app.get_ActivePrinter();
    app.put_ActivePrinter(strPrinter); 
    app.PrintOut(varFalse, varOptional, varOptional, varOptional,
        varOptional, varOptional, varOptional, varOptional, varOptional,
        varOptional, varOptional, varOptional, varOptional, varOptional,
        varOptional, varOptional, varOptional, varOptional, varOptional); 
    app.put_ActivePrinter(strOldPrinter);

CreateNew word document not functional when running within Word VBA

Steps to recreate:

1) Open Word
2) Open VBA Editor
3) Create a new UserForm
4) Put the control on the user form
5) Select file -> new and choose word document

No document is created or displayed. Same with opening an existing word doc on local file system. This same procedure works when creating a VBA userform in MS EXcel. The control successfully displays the new or existing doc. and the related word toolbars etc.

Any ideas?
-Mike

Disables the hotkeys in the MS Word programmatically

Edraw Viewer Component for Word provides some methods to disable the print, save and copy hotkeys.

boolean DisableSaveHotKey(boolean Disable);
Disables the Save keycodes.
Disalbe: Disables the CTRL+S, SHIFT+F12, Alt+SHIFT+F2, F12
boolean DisablePrintHotKey(boolean Disable);
Disables the Print keycodes.
Disalbe: Disables the CTRL+P, CTRL+SHIFT+F12, CTRL+F2, ALT+CTRL+I
boolean DisableCopyHotKey(boolean Disable);
Disables the Copy keycodes.
Disalbe: Disables the CTRL+C, CTRL+V, CTRL+X, SHIFT+DEL, SHIFT+INSERT, ALT+CTRL+V,
CTRL+SHIFT+C, CTRL+INSERT

You can call these methods at the DocumentOpened event.

SavetoServer and UploadAction.aspx

I’m testing the trial version of Edraw Component for Word v.7.4 befor upgrade from my version.
Everything it’s ok except SaveToServer : I’ve tried out both streaming and none, as in the sample code, but I’ve always the same message « Saved successfully ! » but it’s not so.
I’m developing an .asp application.
I can’t debug the UploadAction.aspx page because of the post method and I don’t know witch is the problem.
What does HttpPost method do?

My code in the SavetoServer method is :

[…]
document.getElementById(“OA1”).HttpInit
document.getElementById(“OA1”).HttpAddPostOpenedFile(myFile)
document.getElementById(“OA1”).HttpPost(myUploadPage)

Where myFile is initialized with the full logical path of the file on the server (es. \\Server_name\directory1\..\document.doc)
And myUploadPage is initialized with UploadAction.aspx location (es. http://…/UploadAction.aspx)

I’ve notice that when I open a file from the server the method IsReadOnly return allways true also if is not so.
I’ve tried also to give a new name to the file when I save to the server (replacing the original name with another in myFile variable), but no trace of the new file instead I receive the message « Save successfully ! ».

I’m developing also in C# for a .NET application that use framework 4 and here I’ve a different behaviour: I get error 17 corresponding to eSC_EndRequestFailed. What about this error ? when occur ?

Thanks for your attention