Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am using c sharp language in the windows form. There are different options for each student in the same column in the database as shown in the picture. I want to mark the data I received according to the information in this column to the relevant section in the word document. How should I do this using word automation?

What I have tried:

C#
            if (fieldName == "gender" Male "x")
            {
                myMergeField.Select();
                oWord.Selection.TypeText(dr["gender"].ToString());
else if 
{
(fieldName == "gender" Female "x")
        }
}
Posted
Updated 14-Feb-24 6:49am
v2

1 solution

I am not expert in C#, but in VB. But, with Word and Excel, you can use their macro recorders. You will need to modify your macro to to include interop objects in macro statements, but most part of the work will already be made with macro recorder. But word macro recorder doesn't record all mouse operations. So, most of actions in word documents must be performed from keyboard.

I founded thes pages from Microsoft

Word automation
How to automate Microsoft Word to create a new document by using Visual C# - Office | Microsoft Learn[^]

Word mail merge (Basically getting information from a database to write personalized letters)
How to automate Microsoft Word to perform Mail Merge from Visual C# - Office | Microsoft Learn[^]

Text manipulation

Programmatically insert text into Word documents | Microsoft Learn[^]
 
Share this answer
 
Comments
Member 12505620 14-Feb-24 4:50am    
It is not the thing I want.
cmarcotte 15-Feb-24 0:04am    
I understand your question this way : you go somewhere in a Word Document to write male or female. If I am wrong, please correct. But, if your document will not be modified for a while, you probably don't neeed a C# programm. Word has buil-in support for database fields.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900