none
automate word without closing the word application RRS feed

  • Question

  • i created an application that creates a whole bunch of invoices using word automation but at the end don't want to close word i would like to leave it open for the end user to modify it to his taste i just want to make sure that this will not cause problems

    this is the code is it a problem to leave it here?

      oWord = CreateObject("Word.Application")
                    oWord.Visible = True
                    oDoc = oWord.Documents.Add(path + "\invoice.dotx")
                    Dim ndate As Date = Now
                    Dim hebdate As hebrewdate.strhebrewdate
                    hebdate = ConvertDateToHebrewstring(ndate)
    
    
                    oDoc.Bookmarks.Item("edate").Range.Text = ndate.ToLongDateString
                    oDoc.Bookmarks.Item("date").Range.Text = hebdate.day + "' " + hebdate.month + " " + hebdate.year
                    oDoc.Bookmarks.Item("ename").Range.Text = memtble.Item(0).Item("ename").ToString + ControlChars.NewLine + _
                        memtble.Item(0).Item("address").ToString + ControlChars.NewLine + _
                        memtble.Item(0).Item("city").ToString + ", " + memtble.Item(0).Item("state").ToString + ", " + _
                        memtble.Item(0).Item("zip").ToString
                    oDoc.Bookmarks.Item("shul").Range.Text = shulinfo.Item(0).Item("shullname").ToString
                    oDoc.Bookmarks.Item("shuladd").Range.Text = shulinfo.Item(0).Item("shulladdress").ToString _
                        + ", " + shulinfo.Item(0).Item("shullcity").ToString _
                    + ", " + shulinfo.Item(0).Item("shullstate").ToString _
                     + ", " + shulinfo.Item(0).Item("shullzip").ToString
                    oDoc.Bookmarks.Item("name").Range.Text = " ר' " + memtble.Item(0).Item("firstname").ToString + " " + _
                    memtble.Item(0).Item("lastname").ToString
                    oDoc.Bookmarks.Item("pb").Range.Text = balance.ToString("c2")
                    oDoc.Bookmarks.Item("debit").Range.Text = decadebit.ToString("c2")
                    oDoc.Bookmarks.Item("credit").Range.Text = decacredit.ToString("c2")
                    oDoc.Bookmarks.Item("total").Range.Text = frmMemberBalance.lblBalanceAmount.Text
                    oDoc.Bookmarks.Item("year").Range.Text = stryear
                    ' Dim introws As Integer
                    frmMemberBalance.BalancedetailsBindingSource.Filter = "edate>= '" + edate + "'"
                    introws = frmMemberBalance.dgvBalanceDetails.RowCount
                    oTable = oDoc.Tables.Add(oDoc.Bookmarks("table").Range, introws + 1, 6, 9)
    
                    oTable.Rows(1).Range.Font.Bold = True
                    oTable.Cell(1, 1).Range.Text = "Name"
                    oTable.Cell(1, 2).Range.Text = "Cause"
                    oTable.Cell(1, 3).Range.Text = "Description"
                    oTable.Cell(1, 4).Range.Text = "Date"
                    oTable.Cell(1, 5).Range.Text = "Type"
                    oTable.Cell(1, 6).Range.Text = "Amount"
                    For r As Integer = 1 To introws
    
                        oTable.Cell(r + 1, 1).Range.Text = frmMemberBalance.dgvBalanceDetails.Item("balname", r - 1).Value.ToString
    
    
                        oTable.Cell(r + 1, 2).Range.Text = frmMemberBalance.dgvBalanceDetails.Item("balcause", r - 1).Value.ToString
                        oTable.Cell(r + 1, 3).Range.Text = frmMemberBalance.dgvBalanceDetails.Item("balaliya", r - 1).Value.ToString
                        Dim endate As Date
                        endate = frmMemberBalance.dgvBalanceDetails.Item("baledate", r - 1).Value.ToString
                        oTable.Cell(r + 1, 4).Range.Text = endate.ToShortDateString
    
    
                        oTable.Cell(r + 1, 5).Range.Text = frmMemberBalance.dgvBalanceDetails.Item("balaction", r - 1).Value.ToString
                        Dim amount As Decimal
                        Decimal.TryParse(frmMemberBalance.dgvBalanceDetails.Item("balamount", r - 1).Value.ToString, amount)
                        oTable.Cell(r + 1, 6).Range.Text = amount.ToString("c2")
    
                    Next r
    
                    oTable.Columns(1).Width = oWord.InchesToPoints(2)
                    oTable.Columns(2).Width = oWord.InchesToPoints(1)
                    oTable.Columns(3).Width = oWord.InchesToPoints(1.5)
                    oTable.Columns(4).Width = oWord.InchesToPoints(1)
                    oTable.Columns(5).Width = oWord.InchesToPoints(1)
                    oTable.Columns(6).Width = oWord.InchesToPoints(1.25)
    
    

    Monday, June 18, 2012 9:16 PM

Answers

  • If you don't close oDoc and don't quit oWord, the document will remain open for the user to edit.

    Regards, Hans Vogelaar

    Monday, June 18, 2012 9:55 PM

All replies

  • If you don't close oDoc and don't quit oWord, the document will remain open for the user to edit.

    Regards, Hans Vogelaar

    Monday, June 18, 2012 9:55 PM
  • If you don't close oDoc and don't quit oWord, the document will remain open for the user to edit.

    Regards, Hans Vogelaar

    Monday, June 18, 2012 9:55 PM
  • As Vogelaar says. Word interop/automation, unlike Open XML, is designed to work 'with' the user. If you don't want the screen to update, for example, you must specifically request this. Doing 'busy work' and letting the user define the detail is essentially the raison d'être for this approach.
    Tuesday, June 19, 2012 10:05 AM