PDA

View Full Version : Solved: Creating tables with Word.Application issue (II)



Mr Doubtfire
07-21-2005, 10:55 AM
My second time request for help under the same title. And I have put all togher everyting that I have collected on the first posting.
I am NOT a professional at VB6 BUT I am willing to listen and follow any ideas.
Please be patient.
The error I received is "Object variable or With block variable not set".
Please advice.
Thanks.:banghead:




Option Explicit
Dim Tbl As Table
Dim objApp As Word.Application

Private Sub Form_Load()

With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.TypeText Text:="How are you"
.Selection.TypeText Text:=vbCrLf
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With

Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=4, NumColumns:=2)
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = 20
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"

With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Selection.TypeText Text:=vbCrLf
.Selection.TypeText Text:="Thank you!"
End With
objApp.ActiveDocument.SaveAs FileName:="c:\test\test1.doc"

Set objApp = Nothing
End Sub

:banghead:

lucas
07-21-2005, 01:56 PM
Mr. Doubtfire,

I'm no word expert but this seems to be the problem at the beginning of the script. Not sure why your using a private sub to begin with but thats not the problem. I think you need to define your variables within the routine. Also define the objApp as an Object then set a value(not sure if value is the right term) for it like this.

Option Explicit
Sub Form_Load()
Dim Tbl As Table
Dim objApp As Object
Set objApp = Word.Application



I hope this helps get you going in the right direction. I'm pretty sure one of the board word coders will come along soon and help you with this.

Tommy
07-21-2005, 02:48 PM
Hi Mr. Doubtfire,

I think you have it correct except for 1 statement.


Private Sub Form_Load()

Set objApp = New Word.Application '<- this is what is missing
With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = _
wdAlignParagraphCenter
.Selection.TypeText Text:="How are you"
.Selection.TypeText Text:=vbCrLf
.Selection.ParagraphFormat.Alignment = _
wdAlignParagraphLeft
End With


Hey lucas :hi:

Tommy
07-21-2005, 03:03 PM
:doh:I was wrong, but this will work for you :) sorry about misinforming you earlier



Private Sub Form_Load()
Set objApp = New Word.application
objApp.Documents.Open _
"C:\Documents and Settings\Administrator\Desktop\test.doc"
With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = _
wdAlignParagraphCenter
.Selection.TypeText Text:="How are you"
.Selection.TypeText Text:=vbCrLf
.Selection.ParagraphFormat.Alignment = _
wdAlignParagraphLeft
End With

Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=4, NumColumns:=2)
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = 20
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"

With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Selection.TypeText Text:=vbCrLf
.Selection.TypeText Text:="Thank you!"
End With


objApp.ActiveDocument.SaveAs FileName:="c:test1.doc"
objApp.ActiveDocument.Close False
objApp.Quit False
Set objApp = Nothing
End Sub

geekgirlau
07-21-2005, 06:21 PM
Actually there is one other potential error here:


Option Explicit

Private Sub Form_Load()
Dim Tbl As Table
Dim objApp As Word.application


Set objApp = New Word.application
objApp.Documents.Open "C:\Documents and Settings\Administrator\Desktoptest.doc"

With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.TypeText Text:="How are you"
.Selection.TypeText Text:=vbCrLf
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With

Set Tbl = objApp.ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=4, NumColumns:=2)
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = 20
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"

With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Selection.TypeText Text:=vbCrLf
.Selection.TypeText Text:="Thank you!"
End With


objApp.ActiveDocument.SaveAs FileName:="c:test1.doc"
objApp.ActiveDocument.Close False
objApp.Quit False
Set objApp = Nothing
End Sub

lucas
07-21-2005, 07:38 PM
Hey Tommy,

I'm glad you and geekgirlau came along. How's the sunburn:beerchug:

Tommy
07-22-2005, 06:14 AM
I agree with qeekgirlau. I usually make a variable to hold the document and use that var to work on the doc.

I also agree with lucas, the dim statements need to be inside the sub/functions.

The following are a few suggestions, please do not take it as criticism.

