Drag&Drop event word//link dropped picture

D

dpomt

Hello,

what I want to do is to change D&D behavior when an image from explorer
is dropped to word. The standard behavior of Word is to embed the image - I
want to insert only a reference/link to the image. I did not find any
possiblity to let Word link the image. When doing a D&D with right mouse
button, Word displays a context
menu with "link here", but it's grayed out.

Any suggestions how I could achive to get a linked image when
doing D&D from explorer to Word - either by changing some Word settings or by
implemting an add-in - are greatly appreciated.

Thanks
dpomt
 
C

Cindy M.

Hi =?Utf-8?B?ZHBvbXQ=?=,
what I want to do is to change D&D behavior when an image from explorer
is dropped to word. The standard behavior of Word is to embed the image - I
want to insert only a reference/link to the image. I did not find any
possiblity to let Word link the image. When doing a D&D with right mouse
button, Word displays a context
menu with "link here", but it's grayed out.

Any suggestions how I could achive to get a linked image when
doing D&D from explorer to Word - either by changing some Word settings or by
implemting an add-in - are greatly appreciated.
I can't really provide any concrete help, just give you some information.

1. There is no setting in Word you can change to enable Word to do this
automatically.

2. Word provides no drag-and-drop events. You'd need to use an Add-in and work
with what Word exposes to the Windows API. There have been some discussions
along this line in the VSTO forum (http://social.msdn.microsoft.com/Forums/en-
US/vsto/threads/#page:1), for example. Those mainly focus on drag-and-drop from
a task pane in the Word application to the document, but I imagine the same
principles would apply to most scenarios.

Beyond that, the WindowSelectionChange event of the Word Application should
fire when something is dropped on the document. Problem is, it will fire at
just about anything except typing text...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

\Ji Zhou [MSFT]\

Hello dpomt,

Based on my research, the right click drag/drop can fit this demand in my
side. When I right click a picture file and drag it from the Windows
Explorer into my Word document, I can see there pops up a context menu with
the following options,

*Move Here
*Copy Here
*Create Shortcut Here
*Create Hyperlink Here
*Cancel.

Only the Create Shortcut Here option is disabled(gray out). And when I
click the "Create Hyperlink Here", it drops the picture's url into the
document, but the picture is not embedded in that document. I have tried in
the Word 2003 and 2007. They both work like this way. Is this behavior you
are trying to achieve? If I misunderstood the issue, please feel free to
correct me!

Have a nice day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dpomt

Hello Ji,

thanks for your quick reply.
Only the Create Shortcut Here option is disabled(gray out). And when I
click the "Create Hyperlink Here", it drops the picture's url into the
document, but the picture is not embedded in that document. I have tried in
the Word 2003 and 2007. They both work like this way. Is this behavior you
are trying to achieve? If I misunderstood the issue, please feel free to
correct me!
I have to clarify: what I want to do is not to insert a hyperlink (this
works fine) but a shortcut (the option that is grayed out).
 
J

\Ji Zhou [MSFT]\

Hello dpomt,

Thanks for the clarification! As Cindy has pointed that there is no setting
in the Word for this objective and we have to implement an Add-in if the
objective is really required. Since the Word object model does not provide
the drag and drop event, we have to use the low level Windows API to
achieve this. Detailed information goes as follows,

After some research, I find that when we drop a file into the Word
document, there will be WM_DROPFILES message sent to an child window of
Word. So, I think we can create an Add-in which subclasses that child
window. If we get an WM_DROPFILES message, we determine if it has an
picture type extension name. If yes, we create a shortcut of the picture
and omit the current WM_DROPFILES message. If not, we call the base message
handle function.

I have written a simple C# VSTO Word Add-in for test. The following codes
work fine in my side,

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
h = NativeMethods.FindWindowExW(h, new IntPtr(0), "_WwF", "");
h = NativeMethods.FindWindowExW(h, new IntPtr(0), "_WwB",
this.Application.ActiveDocument.Name);
h = NativeMethods.FindWindowExW(h, new IntPtr(0), "_WwG",
"Microsoft Word Document");

SubclassHWND s = new SubclassHWND();
s.AssignHandle(h);
}

public partial class NativeMethods
{

[System.Runtime.InteropServices.DllImportAttribute("shell32.dll",
EntryPoint = "DragQueryFileW", CallingConvention =
System.Runtime.InteropServices.CallingConvention.StdCall)]
public static extern uint DragQueryFileW(System.IntPtr param0,
uint param1,
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropSer
vices.UnmanagedType.LPWStr)] System.Text.StringBuilder param2, uint param3);
}

public partial class NativeMethods
{

[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint
= "FindWindowExW")]
public static extern System.IntPtr
FindWindowExW([System.Runtime.InteropServices.InAttribute()] System.IntPtr
hWndParent, [System.Runtime.InteropServices.InAttribute()] System.IntPtr
hWndChildAfter, [System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropSer
vices.UnmanagedType.LPWStr)] string lpszClass,
[System.Runtime.InteropServices.InAttribute()]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropSer
vices.UnmanagedType.LPWStr)] string lpszWindow);
}

public class SubclassHWND : NativeWindow
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x233)
{
StringBuilder file = new StringBuilder(100);
NativeMethods.DragQueryFileW(m.WParam, 0, file, 100);
System.IO.FileInfo fi = new FileInfo(file.ToString());
if (fi.Extension.ToLower() == ".jpg")
{
Debug.Print("Detect dropping a picture into the
document");
object DisplayAsIcon = true;
object LinkToFile = true;
object FileName = file.ToString();
object IconName = file.ToString();
object missing = Type.Missing;


Globals.ThisAddIn.Application.Selection.InlineShapes.AddOLEObject(ref
missing, ref FileName, ref LinkToFile, ref DisplayAsIcon,
ref missing, ref missing, ref IconName, ref
missing);
}
else
{
base.WndProc(ref m);
}
}
else
{
base.WndProc(ref m);
}
}
}

