none
Can I close Excel dialog when I operate the charts in Word document By using Microsoft.Office.Interop.Excel and Microsoft.Office.Interop.Word? RRS feed

  • Question

  •  

    /// <summary> /// 更新图表数据 /// </summary> /// <param name="chart"></param> /// <param name="bookMark"></param> /// <param name="dataArr"></param> protected void ChartUpdate(Interop.Word.Chart chart, string bookMark, Object[,] dataArr) { if (chart == null) { //logger.Warn("Word.Chart is null!"); return; } if (dataArr == null) { //logger.Warn("Data is null!"); return; } Object oMissing = System.Reflection.Missing.Value; Interop.Word.Chart chrt; Microsoft.Office.Interop.Excel.Workbook wb = null; Object bk = bookMark; try { //xlApp.ScreenUpdating = false; chrt = chart; if (chrt.ChartData == null) { //logger.Warn("chart.ChartData is null!"); return; } chrt.ChartData.Activate(); wb = (Excel.Workbook)chrt.ChartData.Workbook; if (wb == null) { //logger.Warn("chart.ChartData.Workbook is null!"); return; } wb.Application.ScreenUpdating = false; wb.Application.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized; Microsoft.Office.Interop.Excel.Worksheet wSh = (Excel.Worksheet)wb.Worksheets[1]; if (wSh == null) { //logger.Warn("chart.ChartData.Workbook.Worksheets[1] is null!"); return; } // save formats wSh.Cells.ClearContents(); Microsoft.Office.Interop.Excel.Range Rng = wSh.get_Range("A1", "A1"); Rng.get_Resize(dataArr.GetUpperBound(0) + 1, dataArr.GetUpperBound(1) + 1).Value2 = dataArr; chrt.SetSourceData("'Sheet1'!" + Rng.get_Resize(dataArr.GetUpperBound(0) + 1, dataArr.GetUpperBound(1) + 1).get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing), Type.Missing); chrt.Refresh(); wb.Application.ScreenUpdating = true; wb.Close(Type.Missing, Type.Missing, Type.Missing); } catch (System.Runtime.InteropServices.COMException ex) { //logger.Warn("Update chart failed!", ex); //xlApp.ScreenUpdating = true; } finally { if (wb != null) { try { Marshal.ReleaseComObject(wb); } catch { } } } }

    Look at this fragment code:

    chrt.ChartData.Activate();
    wb = (Excel.Workbook)chrt.ChartData.Workbook;
    
    if (wb == null)
    {
    //logger.Warn("chart.ChartData.Workbook is null!");
    return;
    }
    wb.Application.ScreenUpdating = false;
    wb.Application.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
    
    Microsoft.Office.Interop.Excel.Worksheet wSh = (Excel.Worksheet)wb.Worksheets[1];

    The problem is that .I want to change the data of the chart ,so I must open the Excel.exe.But every time ,the Excel will show a dialog ,so I can't update the data of chart .I must close the dialog by hand?Can I close the dialog of Excel by c#?Or Can Prevent the dialog from showing? I just want to update the charts data.



    赵召

    Tuesday, May 13, 2014 1:28 AM

Answers

  • See my other reply to your related question for information about avoiding the UI behavior...

    Normally, Excel will not display a dialog box when charting is activated. Without knowing what the dialog box is that you're seeing it's not possible to say whether or not there might be a way to avoid or dismiss it.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by 赵召 Wednesday, May 14, 2014 2:22 AM
    Tuesday, May 13, 2014 10:18 AM
    Moderator
  • ok.I confirmed my determination to use OpenXml.

    Thank you.


    赵召

    • Marked as answer by 赵召 Wednesday, May 14, 2014 2:22 AM
    Wednesday, May 14, 2014 2:22 AM

All replies

  • See my other reply to your related question for information about avoiding the UI behavior...

    Normally, Excel will not display a dialog box when charting is activated. Without knowing what the dialog box is that you're seeing it's not possible to say whether or not there might be a way to avoid or dismiss it.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by 赵召 Wednesday, May 14, 2014 2:22 AM
    Tuesday, May 13, 2014 10:18 AM
    Moderator
  • ok.I confirmed my determination to use OpenXml.

    Thank you.


    赵召

    • Marked as answer by 赵召 Wednesday, May 14, 2014 2:22 AM
    Wednesday, May 14, 2014 2:22 AM