1.) If you are going to make a global var place it in a module and use the public keyword, never make a public var in a form - this will load the form every time the var is accessed. 2.) Break the code into smaller subs/functions. This will help later for code reuse. ex Have a sub named OpenWordDoc to start word and open an existing doc/ open a new doc. One to create tables returning the number rows and columns and another to fill the table.

3.) Take all the code out of the form_Load. This should be to inialize vars/setup for work. Move the code to a command button click. This way you can test over and over without haveing to restart the app.

4.) Add objApp.Visible = True This will allow you to see what is going on while you work, enabling you to stop and retry when it doesn't work or something happens.

I am not trying to tell you what to do just making a few suggestions to help out.:hi:

Hey lucas :hi: sunburn is fine and now I am molting (peeling). Back to detailing built-up and angle bracing. :)

Mr Doubtfire
07-22-2005, 06:35 AM
This is what I want from this site.
Every idea/suggestion/advice is what I am expecting.
Salute and thanks to ALL!
I would test it and report whether I miss.

Mr Doubtfire
07-22-2005, 08:01 AM
I have question(s) regarding the global declaration suggestion.
Regarding
Dim Tbl As Table
Dim objApp As Word.Application
which I declare outside the Form_Load().
I need the "objApp" for the "How are you?." - Sub/Function 1
I need the "Tb1" for the table. - Sub/Function 2
I need the "objApp" for the "Thank you!..." - Sub/Function 3
How could I accomplish the above for it/them ?
Any idea/suggestion/advice is/are WELCOME!
Thanks!

Tommy
07-22-2005, 09:23 AM
Mr Doubtfire, this is how I would do it. This does not mean it is the best, or preferred, it is just my way :) .

Something that needs work is to move the curser to a new location, not having the tme now to work on it a suggestion is to look at the collapse move to end methods.

In a module


Option Explicit
Private Sub Form_Load()
End Sub
Option Explicit
Public Tbl As Table
Public objApp As Word.Application
Public WrdDoc As Word.Document
'**********************************************************
' StartWrd (SUB)
'
' PARAMETERS:
' (In/Out) - iDoc - String - value passed must have a valid
' location and full doc name
' "C:\test\test1.doc"
'
' DESCRIPTION:
'
'**********************************************************
Public Sub StartWrd(iDoc As String)
'iDoc must have the full validated path to the doc with
'the full doc name
On Error Resume Next
Set objApp = New Word.Application
DoEvents '<- I add this to give Word time to work and
'finish opening
Set WrdDoc = objApp.Documents.Open(iDoc)
' check for error and display if there is one inform
'user what happened
If Err.Number <> 0 Then
MsgBox Err.Description
Err.Clear
Else
objApp.Visible = True
End If
' turn off error checking - don't want it to show up
'someplace else
On Error GoTo 0
End Sub

'*********************************************************
' KillWrd (SUB)
'
' PARAMETERS:
' (In/Out) - iSvDoc - String - value passed must have a
' valid location and full doc name
' "C:\test\test1.doc"
'
' DESCRIPTION: Used to save a document and close word
'
'*********************************************************
Public Sub KillWrd(iSvDoc As String)
'iSvDoc must have the full validated path to the doc
'with the full doc name
WrdDoc.SaveAs iSvDoc
WrdDoc.Close False
Set WrdDoc = Nothing '<- be nice, release variable
objApp.Quit False
Set objApp = Nothing '<- be nice, release variable
End Sub

'*********************************************************
' AddText (SUB)
'
' PARAMETERS:
' (In/Out) - iTextToAdd - String - string of text to be
' added at current location of the curser
'
' DESCRIPTION: NOTE the selection must be set in advance
'
'*********************************************************
Public Sub AddText(iTextToAdd As String)
With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = _
wdAlignParagraphCenter
.Selection.TypeText Text:=iTextToAdd
.Selection.TypeText Text:=vbCrLf
.Selection.ParagraphFormat.Alignment = _
wdAlignParagraphLeft
End With
End Sub

'*********************************************************
' GenTable (SUB)
'
' PARAMETERS:
' (In/Out) - iColumns - Long -
' (In/Out) - iRows - Long -
'
' DESCRIPTION:
'
'*********************************************************
Public Sub GenTable(iColumns As Long, iRows As Long)
Dim EndOfCellMarker As String
EndOfCellMarker = Chr(13) & Chr(9)
Set Tbl = WrdDoc.Tables.Add(Range:=Selection.Range, _
NumRows:=iRows, NumColumns:=iColumns)
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = 20
'your way

Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"
End Sub





