Saving custom Word document using FilePicker in Windows Store app. RRS feed

  • Question

  • I am making a Windows Store application and I want to allow users that press an "Export To Word" button to have all the data that they have input into the app to be displayed in a Word document and saved to a desired location on their computer. The code below is a test segment of code that almost does what I am after, however after saving the document and opening it using Word rather than the app, it cannot open the file due to it being corrupted apparently. However when you open it in Notepad the text is displayed as I want.

    private async void exportToWord_Click(object sender, RoutedEventArgs e)
        await ExportToWord();
    private async Task ExportToWord()
    {// Create the picker object and set options
        Windows.Storage.Pickers.FileSavePicker savePicker = new Windows.Storage.Pickers.FileSavePicker();
        savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
        // Dropdown of file types the user can save the file as
        // Default file name if the user does not type one in or select a file to replace
        savePicker.SuggestedFileName = "Test";
        Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
        MessageDialog mD;
        if (file != null)
            // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
            // write to fileawait Windows.Storage.FileIO.WriteTextAsync(file, createContentsOfFile());
            // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.// Completing updates may require Windows to ask for user input.
            Windows.Storage.Provider.FileUpdateStatus updateStatus = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
            if (updateStatus == Windows.Storage.Provider.FileUpdateStatus.Complete)
                mD = newMessageDialog("Connect exported to:" + file, "Export Successful");
                mD = newMessageDialog("Could not save file. Try again""Export Unsuccessful");
            mD = newMessageDialog("Operation canceled because the file could not be found. Try again""Export Unsuccessful");
        await mD.ShowAsync();
    privatestring createContentsOfFile()
        return "Testing...";

    Any help would be appreciated.

    Sunday, November 2, 2014 2:42 PM


  • You are facing plain text, not a Word file. You should name it .txt and it will work. I'm not aware of any classes which can generate Word format, but there may be a third party component I'm not aware of.
    Sunday, November 2, 2014 3:49 PM