Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27121

Manipulating MS word. Can this be imporved?

$
0
0
Hi basically i have a ms word document that uses bookmarks. Can this be improved?

vb Code:
  1. Option Strict On
  2.  
  3. Imports Word = Microsoft.Office.Interop.Word
  4. Imports System.Runtime.InteropServices
  5.  
  6. Public Class BuildTimeSheet
  7.  
  8. #Region "Constants"
  9.     'Template Consts
  10.     Private Const TimeSheetLocation As String = "path"
  11. #End Region
  12.  
  13.     ' TODO: Check template exists
  14.     Public Sub BuildTimeSheet(ByVal newDate As List(Of String))
  15.  
  16.         Dim objWdDoc As Microsoft.Office.Interop.Word.Document
  17.         Dim objWord As Microsoft.Office.Interop.Word.Application
  18.  
  19.         objWord = CType(CreateObject("Word.Application"), Word.Application)
  20.         objWdDoc = CType(CreateObject("Word.Document"), Word.Document)
  21.  
  22.         Try
  23.             objWdDoc = objWord.Documents.Add(TimeSheetLocation)
  24.             objWord.Visible = True
  25.  
  26.             'Insert asserted text fields to correct bookmarks in template
  27.             With objWdDoc.Bookmarks
  28.                 'Add dates to word document
  29.                 .Item("MondaysDate").Range.Text = newDate(6)
  30.                 .Item("TuesdaysDate").Range.Text = newDate(5)
  31.                 .Item("WednesdaysDate").Range.Text = newDate(4)
  32.                 .Item("ThursdaysDate").Range.Text = newDate(3)
  33.                 .Item("FridaysDate").Range.Text = newDate(2)
  34.                 .Item("SaturdaysDate").Range.Text = newDate(1)
  35.                 .Item("SundaysDate").Range.Text = newDate(0)
  36.             End With
  37.  
  38.         Catch ex As COMException
  39.             MessageBox.Show(ex.Message)
  40.         Catch ex As Exception ' Swallows all exceptions
  41.             MessageBox.Show(ex.Message)
  42.         End Try
  43.     End Sub
  44. End Class

Viewing all articles
Browse latest Browse all 27121


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>