-
Jan 21st, 2018, 06:20 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Word Automation: Inserting text and then pasting a copied table.
The following is meant to copy and paste the last table in a document, and also place some text between the old end and the next table. 1) It fails to achieve this, instead placing the text at the end after the pasted table. 2) The pasted table also hits against the last one. I think solving the first problem will fix the second. With any suggestion can you please fit them into my code rather than suggest a completely different code? TIA
Code:
Private Sub cmdDoIt_Click()
Dim logSheet As Word.Document
Dim TableCount As Integer
Set logSheet = wrdApp.Documents.Open(bPath & "LogTest.docx")
TableCount = ActiveDocument.Tables.Count 'finds how many tables there are
MsgBox (TableCount) '**OK to here $$$$ remove later
ActiveDocument.Tables(TableCount).Select 'selects the last one and copies it
logSheet.ActiveWindow.ActivePane.Selection.Copy
ActiveDocument.Range.InsertAfter Chr(13) & "Testing" & Chr(13) '$$$ this was meant to come before copied table
ActiveWindow.ActivePane.Selection.Paste '$$$ pastes on end with NO gap. I need "Testing" first, and then copy of table
ActiveDocument.Close
wrdApp.Quit
Set logSheet = Nothing
Set wrdApp = Nothing
End Sub
-
Jan 21st, 2018, 08:35 PM
#2
Re: Word Automation: Inserting text and then pasting a copied table.
Just quickly thinking about it, your tables are objects in that word document. You can reference them by either name or by ordinal number. I believe they're ordered from 1 to Count. If you originally inserted them in order, the last table will be the table with "Count" as its ordinal number.
Now, you could select it (the table) that way (by number), and then "move" the cursor one line up, and you should be sitting just before that table. Then, you could do your insert/paste of your text.
Again, just a quick idea.
Good Luck,
Elroy
EDIT1: Also, something else I do, especially from the VB6 IDE, is put a STOP where I start the Word application, get it started, and then, in the Immediate window, set its Visible property to TRUE. And then, I can step through the code, one-line-at-a-time and watch what it's doing to Word. Now, I almost always work on multi-monitor systems, which make this much easier, but it could be done on a single-monitor system too.
Last edited by Elroy; Jan 21st, 2018 at 08:38 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 22nd, 2018, 01:00 AM
#3
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
Originally Posted by Elroy
Now, you could select it (the table) that way (by number), and then "move" the cursor one line up, and you should be sitting just before that table. Then, you could do your insert/paste of your text
.
As ever, I must have explained myself badly. What I am doing now is copying the last table in the document to the end of it. What I want to do, is place a line or two of text after the original last table and then copy the last table onto the document.
-
Jan 22nd, 2018, 05:46 AM
#4
Re: Word Automation: Inserting text and then pasting a copied table.
do stop working with selection, activedocument, or active anything else
this is tested to work as you have requested
Code:
logSheet.Range.InsertAfter vbnewlinw & "This is another line" & vbNewLine
Set r = logSheet.Range
r.Start = logSheet.Range.End
r.Paste
if the table is to be pasted to a different document, set a reference to a 2nd document object
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 22nd, 2018, 09:31 AM
#5
Re: Word Automation: Inserting text and then pasting a copied table.
@westconn1: As best as I can tell, he's testing things from the Word VBA and then bringing them over to VB6. However, I'm also remembering that he's using early binding. I always use late binding with my Word automation, so I never have those Active... objects.
@el84: But, in any event, westconn1 is correct. To keep track of what you're doing, it's always advisable to create object variables for everything you need. And then, once they're set, there will be no doubt about what you're working with. When recording a macro from Word's VBA, it will often use these Active... objects. However, even in the VBA, they can be replaced with declared object variables.
Good Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 22nd, 2018, 11:17 AM
#6
Re: Word Automation: Inserting text and then pasting a copied table.
@el84 - It seems as though most of your previous posts have been about office automation; are you trying to do this via VB6 or VBA?
-
Jan 22nd, 2018, 01:34 PM
#7
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
Originally Posted by westconn1
do stop working with selection, activedocument, or active anything else
this is tested to work as you have requested
Code:
logSheet.Range.InsertAfter vbnewlinw & "This is another line" & vbNewLine
Set r = logSheet.Range
r.Start = logSheet.Range.End
r.Paste
if the table is to be pasted to a different document, set a reference to a 2nd document object
Thank you for this but it raises some questions for me.
First, I wonder why you are so adamant that I must stop working with selection etc... I am keen to learn
Second, where does this fit in with my code above? Presumably the lines you have posted will not work on their own? Seems to me that there are two things I need to 'tell' the program. One is the text I want to insert. I get that as "This is another line", but the other is where to insert it. Is that vbnewlinw? (or is that meant to be vbnewline?) And if so, is that a arbitrarily named variable or object and should it be defined or set? Or is it a keyword?
There are two things my program must do:
One is copy and paste the last table in an existing document. I am using selection for that (and it works). Do I abandon that? And if so, what is used in its place?
The second thing it must do is place some specified text in between the old last table and the additional table which is a copy of that original last one. So that introduces another query. The text to be inserted comes after the exiting last table in the original document (and now before the added one) rather than text.
-
Jan 22nd, 2018, 03:30 PM
#8
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
Originally Posted by dday9
@el84 - It seems as though most of your previous posts have been about office automation; are you trying to do this via VB6 or VBA?
Simply, I don't know. I am trying fragments of code revealed in a search or via MSDN
-
Jan 22nd, 2018, 03:36 PM
#9
Re: Word Automation: Inserting text and then pasting a copied table.
VBA is the code interface in office applications such as excel
vb6 is a stand alone programming language to create executable files
a lot of times the same code will run in both, but not always, so it is better if we know what you are coding in
most questions for code for in word or excel etc are best asked i office development
or is that meant to be vbnewline?
yes typo, it a keyword, VB constant, similar to vbcrlf
the code i posted should be from after copying the last table of the document, which you arleady had working
see elroys reply regarding working with active objects
Last edited by westconn1; Jan 22nd, 2018 at 03:41 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 22nd, 2018, 03:40 PM
#10
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
Thank you. Could you reply to my post #7 Sorry to be a pain
-
Jan 22nd, 2018, 06:32 PM
#11
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
Originally Posted by westconn1
VBA is the code interface in office applications such as excel
vb6 is a stand alone programming language to create executable files
Yes, got that
yes typo, it a keyword, VB constant, similar to vbcrlf
I think you may have edited that after the original post? Didn't see it before... Clears that up nicely
the code i posted should be from after copying the last table of the document, which you arleady had working
What is happening is that the additional words are being placed at the end of the document, ie *after* the table has been copied. I wanted them between the original last table and the new one. I've got rid of the Active Document when counting the tables. I now have
Code:
Set logSheet = wrdApp.Documents.Open(bPath & "LogTest.docx")
TableCount = logSheet.Tables.Count 'finds how many tables there are
MsgBox (TableCount) '**OK to here $$$$ remove later
logSheet.Range.InsertAfter Chr(13) & "Testing" & Chr(13) '$$$ this was meant to come before copied table
ActiveDocument.Tables(TableCount).Select 'selects the last one and copies it
logSheet.ActiveWindow.ActivePane.Selection.Copy
ActiveWindow.ActivePane.Selection.Paste '$$$ pastes on end with NO gap. I need "Testing" first, and then copy of table
-
Jan 23rd, 2018, 02:39 AM
#12
Re: Word Automation: Inserting text and then pasting a copied table.
for your whole code, i would probably do like
Code:
dim r as range 'or in vb6, As word.range or As Object
with wrdApp.Documents.Open(bPath & "LogTest.docx")
.tables(.tables.count).copy
.Range.InsertAfter vbnewlinw & "This is another line" & vbNewLine
Set r = .Range
r.Start = .Range.End
r.Paste
.close true ' save changes
end with
wrdapp.quit
while this is untested, i tested before posting in #4 that the table was pasted below the additional line of text
using the
Set logSheet = wrdApp.Documents.Open(bPath & "LogTest.docx")
is really much better than you had before, but in this instance does not seem to be required
most people in these forums prefer to help other learn, rather than doing it all for them, but it is hard to give a complete tutorial to the use of VB and good code practices (even if we could agree on what is good practice), i try to answer questions as asked, then allowing that to be fitted to existing code, rather than saying it is all wrong, start again (as some people do), even if some of the code is not good (it will improve with practice and more coding), if i think it should work i leave it alone
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 23rd, 2018, 02:06 PM
#13
Re: Word Automation: Inserting text and then pasting a copied table.
Originally Posted by westconn1
most people in these forums prefer to help other learn, rather than doing it all for them, but it is hard to give a complete tutorial to the use of VB and good code practices (even if we could agree on what is good practice), i try to answer questions as asked, then allowing that to be fitted to existing code, rather than saying it is all wrong, start again (as some people do), even if some of the code is not good (it will improve with practice and more coding), if i think it should work i leave it alone
Nicely said.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 23rd, 2018, 03:22 PM
#14
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
Thanks for your efforts. I sense some impatience. I didn't want you to 'do it all' for me, by any means. The very opposite. I was hoping you would show me where your proposed change would fit into my code, rather than throwing *all* of mine away (I have learned that with code there are lots of ways to achieve an end). I am still confused by some of what you sent me. But I will try to fit it in myself. I imagine vbnewlinw is still a typo? Also confused by comment on first line. This is vb6 (always has been) so should it be As word.range or As Object? Does this code go before my existing copy and paste? What existing code should I delete? The problem there was that the additional text was being added *after* the copied table. I wanted it between,. Does this solve that? Do I need any new Sets or Dims? Do I need to remove any of mine? I'll quite understand if you throw your hands in the air with frustration.
Last edited by el84; Jan 23rd, 2018 at 03:30 PM.
-
Jan 23rd, 2018, 03:44 PM
#15
Re: Word Automation: Inserting text and then pasting a copied table.
not in this thread
so should it be As word.range or As Object?
the first for early binding, the latter for late binding
I imagine vbnewlinw is still a typo?
of course, just copied fro previous
did you test it?
Do I need to remove any of mine?
the code in post #12 is an example that would replace your entire code posted in #1, the only variable used within that code is r, and wrdapp which presumably is dimensioned elsewhere as it is already an object of word
the code sample as in post #4 was to work with you existing code, from after you had selected and copied the table, to paste to the correct location, this was tested to work as requested
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 23rd, 2018, 07:34 PM
#16
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
OK. Have put in that code, with Dim r As Word.Range
The line you have given,
Code:
.tables(.tables.count).copy
fails. I noticed in Intellisense that .copy was not listed as an option.
I assume that you want me to Set r = Nothing at end?
-
Jan 24th, 2018, 02:21 AM
#17
Re: Word Automation: Inserting text and then pasting a copied table.
I assume that you want me to Set r = Nothing at end?
while it is good practice, vb6 is good at disposing of out of scope variable
try
Code:
.tables(.tables.count).range.copy
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 24th, 2018, 02:58 PM
#18
Thread Starter
Frenzied Member
Re: Word Automation: Inserting text and then pasting a copied table.
Thanks again. That worked fine. Here is my finished version with slight changes, such as having the user enter what title they require. All just as I wanted.
EDIT: One final problem!
Code:
'TableTest
'PURPOSE: test use of word application in selecting/copying tables in Word document
'Project > References > Microsoft Word 15.0 Object Library
Option Explicit
Dim wrdApp As Word.Application
Dim bPath As String
Dim newTitle As String
Private Sub Form_Load()
lblVersion.Caption = "Version 7 25/1/18"
bPath = "C:\Users\Alan\Documents\aaTest\"
Set wrdApp = CreateObject("word.application")
End Sub
Private Sub cmdDoIt_Click()
newTitle = txtNewTitle.Text
Dim MyRange As Word.Range
With wrdApp.Documents.Open(bPath & "LogTest.docx")
.Tables(.Tables.Count).Range.Copy
.Range.InsertAfter vbNewLine & newTitle & vbNewLine
Set MyRange = .Range
MyRange.Start = .Range.End
MyRange.Paste
.Close True
End With
Set MyRange = Nothing
MsgBox ("Finished. Exit now if no more required...")
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
My final problem is I would like to (In a simple way) have newTitle in bold. I have experimented without success.
Last edited by el84; Jan 24th, 2018 at 06:08 PM.
-
Jan 25th, 2018, 05:18 AM
#19
Thread Starter
Frenzied Member
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
EDIT: I SPOKE TOO SOON! SEE NEXT POST! Well, that turned out to be easy it did not!. After the insert line, I simply added
Code:
.Range.Font.Bold = True
I had laboured under the impression that if Intellisense did not offer choices for True/False once the equals sign was typed, then it would not be valid syntax. Apparently I was wrong.
SEE NEXT POST!
Last edited by el84; Jan 25th, 2018 at 06:47 PM.
-
Jan 25th, 2018, 06:59 PM
#20
Thread Starter
Frenzied Member
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
Have fixed that properly now by locating just the new title and replacing it with a bold title. For anyone following along, here is the full code
Code:
'TableTest
'PURPOSE: test use of word application in selecting/copying tables in Word document
'all cells in previous table to be blanked, except title row
'insert new title, this only to be bold
'Project > References > Microsoft Word 15.0 Object Library
Option Explicit
Dim wrdApp As Word.Application
Dim bPath As String
Dim newTitle As String
Private Sub Form_Load()
lblVersion.Caption = "Version 13 26/1/18"
bPath = "C:\Users\Alan\Documents\aaTest\"
Set wrdApp = CreateObject("word.application")
End Sub
Private Sub cmdDoIt_Click()
' **new table title from textbox on form
newTitle = txtNewTitle.Text
Dim MyRange As Word.Range
With wrdApp.Documents.Open(bPath & "LogTest.docx")
' **copies previous last table in word document
.Tables(.Tables.Count).Range.Copy
'** inserts new title
.Range.InsertAfter vbNewLine & Chr(13) & newTitle & vbNewLine
' **this block finds newTitle and makes only it bold
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Replacement.Font.Bold = True
.Execute FindText:=newTitle, ReplaceWith:=newTitle, Replace:=wdReplaceAll
End With
' **pastes the copied table into the document
Set MyRange = .Range
MyRange.Start = .Range.End
MyRange.Paste
' **counts number of rows in the table
Dim RowCount As Integer
RowCount = .Tables(.Tables.Count).Rows.Count
' **blanks all cells except the first row (headers)
Dim n1, n2 As Integer
For n1 = 1 To 7 ' **each cell in row; tables in this document have 7 cells across
For n2 = 2 To RowCount ' **each row as counted (leave first (title) row alone)
.Tables(.Tables.Count).Cell(n2, n1).Select ' **if no select, delete would remove cell!
Selection.Text = "" ' **could be "" or delete
Next n2
Next n1
.Close True
End With
Set MyRange = Nothing
MsgBox ("Finished. Exit now if no more required...")
End Sub
Private Sub cmdExit_Click()
wrdApp.Quit
Unload Me
End Sub
the EL84 was a common output valve (=tube!)
-
Jan 25th, 2018, 09:47 PM
#21
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
that turned out to be easy it did not!
for bold text, this would also work
Code:
.Tables(.Tables.Count).Range.Copy
Set r = .Range
r.Start = .Range.End
r.InsertAfter vbnewline & "This is another line" & vbNewLine
r.Font.Bold = True
Set r = .Range
r.Start = .Range.End
r.Paste
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 26th, 2018, 01:44 AM
#22
Thread Starter
Frenzied Member
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
Is that right that there are two set r lines?
Thanks all !
-
Jan 26th, 2018, 01:57 AM
#23
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
yes first both set the start of r to the end of the document, which changes on inserting the text
the first in then the start of the range where the text is inserted, it then expands to hold the text, so the font of that whole range can be set to bold
the second which then moves to end of document again (probably could also replace the 2 lines with r.start = r.end, no need to set the range object) then the table is pasted there
there are i am sure other ways to do the same thing
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 26th, 2018, 02:14 AM
#24
Thread Starter
Frenzied Member
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
OK. I'll try to assimilate that, and work out how to fit that into the whole working code I posted. Presumably this means that my block of code With Selection Find... is not needed.
EDIT: I've puzzled it out. This is the changed fragment. &&& are the lines in your fragment. It works fine. However, you say 2 lines *may* not be needed. Which do you suggest?
Code:
With wrdApp.Documents.Open(bPath & "LogTest.docx")
' **copies previous last table in word document
.Tables(.Tables.Count).Range.Copy '&&& line 1 of Westconn1's suggestion
Set MyRange = .Range '&&& 2 new
MyRange.Start = .Range.End '&&& 3 new
' ** inserts new title &&& bold
MyRange.InsertAfter vbNewLine & Chr(13) & newTitle & vbNewLine '&&& 4
MyRange.Font.Bold = True '&&& 5
' **pastes the copied table into the document
Set MyRange = .Range '&&& 6
MyRange.Start = .Range.End '&&& 7
MyRange.Paste '&&& 8
...
...
End With
Last edited by el84; Jan 26th, 2018 at 04:14 AM.
Thanks all !
-
Jan 26th, 2018, 05:55 AM
#25
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
i would believe that lines 6 and 7 could be replaced, with myrange.start = myrange.end
as all we needed to do is move the start of myrange to the end of the same range, which is the same as the end of the document, as that is where we want to paste the table
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 26th, 2018, 02:34 PM
#26
Thread Starter
Frenzied Member
Re: [RESOLVED] Word Automation: Inserting text and then pasting a copied table.
OK, done that and tests fine! Thanks also for the explanation.
Thanks all !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|