In your form


Private Sub Command1_Click()
StartWrd "C:\test1.doc"
AddText "How are you?"
GenTable 2, 4
AddText "Thank you!"
KillWrd "C:\test.doc"
End Sub



Happy coding :devil:

Mr Doubtfire
07-22-2005, 10:04 AM
:bow:
Very clear coding with explanation.
I would try it later.
Thanks for the first lesson of VB.
I am waiting for the second.................

fumei
07-22-2005, 10:11 AM
My two cents.

Tommy is correct that it is much better to have separate procedures. Having chunks, especially if commented (as his is) makes it much easier to debug things.

I would use more With statements, and include inserted text as one piece of code, rather than multiple. For example:
With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.TypeText Text:=iTextToAdd
.Selection.TypeText Text:=vbCrLf
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
could be replaced by:
Public Sub AddText(iTextToAdd As String)
With objApp.Selection
.Font.Bold = True
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeText Text:=iTextToAdd & vbcrlf
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With

Tommy's code for GenTable declares and sets string variable EndOfCellMarker. This variable is never used in the code. I also wonder if it is needed, as every cell in fact has a endofcell marker.

Tommy
07-22-2005, 10:29 AM
Gerry the EndOfCellMarker is a string that I ws going to show an alternate way to insert text into the table, instead of directly for each cell. I didn't have time to finish it so I took out part of it (my bad). I will post the code when I have time to debug.

LOL your replacement code is good! Slipped by me, sorry, I had planned on using the WrdDoc object, I aslo forgot to Set Tbl = Nothing.

I started with Word95 and VB5, so I get confused quite a bit cause MY code has a lot of documentation for problems I had with development, since it was a problem in the past I just don't go there, some may have been fixed in newer realeases.

fumei
07-22-2005, 10:40 AM
Ah...makes sense to insert text as a string. However:

1. you set EndOfCellmarker quite specifically as Chr(13) and Chr(9), not as inserted text. So I am not sure what you were trying to with the textual content to go into a cell.

1. The Chr(13) & Chr(9) is incorrect. Go to an empty cell (ie. no text in it) and select it. The only thing that will be selected is the endof cell marker. This essentially a special type of paragraph mark. I have got into a large discussion regarding this mark elsewhere. I still insist that, within the Word Object model, this IS a paragraph mark. ALL cells in a table count as individual paragraphs in Paragraphs.Count. With an empty cell, try running:
MsgBox Asc(Left(Selection.Text, 1)) & " " & Asc(Right(Selection.Text, 1))

This returns a Chr(13) and a Chr(7) - not Chr(9). The endofcell marker is a unique TWO character object that is read in the character count of a document as ONE character. The Chr(7) - or "bell" - is the "ding" sound of a typewriter when it reaches the end of its carriage.

Mr Doubtfire
07-22-2005, 10:57 AM
After inserting "444" (last row data of table), do I need to move the cursor to the next line before "AddText" for "Thank you"?
Thanks.

Tommy
07-22-2005, 12:04 PM
Gerry I picked up on that after the post, thanks, I remember it was in that range of numbers somewhere :).
My idea was to make a string with the chr(13) & chr(7) to tell word that this is in 1 cell the next cell so forth and so on. After all when you say


Dim sData as String
sData = ActiveDocument.Tabels(1).Range.Text


sData contains all the text with the chr(13) & chr(7) for each cell and once again for each end of row :yes. So I was just trying to reverse the process. It looks like I can't get it to work right now :dunno

Mr Doubtfire the below sub is modified from the original post to move the curser to the end of the table.


'*********************************************************
' GenTable (SUB)
'
' PARAMETERS:
' (In/Out) - iColumns - Long -
' (In/Out) - iRows - Long -
'
' DESCRIPTION: this sub will generate a table based on the
' number of rows and columns fill the first 2 columns
' with data move the curser to the end of the table
' and move 1 more line.
'
'*********************************************************
Public Sub GenTable(iColumns As Long, iRows As Long)
Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
NumRows:=iRows, NumColumns:=iColumns)
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = 20
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"
Set Tbl = Nothing '<- be nice, release variable
objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 '<-
'move to end of table
objApp.Selection.MoveDown Unit:=wdLine, Count:=1, _
Extend:=wdMove '<- move foward 1 line
End Sub

