Posted October 28, 2009
I'm trying to build a function to take a list of available subnets for an IP address and output them as a part of a word document. This script is opening word and building the initial part of the document perfectly but as soon as I call the function to build and populate the table, it opens a new document in place of the original whereupon it populates the table perfectly, saves the docx and closes word exactly as instructed, aside from the new document.
I'm guessing the error is in the called "tableformat" function but I'm buggered if I can see where its building a new document
Code follows. This is easiest to read if pasted into in notepad++
subject = "ITC308"
studentnum = "11111111"
nwipaddy = "168.221.0.0"
Set WordApp = CreateObject ("Word.Application")
WordApp.Documents.Add()
set objDoc = WordApp.ActiveDocument
sTITLE = "Subnet output"
WordApp.Visible = True
WordApp.Caption = sTITLE
WordApp.Selection.TypeText "Subnet output" & vblf
WordApp.Selection.TypeText "Compiled on: "
WordApp.Selection.InsertDateTime
WordApp.Selection.TypeParagraph()
WordApp.Selection.Font.Bold = True
WordApp.Selection.TypeText "Table of available Subnets for IP " & nwipaddy & "." & vblf
WordApp.Selection.Font.Bold = False
Call tableformat
objDoc.SaveAs "c:\" & subject & "_" & studentnum & ".docx"
WordApp.Quit
function tableformat
Const NUMBER_OF_ROWS = 1
Const NUMBER_OF_COLUMNS = 3
Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
Set objTable = objDoc.Tables(1)
objTable.Cell(1, 1).Range.Text = "Network Address"
objTable.Cell(1, 2).Range.Text = "Host Address Range"
objTable.Cell(1, 3).Range.Text = "Broadcast Address"
objTable.Rows.Add()
objTable.Cell(2, 1).Range.Text = "168.221.0.0"
objTable.Cell(2, 2).Range.Text = "168.221.0.1 to 168.221.63.254"
objTable.Cell(2, 3).Range.Text = "168.221.63.255"
objTable.AutoFormat(1)
end function
I'm guessing the error is in the called "tableformat" function but I'm buggered if I can see where its building a new document
Code follows. This is easiest to read if pasted into in notepad++
subject = "ITC308"
studentnum = "11111111"
nwipaddy = "168.221.0.0"
Set WordApp = CreateObject ("Word.Application")
WordApp.Documents.Add()
set objDoc = WordApp.ActiveDocument
sTITLE = "Subnet output"
WordApp.Visible = True
WordApp.Caption = sTITLE
WordApp.Selection.TypeText "Subnet output" & vblf
WordApp.Selection.TypeText "Compiled on: "
WordApp.Selection.InsertDateTime
WordApp.Selection.TypeParagraph()
WordApp.Selection.Font.Bold = True
WordApp.Selection.TypeText "Table of available Subnets for IP " & nwipaddy & "." & vblf
WordApp.Selection.Font.Bold = False
Call tableformat
objDoc.SaveAs "c:\" & subject & "_" & studentnum & ".docx"
WordApp.Quit
function tableformat
Const NUMBER_OF_ROWS = 1
Const NUMBER_OF_COLUMNS = 3
Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
Set objTable = objDoc.Tables(1)
objTable.Cell(1, 1).Range.Text = "Network Address"
objTable.Cell(1, 2).Range.Text = "Host Address Range"
objTable.Cell(1, 3).Range.Text = "Broadcast Address"
objTable.Rows.Add()
objTable.Cell(2, 1).Range.Text = "168.221.0.0"
objTable.Cell(2, 2).Range.Text = "168.221.0.1 to 168.221.63.254"
objTable.Cell(2, 3).Range.Text = "168.221.63.255"
objTable.AutoFormat(1)
end function
Post edited October 28, 2009 by Aliasalpha
This question / problem has been solved by Wishbone