It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
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
Post edited October 28, 2009 by Aliasalpha
This question / problem has been solved by Wishboneimage
I've been working through this 'hey scripting guy' post but I can't see any difference in our code other than the names I've used
It's been a very long time since I even looked at VB script and I was never very good at it but
WordApp.Documents.Add()
set objDoc = WordApp.ActiveDocument
not entirely sure how you would fix it maybe, set objDoc = WordApp.ActiveDocument.Add() or set objDoc = WordApp.Documents.Add() and remove the other line compeletly?
As you create the document in the line above and again with one of those lines?
I'm probably way off but maybe my rant might inspire a moment of madness so you can work it out.
Unless I've fucked things up, objDoc isn't a command, its a variable to shorten the typing on the other lines. Basically it doesn't so anything other than become shorthand
Post edited October 28, 2009 by Aliasalpha
I assume the 'TypeParagrap h()' on the third chunk is just a board copy/paste typo, right ?
avatar
Namur: I assume the 'TypeParagrap h()' on the third chunk is just a board copy/paste typo, right ?

The GOG board randomly inserts spaces into strings it thinks are too long.
Yeah its a random space insertion thing here. The code itself works flawlessly aside from the odd instance of a replacement document
I just tried pulling the table creation and population out of the function in case there was some weird error calling it but it didn't help
I keep looking at "objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS " and feeling like it could be the problem but I'm not QUITE sure how
Post edited October 28, 2009 by Aliasalpha
Are you using an IDE where you can set breakpoints, or just a text editor?
Where exactly in the code does the new document get created?
Can I give myself rep? I think I've found something that seems right
"The reason you're overwriting previous tables with new ones is that you set
objRange = objDoc.Range(), which is literally "the range of the entire
document", and then you add the new table using that range. Therefore the
table replaces anything that's already in the document."
I'll have to have a tinker with this but feel free to keep suggesting things in case this doesnt work
avatar
Miaghstir: Are you using an IDE where you can set breakpoints, or just a text editor?
Where exactly in the code does the new document get created?

I'm running notepad++, I think a programmer would be mad to choose any other text editor
Post edited October 28, 2009 by Aliasalpha
Is it this line
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
with this bit: objDoc.Tables.Add
is that adding the active document in as the extra? As objDoc was set to wordApp.ActiveDocument?
Because your creating it right at the top then would that create the second one before depositing the table in the new one?
or am I way off base again and need to go away?
It looks like that line just adds the table to the document but from what I just posted, it looks like I effectively have the entire document selected and am overwriting it with the table
avatar
Ralackk: Is it this line
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
with this bit: objDoc.Tables.Add
is that adding the active document in as the extra? As objDoc was set to wordApp.ActiveDocument?
Because your creating it right at the top then would that create the second one before depositing the table in the new one?
or am I way off base again and need to go away?

The executed code would be wordApp.ActiveDocument.Tables.Add - meaning "add a new table to the active document". I think.
avatar
Aliasalpha: It looks like that line just adds the table to the document but from what I just posted, it looks like I effectively have the entire document selected and am overwriting it with the table

I can never get my head fully round this programming stuff, kind of why I never bothered after 6 months of trying. Have you got it fully funtional now then if the range thing is the problem?
edit:
avatar
Miaghstir: The executed code would be wordApp.ActiveDocument.Tables.Add - meaning "add a new table to the active document". I think.

Yea that makes sense, I'm going to stop saying things now.
Post edited October 28, 2009 by Ralackk
I would suggest trying to do a "push a button to continue" thing after each line, to figure out where the mess-up is, but I'm not well versed in vbScript, so I have no idea how that would work.
avatar
Aliasalpha: I'm running notepad++, I think a programmer would be mad to choose any other text editor

I dunno. I use UltraEdit whenever I need to do stuff outside Visual Studio or SQL Server Management Studio. I'm quite happy with it.