Mr Doubtfire
07-23-2005, 07:17 PM
I have a question first before reporting my problem.
What is the difference between
Set objApp = New Word.Application
and
Set objApp = createobject("Word.Application ")
?

I am running under XP platform but the folder(s) involved have the permissions (Full control) to Everyone and logged under Administrator. (therefore it is NOT a permission issue and please note this setting is just for testing ONLY)

I got the error message "This file could not be found...............(C:\test.doc)"
I ran the following code.

Option Explicit
Public Tbl As Table
Public objApp As Word.Application
Public WrdDoc As Word.Document
'************************************************************************** *****
' StartWrd (SUB)
'
' PARAMETERS:
' (In/Out) - iDoc - String - value passed must have a valid location and full doc name
' "C:\test\test1.doc"
'
' DESCRIPTION:
'
'************************************************************************** *****


Private Sub Form_Load()
StartWrd "C:\test.doc"
AddText "How are you?"
GenTable 2, 4
AddText "Thank you!"
KillWrd "C:\test.doc"


End Sub
Public Sub StartWrd(iDoc As String)
'iDoc must have the full validated path to the doc with the full doc name
On Error Resume Next
Set objApp = New Word.Application
DoEvents '<- I add this to give Word time to work and finish opening
Set WrdDoc = objApp.Documents.Open(iDoc)
' check for error and display if there is one inform user what happened
If Err.Number <> 0 Then
MsgBox Err.Description
Err.Clear
Else
objApp.Visible = True
End If
' turn off error checking - don't want it to show up someplace else
On Error GoTo 0
End Sub

'************************************************************************** *****
' KillWrd (SUB)
'
' PARAMETERS:
' (In/Out) - iSvDoc - String - value passed must have a valid location and full doc name
' "C:\test\test1.doc"
'
' DESCRIPTION: Used to save a document and close word
'
'************************************************************************** *****
Public Sub KillWrd(iSvDoc As String)
'iSvDoc must have the full validated path to the doc with the full doc name
WrdDoc.SaveAs iSvDoc
WrdDoc.Close False
Set WrdDoc = Nothing '<- be nice, release variable
objApp.Quit False
Set objApp = Nothing '<- be nice, release variable
End Sub

'************************************************************************** *****
' AddText (SUB)
'
' PARAMETERS:
' (In/Out) - iTextToAdd - String - string of text to be added at current location of the curser
'
' DESCRIPTION: NOTE the selection must be set in advance
'
'************************************************************************** *****
Public Sub AddText(iTextToAdd As String)
With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.TypeText Text:=iTextToAdd
.Selection.TypeText Text:=vbCrLf
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
End Sub

'************************************************************************** *****
' GenTable (SUB)
'
' PARAMETERS:
' (In/Out) - iColumns - Long -
' (In/Out) - iRows - Long -
'
' DESCRIPTION: this sub will generate a table based on the number of rows and columns
' fill the first 2 columns with data move the curser to the end of the table and move 1 more line.
'
'************************************************************************** *****
Public Sub GenTable(iColumns As Long, iRows As Long)
Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, NumRows:=iRows, NumColumns:=iColumns)
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = 20
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"
Set Tbl = Nothing '<- be nice, release variable
objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 '<- move to end of table
objApp.Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove '<- move foward 1 line
End Sub



Thanks and please advice.

xCav8r
07-23-2005, 07:42 PM
I haven't looked at your code, but I thought I'd answer your first question. There is a difference between the two methods, but they both essentially create a new instance of Word. For what you're trying to do here, I wouldn't worry about the difference. If you're still curious, this article at the MSDN might be helpful: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconhowobjectcreationworksinvbcomponents.asp

