This question has been seen on the Office Development Forum in Chinese and English MSDN (and I have provided the answer). It seems to be an "international demand. Record and share with you today.
The story is as follows: a developer has a WinForm application that reads a record from the database and obtains a record ID. Then, you need to open Word or Excel, use the prepared VSTO document or plug-in to generate the corresponding Office document content based on the Record ID. The question is, how can I pass this ID to Word or Excel?
There is a very simple implementation method-directly add parameters in the command line that starts Word/Excel! For example:
<Excel.exe>/r "D: \ ExcelWorkbook1.xlsx"/e/id: 1234
<Excel.exe> is the complete path of Excel.exe.
/R is followed by the complete path of the VSTO Excel document.
/E tells Excel not to open a blank new document.
/Id: 1234 is the custom parameter. You can input your id number here. Of course, the parameter name can also be other, not necessarily "id ".
Step 2: In the VSTO Excel document, you can use the following code to enumerate command line parameters:
For Each a As String In Environment. GetCommandLineArgs
MsgBox ()
Next
If the code above is executed, all command line parameters are displayed.
You only need to find the parameter starting with/id: and retrieve the number.
You can also use a similar method for Word, except that parameters such as/r/e must be replaced with corresponding Word parameters.
This method is applicable to Word 2007/Excel 2007, but I cannot guarantee whether it can be used in future versions. Especially if the custom parameters you added are accidentally used as standard parameter names by the new version ...... So it is better to have a unique parameter name.