You can also make a little modification to get it work in a Shared Add In.
If you have any future question or concerns, please feel free to let me
know. I will be more than happy if I can provide future assistance.

Have a nice day!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dpomt

Hello Ji,

thanks a lot for your reply, idea and code sample.
I have built a VSTO project and with two minor modifications it works like a
charme.
One modification was that I have to use "Microsoft Word-Dokument" for third
FindWindowExW call in my german environment. Second modification to use
Globals.ThisAddIn.Application.ActiveDocument.Shapes.AddPicture with
LinkToFile=true.

You can also make a little modification to get it work in a Shared Add In.
Could you be more precise what have to be done?

If you have any future question or concerns, please feel free to let me
know. I will be more than happy if I can provide future assistance.
I have a slightly different D&D request for PPT 2007, but I will open a new
thread.


Again, thanks for your excellent post.
Best regards
dpomt
 
J

Jialiang Ge [MSFT]

Hello dpomt,

Ji is not in office from today to the end of the week. I will help you with
the follow-up question.

Shared Add-in is a template that is provided by VS and doesn't require VSTO
to be installed. Add-in created using this approach inherits from
IDTExtensibility2 interface. I list the major commonness and differences
between Shared Add-in and VSTO Add-in at the bottom of this message. As a
quick tutorial of how to write a shared add-in targeting Word, please refer
to the KB article:

How to build an Office COM add-in by using Visual C# .NET
http://support.microsoft.com/kb/302901/en-us

If you would like to write a shared add-in instead of a VSTO add-in in this
task, please refer to these steps:

1. Create a shared add-in project targeting Word according to the steps 1-4
in KB 302901 (In Step 4.b, select Microsoft Word only)

2. Add the reference of Office Word Primary Interop Assembly (Right click
on the project -> Add Reference -> COM -> Microsoft Word 11/12.0 Object
Library)

3. Add the line
using Word = Microsoft.Office.Interop.Word;
into the Connect.cs file

4. Replace the line
private object applicationObject;
with
private Word.Application applicationObject;

5. In the OnConnection method, replace
applicationObject = application;
with
applicationObject = (Word.Application)application;

6. Copy & paste Ji's code of ThisAddIn_Startup into the OnStartupComplete
method.

7. Copy & paste Ji's rest code into Connect.cs. Replace the line

Globals.ThisAddIn.Application.Selection.InlineShapes.AddOLEObject(ref
missing, ref FileName, ref LinkToFile, ref DisplayAsIcon,
ref missing, ref missing, ref IconName, ref
missing);

with

applicationObject.Selection.InlineShapes.AddOLEObject(ref
missing, ref FileName, ref LinkToFile, ref DisplayAsIcon,
ref missing, ref missing, ref IconName, ref
missing);

8. Compile the project and run the setup.

=========================================
The commonness of the Shared Add-In and the VSTO Add-In is

1. Both of them are registered in the host application's registry key, for
example, HKCU\Software\Microsoft\Office\Word\Addins. After Office loading
them, they will both appear in the COM Add-in Dialog.

2. They both build upon the COM interface to do Office extensibility by
consuming the Office Object Model. And they can both add the extensible
UIs, like custom task pane, ribbon, outlook form region to Office
application.

=========================================
The major differences between the Shared Add-In and the VSTO Add-In.

1. As the name indicates, one Shared Add-In can be shared among several
different Office applications. So, it needs additional codes to judge the
host application in our Add-In. But in Visual Studio Tools for Office
Add-Ins, one Add-In can only serve one host application. So, in our Add-In,
what we are working on is strong typed host application. We do not need to
cast the application object to a specific one, or call its function via
late binding anymore.

2. To create the UI extensibility, ribbon, custom task pane, form region,
in the Shared Add-In, the programmers have to implement three interfaces,
Office.IRibbonExtensibility, Office.ICustomTaskPaneConsumer,
Outlook.FormRegionStartup. No matter creating which one of the extensible
UIs, we need a lot of manual work to write codes. But if we are using the
Visual Studio Tools for Office, everything is wrapped for us. We just need
to add a new Ribbon, UserControl, or FormRegion item into our VSTO Add-In
project. It is now supported to design these three UIs from the VSTO
designer. All we need to do are just dragging and dropping controls,
setting properties and double clicking to generate the event handle like we
are used to do in the Windows Form Designer.

3. Another importance difference is the security model. There is no
requirement for IDTExtensibility2 Shared Add-Ins to have any CAS policy
associated with them. So, before the Office can load the Shared Add-In, we
do not need to call the caspol to grant full trust to the Add-In assembly.
But as to VSTO, this security granting process is exactly necessary. The
VSTO uses the .NET security model to trust the Add-In managed assembly.
Consequently, it is more secure than the Shared Add-In. but takes some
extra work for us to do in the deployment.

4. Visual Studio Tools for Office creates a new AppDomain to load each of
the VSTO Add-Ins. So, it will not affect with each other. And the
programmers will not be worry about the "Outlook won't shut down" issue.
When the VSTO Add-in shuts down, the AppDomain gets unload and it will do
the clean for us.


If you have any other questions or concerns about this thread, please feel
free to tell me.

Have a very nice day!


Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
D

dpomt

Hello Jialang,

thanks a lot for the detailled description. I will give that a try.


Best regards
dpomt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top