Edit: On second thought, I was too brief. It might be better to stick with the CreateObject since that works in all circumstances (early or late bound objects) while New doesn't (early bound objects only). In other words, you'd have to have a reference to the library in your project to use New (that's already here by default since you're using the Word library), but you wouldn't have to worry about the reference if you used CreateObject (good when working from another application). Another advantage of CreateObject is your code would work with different versions of Word.

I'm not sure how understandable that explanation was. If you want to discuss this in more detail, we should start a new thread for it, because I think it will be of interest to others as well.

fumei
07-24-2005, 02:10 AM
Tommy, I am not sure what you mean by "Data contains all the text with the chr(13) & chr(7) for each cell and once again for each end of row" .

Please explain your statement "one again". The fact is:

sData = ActiveDocument.Tabels(1).Range.Text means precisiely this:

sData = all text between the Long Integer calculated to be the CURRENT Range.Start of the table, and the Long Integer as the Range.End of the table. Period. There is NO determination of the number of paragraph associated with the text assigned by the instruction. There is no review nor calculation of endofcell, OR endofRow, The VALUE is all text between 41,234 and 41,966- for example. The Range.Start has a specific calculated Long, as does the .End. SData = all characters between those numbers. It means no more, but no less.

Mr Doubtfire
07-24-2005, 03:23 PM
I have some questions here as followed:

1. My table has Font '10' and when I set column width setting (like 40 characters for each one)
this DOES NOT work Tbl.Columns(1).Width = 20
instead I have to do Tbl.Columns(1).Width = 300
Is it in pixels of proportional font size !?
2. How I could get rid of the table GRID lines inside the Word document ?
3. How I could test the existing of an instance of MS-Word, if YES how I could get
rid of it and make sure it is always a fresh start?
4. I have tried to put a loop for a mulitple running to get several Word documents
and found out the first time was successful, and got stuck the second time on the
line with "Set Tbl=......", although there is "Set Tbl = nothing".
Please advice.
THANKS!

Tommy
07-25-2005, 07:53 AM
Gerry,

This is what I see when working with the Range.Text in a word table.
First cell text (in this case "AAA") followed by Chr(13), Chr(7), the next cell text ("111") followed by Chr(13), Chr(7), followed by Chr(13), Chr(7), hence my once again. To test:


Public Sub GenTable(iColumns As Long, iRows As Long)
Dim mAA As String
Dim marrBB() As String
Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
NumRows:=iRows, NumColumns:=iColumns)
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = 20
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"
mAA = Tbl.Range.Text
MsgBox Asc(Mid(mAA, 4, 1)) '<- chr 13
MsgBox Asc(Mid(mAA, 5, 1)) '<- chr 7 -
'end of first cell
MsgBox Asc(Mid(mAA, 9, 1)) '<- chr 13
MsgBox Asc(Mid(mAA, 10, 1)) '<- chr 7
'- end of second cell
MsgBox Asc(Mid(mAA, 11, 1)) '<- chr 13
MsgBox Asc(Mid(mAA, 12, 1)) '<- chr 7
'- end of row ** once again
marrBB = Split(Tbl.Range.Text, Chr(13) & Chr(7))
MsgBox UBound(marrBB) '<- = 12 not 8
Set Tbl = Nothing '<- be nice, release variable
objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 '<-
'move to end of table
objApp.Selection.MoveDown Unit:=wdLine, Count:=1, _
Extend:=wdMove '<- move foward 1 line
End Sub


This happens in VB6 AND VBA. So in conclusion I agree with you, it holds ALL text between the start and the stop of the Range.Text.

Mr Doubtfire,

C:\test.doc is a file I loaded for testing, if you don't have it it will not load. I agree with what xCav8r has posted, the only thing is I had a lot of problems with createobject and getobject to create an instance of Word through VB. I got a lot of ActiveX couldn't create object, etc. When I changed it to New Application I got a lot fewer phone calls :). I also use doevents right after New Application so word will have time to load and get going, then it ready to recieve instructions. This is what I have deterined works for me best, does not mean it is the best or preferred, just works best for me.:yes

1.) - Tbl.Columns(1).Width is not characters You could use the autofit which would resize the columns to fit the data.
2.)

Sub look()
Dim A As Table
Set A = Tables.Add(Range:=Selection.Range, NumRows:=5, _
NumColumns:=5)
A.Columns(1).Width = InchesToPoints(1.25)
With A.Borders
.InsideLineStyle = wdLineStyleNone
.OutsideLineStyle = wdLineStyleNone
End With
End Sub


3.) use getobject if an error occurs use create object if there is no error close activedocument - here could be a problem you have no idea if the doc needs to be saved.

4.)

Private Sub Command1_Click()
StartWrd "C:\test1.doc"
AddText "How are you?"
GenTable 2, 4
AddText Chr(13)
GenTable 6, 6
AddText Chr(13)
GenTable 8, 8
AddText "Thank you!"
KillWrd "C:\test.doc"
End Sub


HTH

Mr Doubtfire
07-25-2005, 06:33 PM
Thank you again for the suggestion and sample code.
With NO luck the following code does not work on my machine

A.Columns(1).Width = InchesToPoints(1.25)
With A.Borders
.InsideLineStyle = wdLineStyleNone
.OutsideLineStyle = wdLineStyleNone
End With


I have also tried
A.Borders.Enable = False
With A.Range.Borders
.InsideLineStyle = wdLineStyleNone
.OutsideLineStyle = wdLineStyleNone
End With
Neither of them work.
Please advice.
Thanks ALL! Especially those with detailed explanation.

fumei
07-25-2005, 07:01 PM
Tommy, I am not sure what you are trying to do with those msgbox instructions. What ARE you doing?

Mr. Doubtfire - please be specific. "Does not work" is not helpful.

Do you get an error message?
Nothing at all happens?

I think it is a good idea to split up instruction into separate subs, but in this case the instructions are separated out, IMHO incorrectly.

The code has the creation of the table and insertion of text, THEN a separate sub for format of the table. IMHO format follows creation. In other words, you use the table object, RIGHT AFTER it is created, to do your format.

Public Sub GenTable(iColumns As Long, iRows As Long)

Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
NumRows:=iRows, NumColumns:=iColumns)
With Tbl.Range.Borders
.InsideLineStyle = wdLineStyleNone
.OutsideLineStyle = wdLineStyleNone
End With

Mr Doubtfire
07-26-2005, 05:51 AM
Sorry for NOT being clear.
1. A.Columns(1).Width = InchesToPoints(1.25)
This DOES not adjust to Inches from pixels.
2. With Tbl.Range.Borders
.InsideLineStyle = wdLineStyleNone
.OutsideLineStyle = wdLineStyleNone
End With
This DOES not eliminate the "grid"/"border".
Thanks and please advice.

fumei
07-26-2005, 07:39 AM
Does your code make the table? If you put the format instructions right after it SHOULD remove the border. It does for me. Are you sure it doesn't? Sometimes, depending on what view you are looking with, it is hard to see that the borders are gone.

However, if you are sure it does NOT work, please post the ENTIRE code you are working with.

Tommy
07-26-2005, 07:42 AM
Gerry I agree with the instructions not being broken out correctly I was just posting a sample of how to do it. So to correct that:


Public Sub GenTable(iColumns As Long, iRows As Long)
Dim mAA As String
Dim marrBB() As String
Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
NumRows:=iRows, NumColumns:=iColumns)
With Tbl.Borders
.OutsideLineStyle = wdLineStyleNone
.InsideLineStyle = wdLineStyleNone
End With
Tbl.Range.Font.Bold = True
Tbl.Columns(1).Width = InchesToPoints(1.25)
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"
Tbl.Cell(2, 1).Range.Text = "BBB"
Tbl.Cell(2, 2).Range.Text = "222"
Tbl.Cell(3, 1).Range.Text = "CCC"
Tbl.Cell(3, 2).Range.Text = "333"
Tbl.Cell(4, 1).Range.Text = "DDD"
Tbl.Cell(4, 2).Range.Text = "444"
Set Tbl = Nothing '<- be nice, release variable
objApp.Selection.MoveEnd Unit:=wdTable, Count:=1
'<- move to end of table
objApp.Selection.MoveDown Unit:=wdLine, Count:=2, _
Extend:=wdMove '<- move foward 1 line
End Sub




Also Gerry I changed Tbl.Range.Borders to Tbl.Borders because the columns did not have the borders removed, I don't understand why but, I have fun figuring it out :yes.

