Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Automating Word application in C#

Automating Word application in C#

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpcomhelptutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Noam Ben Haim
    wrote on last edited by
    #1

    Hi all, I am trying to write some small app that automates Word Application (mainly MailMerge, and sending word documents to the printer), and I have come to a problem. Using the Missing.Value as a missing parameter (which is a good thing to do in every other method of the word app) will not work as most of the methods need a ref object as a parameter. does anyone have a clue? thanks Noam I attach a small piece of code for example. this code does not compile, for the above reason. Word._Application word = new Word.ApplicationClass(); Word.Document doc = (Word.Document)word.Documents.Open(ref m_confirmationFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.MailMerge mailMerge = (Word.MailMerge)doc.MailMerge; mailMerge.CreateDataSource("Data.doc", Missing.Value, Missing.Value, "CandidateID, FirstName, LastName, Address", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Document dataDoc = (Word.Document)word.Documents.Open("Data.doc", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Table tbl = (Word.Table)((Word.Tables)dataDoc.Tables).Item(1); tbl.Cell(2, 1).Range.InsertAfter(candidateId); tbl.Cell(2, 2).Range.InsertAfter(firstName); tbl.Cell(2, 3).Range.InsertAfter(lastName); tbl.Cell(2, 4).Range.InsertAfter(address); dataDoc.Save(); dataDoc.Close(Missing.Value, Missing.Value, Missing.Value); mailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter; mailMerge.Execute(Missing.Value); doc.Close(Missing.Value, Missing.Value, Missing.Value); Noam Ben Haim Web Developer Intel noam.ben.chaim@intel.com

    P S C 3 Replies Last reply
    0
    • N Noam Ben Haim

      Hi all, I am trying to write some small app that automates Word Application (mainly MailMerge, and sending word documents to the printer), and I have come to a problem. Using the Missing.Value as a missing parameter (which is a good thing to do in every other method of the word app) will not work as most of the methods need a ref object as a parameter. does anyone have a clue? thanks Noam I attach a small piece of code for example. this code does not compile, for the above reason. Word._Application word = new Word.ApplicationClass(); Word.Document doc = (Word.Document)word.Documents.Open(ref m_confirmationFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.MailMerge mailMerge = (Word.MailMerge)doc.MailMerge; mailMerge.CreateDataSource("Data.doc", Missing.Value, Missing.Value, "CandidateID, FirstName, LastName, Address", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Document dataDoc = (Word.Document)word.Documents.Open("Data.doc", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Table tbl = (Word.Table)((Word.Tables)dataDoc.Tables).Item(1); tbl.Cell(2, 1).Range.InsertAfter(candidateId); tbl.Cell(2, 2).Range.InsertAfter(firstName); tbl.Cell(2, 3).Range.InsertAfter(lastName); tbl.Cell(2, 4).Range.InsertAfter(address); dataDoc.Save(); dataDoc.Close(Missing.Value, Missing.Value, Missing.Value); mailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter; mailMerge.Execute(Missing.Value); doc.Close(Missing.Value, Missing.Value, Missing.Value); Noam Ben Haim Web Developer Intel noam.ben.chaim@intel.com

      P Offline
      P Offline
      Paul Riley
      wrote on last edited by
      #2

      Try null? Might not work but it's a clue. Paul

      1 Reply Last reply
      0
      • N Noam Ben Haim

        Hi all, I am trying to write some small app that automates Word Application (mainly MailMerge, and sending word documents to the printer), and I have come to a problem. Using the Missing.Value as a missing parameter (which is a good thing to do in every other method of the word app) will not work as most of the methods need a ref object as a parameter. does anyone have a clue? thanks Noam I attach a small piece of code for example. this code does not compile, for the above reason. Word._Application word = new Word.ApplicationClass(); Word.Document doc = (Word.Document)word.Documents.Open(ref m_confirmationFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.MailMerge mailMerge = (Word.MailMerge)doc.MailMerge; mailMerge.CreateDataSource("Data.doc", Missing.Value, Missing.Value, "CandidateID, FirstName, LastName, Address", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Document dataDoc = (Word.Document)word.Documents.Open("Data.doc", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Table tbl = (Word.Table)((Word.Tables)dataDoc.Tables).Item(1); tbl.Cell(2, 1).Range.InsertAfter(candidateId); tbl.Cell(2, 2).Range.InsertAfter(firstName); tbl.Cell(2, 3).Range.InsertAfter(lastName); tbl.Cell(2, 4).Range.InsertAfter(address); dataDoc.Save(); dataDoc.Close(Missing.Value, Missing.Value, Missing.Value); mailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter; mailMerge.Execute(Missing.Value); doc.Close(Missing.Value, Missing.Value, Missing.Value); Noam Ben Haim Web Developer Intel noam.ben.chaim@intel.com

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        string s = @"c:\mydoc.doc";
        object so = (object) s;
        object o = null;
        Word.Document doc = word.Documents.Open(ref so,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);

        (the ref stuff is because the tlbimporter is stupid, and prefers to assume nothing, even when the type-library brings everything top-to-bottom and left-to-right). Have fun!! :cool:


        MS quote (http://www.microsoft.com/ddk) : As of September 30, 2002, the Microsoft® Windows® 2000 DDK, the Microsoft Windows 98 DDK, and the Microsoft Windows NT® 4.0 DDK will no longer be available for purchase or download on this site.

        1 Reply Last reply
        0
        • N Noam Ben Haim

          Hi all, I am trying to write some small app that automates Word Application (mainly MailMerge, and sending word documents to the printer), and I have come to a problem. Using the Missing.Value as a missing parameter (which is a good thing to do in every other method of the word app) will not work as most of the methods need a ref object as a parameter. does anyone have a clue? thanks Noam I attach a small piece of code for example. this code does not compile, for the above reason. Word._Application word = new Word.ApplicationClass(); Word.Document doc = (Word.Document)word.Documents.Open(ref m_confirmationFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.MailMerge mailMerge = (Word.MailMerge)doc.MailMerge; mailMerge.CreateDataSource("Data.doc", Missing.Value, Missing.Value, "CandidateID, FirstName, LastName, Address", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Document dataDoc = (Word.Document)word.Documents.Open("Data.doc", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Word.Table tbl = (Word.Table)((Word.Tables)dataDoc.Tables).Item(1); tbl.Cell(2, 1).Range.InsertAfter(candidateId); tbl.Cell(2, 2).Range.InsertAfter(firstName); tbl.Cell(2, 3).Range.InsertAfter(lastName); tbl.Cell(2, 4).Range.InsertAfter(address); dataDoc.Save(); dataDoc.Close(Missing.Value, Missing.Value, Missing.Value); mailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter; mailMerge.Execute(Missing.Value); doc.Close(Missing.Value, Missing.Value, Missing.Value); Noam Ben Haim Web Developer Intel noam.ben.chaim@intel.com

          C Offline
          C Offline
          Chad Smith
          wrote on last edited by
          #4

          object isVisible = false; object missing = System.Reflection.Missing.Value; Word.ApplicationClass WordApp = new Word.ApplicationClass(); WordApp.Visible = false; WordApp.ScreenUpdating = false; WordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; Word.Document aDoc = WordApp.Documents.Add(ref fileName, ref missing, ref missing, ref isVisible); aDoc.Activate(); WordApp.ActivePrinter = _DeviceName; // Office 2000 format WordApp.ActiveDocument.PrintOut(ref bool_true, ref bool_false, ref missing, ref newFileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref bool_true, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups