Lets Learn

Opinion Matters

Archive for October, 2009

Have you ever thought of inserting an HTML page into Word directly?

Posted by Ankush on October 5, 2009

Well, it is very easy.

Simply call ActiveDocument.Range.InsertFile “test.htm”, , , True

Here is the source code for test.htm

==
<html>
This is the image
<img width=625 height=500 src=”image002.jpg”>
</html>
==

But here comes the interesting part. Let’s say you have some images referenced in the HTML page and you insert this HTML page into Word document. Now just delete (rename) the image referenced in the HTML page and open the Word document, you don’t see the images in the document anymore..WHY???

Because they are linked. So to unlink them

====
Sub test()
ActiveDocument.Range.InsertFile ” test.htm “, , , True
ActiveDocument.Fields.Unlink
ActiveDocument.Save
End Sub
====

This code works fine in Word 2003 but doesn’t work in Word 2007 for some reason. To make this to work, you need to call an extra save. Here is an updated code

===============
Sub test()
ActiveDocument.Range.InsertFile “C:\Users\Administrator\Desktop\a.htm”, , , True
ActiveDocument.Save
ActiveDocument.Fields.Unlink
ActiveDocument.Save
End Sub
==============

Hope this helps!!!!

Posted in Uncategorized | Tagged: , , , | Leave a Comment »

Excel Warning Message

Posted by Ankush on October 5, 2009

Hi Guys,

This is my first blog and today I write about a new security feature which has been added onto Excel 2007 and is named Extension Hardening.

When you open a file in Microsoft Office Excel 2007 that has content, it does not match the file’s extension; you receive the following warning message:

The file you are trying to open, ‘abc.xyz’, is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

Note: abc.xyz is the file name that you are trying to open in Excel.

Once this warning is displayed, then a user can decide whether he/she wants to open the file or not.  However this warning can be set to different levels via the registry editor.  You can check/set the settings as per the following:

  • Click Start, Run, type regedit, and then click OK.
  • Locate and then click the following registry subkey: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security
  • On the Edit menu, point to New, and then click DWORD Value.
  • Type ExtensionHardening, and then press ENTER.
  • Right-click ExtensionHardening, and then click Modify.

 In the Value data box, type the value data, and then click OK.
 
The following list contains the value data settings that are appropriate for the ExtensionHardening setting:

Value data

Warning Message

0 Do not check the file name extension and the file type, and bypass the function of the warning message
1 Check the file name extension and the file type. If they do not match, display the warning message.  
2 Check the file name extension and the file type. If they do not match, do not open the file. 

Default setting if value not present is 1 (enable and prompt).If this key is not present, you will see the dialog similar in case of Value data 1.

 Please have a look at the different warnings. Depending upon the value data, you get the option whether you can bypass the message and open the file or not.

Note: If user gets the warning message and you don’t see the ExtensionHardening key under  HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security, then also check at HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\12.0\Excel\Security

Now I have explained about the ExtensionHardening warning, let’s see how we can bypass it.

One easy method is change the registry and set the value data to 0. But if changing the registry is not possible then  ?????

To bypass this, we need to understand a little bit about this concept. Basically this warning comes when you generate the Excel file on the fly/export the content of a dataset as an Excel 2007 file and end user will open the file in excel 2007. So we need to change the underlying logic to generate the Excel file so that content of the file and the extension does match.

So let’s summarize, how we can do this:

  • CSV file: This is handy when you just want to put the data.
  • Generate an XML file: Generate a proper spreadsheet XML
  • Generate from GridView: Export the data as an Excel file.
  • Generate an Excel file using the Open XML SDK 2.0: Use Open XML SDK to generate the file.
  • Excel ADO: If you have a template, then using this approach, you can simply connect to the file and insert records.
  • Excel PIA’s: DO NOT use the Excel PIAs to generate a spreadsheet server-side. It’s not supported so don’t go on this route. For more information, refer: http://support.microsoft.com/kb/257757

In my upcoming blog I will write about every approach in detail with some sample codes.

So watch this space for some more …

Let me know what you think, things that I can include and if you have questions.

Posted in Uncategorized | Tagged: , , , , | 2 Comments »

 
%d bloggers like this: