none
Office Interop Word Issue RRS feed

  • Question

  • I am having following issue while handling Word Automation using Microsoft.Office.Interop.Word library.
    I was trying to show a Dialog Box corresponding to "WdWordDialog.wdDialogContentControlProperties".

    using Microsoft.Office.Interop.Word;
    using System;

    namespace TestWordAutomation
    {
        public partial class TestReport 
        {
            private Application wordApplication;
            private Document wordDocument;
            private Object oMissing = System.Reflection.Missing.Value;

            public TestReport()
            {

                wordApplication = new Application();
                wordDocument = wordApplication.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                InitializeComponent();
            }

            private void btnTestWordAutomation_Click(object sender, EventArgs e)
            {
                Dialog newDialog = wordApplication.Dialogs[WdWordDialog.wdDialogContentControlProperties];

                newDialog.Show();
            }
        }
    }

    But the dialog box is not appearing. It landed into exception System.Runtime.InteropServices.COMException 

    Exception Message : {"This command is not available."}

    Exception ErrorCode : -2146823683

    Exception StackTrace : 

    System.Runtime.InteropServices.COMException was unhandled
      HelpLink=C:\Program Files\Microsoft Office\Office12\1033\WDMAIN11.CHM#37373
      Message=This command is not available.
      Source=Microsoft Word
      ErrorCode=-2146823683
      StackTrace:
           at Microsoft.Office.Interop.Word.Dialogs.get_Item(WdWordDialog Index)
           at WordAutomation.TestReport.btnTestWordAutomation_Click(Object sender, EventArgs e) in Projects\WordAutomation\WordAutomation\Report\TestReport.cs:line 98
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at WordAutomation.Program.Main() in Program.cs:line 18
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    Can anyone help to resolve this issue.

    Thanks,

    Mukul

    Thursday, April 12, 2012 10:17 AM

Answers

All replies

  • In order for this dialog to open the content control must be selected.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, April 12, 2012 12:43 PM
  • How do i do that.

    A code example would do great.

    Thanks

    Thursday, April 19, 2012 4:38 AM
  • The content control must already exist on the document that you open and your code must select it. Here is an example converted from VB.Net:

    public void MyCurrentTestRoutine()
    {
    	Word.Document doc = default(Word.Document);
    	Word.Range rng = default(Word.Range);
    	Word.Dialog newDialog = default(Word.Dialog);
    	doc = wApp.ActiveDocument;
    	rng = doc.Content;
    	if (rng.ContentControls.Count > 0) {
    		rng.ContentControls(1).Range.Select();
    		newDialog = wApp.Dialogs(Word.WdWordDialog.wdDialogContentControlProperties);
    		newDialog.Show();
    	}
    }


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, April 19, 2012 6:35 PM