打开一个特定的单词.doc C# [英] Opening a specific word .doc C#

查看:63
本文介绍了打开一个特定的单词.doc C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我目前有一个程序将一系列单词文档合并为一个合并文档,然后将其保存在用户通过FolderBrowserDialog选择的位置。选择所需的文件和位置后,将按下合并按钮以显示消息框,指示文件已成功合并。



所需功能:

理想情况下,我希望程序能够启动创建/合并后的文档成功合并。



问题:

我遇到的问题是word文档的输出/保存位置已创建将根据用户的选择而有所不同,因为这样使用代码如:

Hello, I currently have a program that merges a selection of word documents into 1 combined document which is then saved at a location selected by the user via a 'FolderBrowserDialog'. Once the desired files and location are selected, a merge button is pressed to display a messagebox indicating the files have been successfully combined.

Desired Feature:
Ideally I would love the program to launch the document created / combined once it is successfully merged.

Issue:
the issue i am having is that the output / save location of the word document created will vary depending on user selection, because of this using code such as;

Document doc = Word.Documents.Open( @"C:\File.docx", ReadOnly: false, Visible: false );

不起作用,因为此示例中的位置是静态的。我面临的另一个因素是我正在使用的文件命名结构是;

will not work as the location in this example is static. An another factor i am facing is that the file naming structure i am using is;

string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".docx";



所以在尝试查找文档时,时间/日期可能是一个因素?我很好奇有关于打开一个单词的功能。 doc将启动最近创建/添加,允许更改文件位置和时间/日期。



使用的代码摘要:


so the time / date may be a factor when trying to locate the document? Im curious is there is a feature regarding opening a word. doc that will launch a "recently created / added" which will allow for the changing file locations and time / dates.

Summary of Code Used:

 // Input Destination - for Folder
private string[] sourceFiles;
string selectedFolder = null;

private void browseButton_Click(object sender, EventArgs e)
 {
    FolderBrowserDialog diagBrowser = new FolderBrowserDialog();
    diagBrowser.Description = "Select a folder which contains files...";

// Default folder, altered when the user selects folder of choice 
    diagBrowser.SelectedPath = selectedFolder;

// initial file path display
    folderPath.Text = diagBrowser.SelectedPath;

            // 'OK' button being confirmed on the popup menu
            if (DialogResult.OK == diagBrowser.ShowDialog())
            {
                // Grab the folder that was chosen
                selectedFolder = diagBrowser.SelectedPath;
                folderPath.Text = diagBrowser.SelectedPath;
            }
        }

 // Output Destination - for Folder
string outputFolder = null;
 
private void browseButtonOut_Click(object sender, EventArgs e)
   {
       FolderBrowserDialog diagBrowserOutput = new FolderBrowserDialog();
       diagBrowserOutput.Description = "Select a folder location to save the document...";

       // Default folder, altered when the user selects folder of choice 
       diagBrowserOutput.SelectedPath = outputFolder;

        // Output file path display
        outputPath.Text = diagBrowserOutput.SelectedPath;

            // 'OK' button being confirmed on the popup menu
            if (DialogResult.OK == diagBrowserOutput.ShowDialog())
            {
                // Grab the folder that was chosen
                outputFolder = diagBrowserOutput.SelectedPath;
                outputPath.Text = diagBrowserOutput.SelectedPath;
            }
        }

private void combineButton_Click(object sender, EventArgs e)
{ 
string fileDate = DateTime.Now.ToString("dd-MM-yy");
string fileTime = DateTime.Now.ToString("HH.mm.ss");
string outcomeFolder = outputFolder;
string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".docx";
string outputFileName = Path.Combine(outcomeFolder, outputFile);

// combines the file name, output path selected and the yes / no for pagebreaks. 
MsWord.Merge(sourceFiles, outputFileName, pageBreaker);
MessageBox.Show("A total of " + sourceFiles.Length.ToString()
+ " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}





任何有关此尝试成功的建议或有关我的代码的其他问题我都乐意提供。谢谢!



我尝试过:



如上所述,我已经使用特定的文件路径(c:// xx / xxx)等成功打开了测试单词doc。但是我无法操纵它来打开文档。当文件路径是用户选择而不是静态位置时



any advice to succeed with this attempt or further questions regarding my code i will happy to provide. thank you!

What I have tried:

As mentioned above, i've successfully opened a test word doc using specific file path (c://xx/xxx) etc. however I couldn't manipulate this to open a doc. when file paths were user selected and not in static locations

推荐答案

感谢您的建议,在一些愚弄想法之后我终于得到了我想要的结果。 />
Thank you for the advice, after some fooling around with ideas i finally got the outcome i desired.
// File Combination / User selection:
string outcomeFolder = outputFolder;
string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".docx";
string outputFileName = Path.Combine(outcomeFolder, outputFile); 



如前所述,用户将通过FileBrowserDialog选择输出文件,然后这将成为outputFolder。如图所示,outputFile是创建的doc的名称+时间/日期,它将与选择的文件路径组合以创建outputFileName。在同一个'merge'按钮中,结合了outputFolder和outputFile,然后我添加了这个:




As previously mentioned, the user would select their output file via the FileBrowserDialog, this would then become the outputFolder. The outputFile as shown is the name of the created doc + a time / date which would combine with the filepath selected to create the outputFileName. Within the same 'merge' button that combines both the outputFolder and outputFile, i then added this:

// Launches new office to Display document created:
var officeApp = new Microsoft.Office.Interop.Word.Application();
Document documentTest = officeApp.Documents.Open(outputFileName);
officeApp.Visible = true;





这会创建一个新单词应用程序并显示outputFileName形成。我现在正在实施一个可选的复选框,询问用户是否希望查看该文档。一旦文件合并。谢谢大家的建议。我很高兴看到我走在正确的轨道上。



this creates a new word application and displays the outputFileName formed. i am in the process now of implementing an optional check box to ask the user whether they wish to see the doc. once the files have been combined. Thank you all for your suggestions. I am glad to see i was on the right track.


这篇关于打开一个特定的单词.doc C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