fumei
07-27-2005, 07:07 AM
Yo! Would people PLEASE (pretty please) use the underscore character to break their code lines?

Putting long instruction lines causes the code window to expand and forces people to scroll left/right AND up down. This is a real pain sometimes. Further, using the underscore can make the code much easier to read.

Tommy, for example:

Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
NumRows:=iRows, NumColumns:=iColumns)

keeps the instruction line within the boundary of the code window.

Not that I am picking on Tommy. It is an easy thing to do, and would be greatly appreciated. It will make it better for you too!

Mr Doubtfire
07-27-2005, 07:14 AM
I found out the "grid" lines do not show up on printing/printing view.
Thanks!

Tommy
07-27-2005, 07:26 AM
I'll try to remember to do that. I have a 19" monitor at a res of 1600x1200 so I forget, sorry. I don't mind being picked on, I can handle it :), like you said it's an easy thing to do. :rofl:

fumei
07-27-2005, 07:36 AM
Yeah...it is those huge monitors with high-rez that makes people forget that.....well, gosh....not all of us HAVE those.

me, me, me it is all about me

I have a 19" as well, but I could not have it at 1600 x 1200. My tired old eyes can not see text at that rez. And I do not mean I can not READ it...I can not SEE it.....just kidding.

Thanks Tommy, I appreciate it.

Tommy
07-27-2005, 07:48 AM
NP I code FORTRAN in the dos screen (640x480 normally), do Acad work, and then goto VB, if it wasn't for my reading spec's I couldn't see anything. I've been told that if you change the tab spacing from 4 to 2 it is easier also but I quite frankly can't read that even with my readers. If I forget in the future remind me and I'll fix it. :)

Mr Doubtfire
07-27-2005, 12:59 PM
Could anyone tell me how to add a
1. page number
2. footnote

at bottom of each page for the Word document?
Thanks!

MOS MASTER
07-27-2005, 03:42 PM
I found out the "grid" lines do not show up on printing/printing view.
Thanks!

In Word goto Table | Hide / Show gridlines to toggle that grid! :yes

MOS MASTER
07-27-2005, 03:48 PM
Could anyone tell me how to add a
1. page number
2. footnote

at bottom of each page for the Word document?
Thanks!

As long as you leave the Header Footers alone you only have to set one Footer and the rest will follow:
Sub InsertPageNumber()
Dim oRange As Word.Range
Set oRange = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range

With oRange
.Fields.Add Range:=oRange, Type:=wdFieldPage
End With

Set oRange = Nothing
End Sub


I didn't add the footnote cause that's a strange request! Normally those go at the bottom of the page in there own space so why do you want them in the footer?

HTH, :whistle:

Mr Doubtfire
07-27-2005, 04:56 PM
Thanks!
For example, I would like to add "myweb.com" at the page number at the left bottom and "mywebsite.com" at the right bottom of every page of my Word document.
That's what I want to achieve.
Like this
Page xx please visit mywebsite.com

Please advice.

scratch
10-12-2005, 08:18 AM
Sorry for NOT being clear.
1. A.Columns(1).Width = InchesToPoints(1.25)
This DOES not adjust to Inches from pixels.
2. With Tbl.Range.Borders
.InsideLineStyle = wdLineStyleNone
.OutsideLineStyle = wdLineStyleNone
End With
This DOES not eliminate the "grid"/"border".
Thanks and please advice.

This will remove the gridlines. (The same as the Hide/Show gridlines on the Tools Menu).
objApp.ActiveWindow.View.TableGridlines = False

MOS MASTER
10-13-2005, 01:52 PM
This will remove the gridlines. (The same as the Hide/Show gridlines on the Tools Menu).
objApp.ActiveWindow.View.TableGridlines = False

A warning about this setting.

This is a setting that is not retained switching to another client. Meaning if someone else has gridlines to show then he wil see the grid.

If you want to make sure no one sees the grid make sure you format the table borders to white. :whistle:

Mr Doubtfire
10-13-2005, 02:30 PM
Thanks again, guys for the knowledge.

MOS MASTER
10-13-2005, 02:32 PM
Hi, :hi:

Glad to see it worked out for you! :yes