Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > VSTO 2008, Custom Ribbon, Open a workbook with OpenFileDialog
 

VSTO 2008, Custom Ribbon, Open a workbook with OpenFileDialog

Hi All,

I am new to VSTO moving from VBA. I have an Excel 2007 workbook app with a custom ribbon that has a button. The idea is to click the button, display the open file dialog, aloow the user to selectanother Excel wookbook and programatically copy its contents to the current workbook.

I have done this in VBA but not quite sure how to do it in VSTO.

So far this is my code in the Ribbon1 class:
PrivateSub btnOpenCust_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnOpenCust.Click  

Dim custFile As New OpenFileDialog 

custFile.ShowDialog()

If custFile.ShowDialog = DialogResult.Cancel Then

MessageBox.Show("You canceled the file open dialog") 
 
Exit Sub
End If
custFile.OpenFile()

End Sub




Thanks for any help

BubbaMac
BubbaMac  Sunday, October 18, 2009 9:41 PM

Hi BubbaMac!

There are many ways to display the Open File dialog in Excel.
Here are your options:


Excel.Application application;
object missing = Type.Missing;

// METHOD 1:
Microsoft.Office.Core.FileDialog fileDialog = application.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogOpen);
int cancelled = fileDialog.Show();

// Don't forget to set the Filters and AllowMultiSelect properties

if (cancelled != 0)
{
string fileOpened = fileDialog.Item;

Excel.Workbook workbook = application.Workbooks.Open(fileOpened, ...);
}

// METHOD 2:
// Don't forget to set the FileFilter and MultiSelect parameters
object ret = application.GetOpenFilename(missing, missing, missing, missing, missing);

if (ret is String)
{
string fileOpened = ret.ToString();

Excel.Workbook workbook = application.Workbooks.Open(fileOpened, ...);
}

// METHOD 3:
bool fileOpened = application.Dialogs[Excel.XlBuiltInDialog.xlDialogOpen].Show(
missing, missing, missing, missing, missing, false, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

if (fileOpened)
{
Excel.Workbook workbook = application.ActiveWorkbook;
}

Jose Anton Bautista  Monday, October 19, 2009 2:12 AM

Hello BubbaMac,

In addition to what Jose said, here is VB.NET code.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
        Dim CustFile As New OpenFileDialog
        Dim wk As Excel.Workbook
        If CustFile.ShowDialog() = DialogResult.OK Then
            wk = Globals.ThisAddIn.Application.Workbooks.Open(CustFile.FileName)
        End If
    End Sub
<<programmatically copy its contents to the current workbook.>>
Could you bespecific about its contents? sheet? If yes, you could get an answer from this thread: Copy Excel Sheet from one Workbook to Other Workbook. If not, also you could search this forum with key "copy excel" or something else.

Best regards,
Bessie

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bessie Zhao  40 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• outlook 2007 trouble...not sure if right forum?
• listing outlook contacts
• Operation Failed error: while saving outlook email from vsto
• How to get/access all the shared calendars visible in Oulook ?
• How To configure application develop by VSTO for exchange server
• Best way to migrate an Outlook 2003 folder based solution to a COM Add-in?
• Redemption equivalent objects to Outlook
• Visio 2003 : Save as web page bug
• VSTO PowerPoint 2007 code In C#
• SMS Push Outlook Addins?