I am writing an app in VB to create and write to a word document.
The document will be using a template with a couple of text string tags in it, the idea being that a new document is created, the tag is found and replaced by my text.
I am nearly there, I have got the find working ok and going to the text tag, but then when I use the selection object to change the text it is referencing a previously opened document, I can't get it to refer to my document.
Here is the code, as I say, it is fine as long as mine is the only document open, otherwise the text seems to end up on one of the others. Can anyone help?
If Selection.Text = TOTAL_LOCATION_TEXT Then
Selection.TypeText "Net Total = " & FormatCurrency(prcNetTotal)
Else
MsgBox "Tag not found"
End If
Thanks
Robin
Cimperiali
September 17th, 2001, 06:48 AM
How have you referenced "mObjDocument" ?
(I think it points to default blank document instead of yours...)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
d0wn_under
September 17th, 2001, 09:49 AM
mObjDocument is created by doing the following.
Set mObjDocument = objWord.Documents.Add(prsTemplate & ".dot", False, 0, True)
Having played with it more, it appears that selected always points to the first document opened or created. I've found this when just doing general moving around the document.
So I suppose my question now should be, how do I get selected to point to my document.
Ta
Robin
Cimperiali
September 17th, 2001, 09:55 AM
after:
Set mObjDocument = objWord.Documents.Add(prsTemplate & ".dot", False, 0, True)
try:
mObjDocument.Open filePath '(=Path and name to your doc)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
d0wn_under
September 17th, 2001, 09:57 AM
I'm not opening a document, I am creating a blank one each time and I'd rather not name it as the people who are using it could end up saving over old versions if each one had the same name.
d0wn_under
September 17th, 2001, 09:59 AM
I'm not opening a document, I am creating a blank one each time and I'd rather not name it as the people who are using it could end up saving over old versions if each one had the same name.
Cimperiali
September 17th, 2001, 10:06 AM
so, your active document should be:
mObjDocument(0)
or
oword.documents(0)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
d0wn_under
September 17th, 2001, 10:13 AM
My document is mobjDocument which is an object of type word.document
Once I have created the document, if I check document.fullname I get the name of the document I have just created, however if I check selection.document.fullname I get the name of the first document opened. Thus my object is pointing to the correct place, but selection isn't and that is what I need to be correct.
Robin
Cimperiali
September 17th, 2001, 10:28 AM
'this seems to work. If it does not for you answer is:
'your template has more than a single doc.
'So check your template
'(open it and look at how many doc you have inside it)
Dim wdapp as Word.Application
set wdapp = new Word.Application
'-----
'for debug purpouse:
wdapp.Visible = true
'-----
Dim mobjdocument as Word.Document
set mobjdocument = wdapp.Documents.Add (prsTemplate & ".dot") ', false, 0, true)
mobjdocument.Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = TOTAL_LOCATION_TEXT
.Replacement.Text = ""
.Forward = true
.Wrap = wdFindContinue
.Format = false
.MatchCase = false
.MatchWholeWord = false
.MatchWildcards = false
.MatchSoundsLike = false
.MatchAllWordForms = false
End With
Selection.Find.Execute
If Selection.Text = TOTAL_LOCATION_TEXT then
Selection.TypeText "Net Total = " & FormatCurrency(prcNetTotal)
else
MsgBox "Tag not found"
End If
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
d0wn_under
September 17th, 2001, 10:40 AM
The code you have given is exactly what I have, I've checked templates and the template I am using is based on the normal template and nothing else. The first document I open can be anything, a blank doc based on normal or a complex multi-template thing, it doesn't matter, selection still will only point at that document if it is opened first.
Just checking...
If one doc was created through word (doc1) and then another created through my app (doc2), would you expect the following two debug lines to produce different results?
Debug.print Selection.Document.FullName
set sel = mObjDocument.Select
Debug.print Selection.Document.FullName
as on mine they both point at doc1 regardless of whether or not I select or do anything with doc2. Also if I close doc1, selection falls over with the error:
The remote server machine does not exist or is unavailable
It is quite the time for me to disconnect from here for today. However, post your full code to manage word here. It will be easyer to find the bug, if not by me, by someone else...
Best regards,
Cesare
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
Cimperiali
September 18th, 2001, 03:27 AM
private Sub Command1_Click()
'Now: my template has following text:
'Aaa
'~10000~5000
'bbb
'ccc
'-----
'and after running the pgm it is so:
'----
'Aaa
'Net Total = 10000~5000
'bbb
'ccc
'Thing I have noticed is: running this two times, make it create another instance of word
'wihch i think to understand is your matter.
'So:
'to create only one instance, enable errorHandling
'to write to your mobjdocument simpluy add before selection:
' mobjdocument.Activate
'Try the following:
Dim wdApp as Word.Application
on error resume next
Err.Clear
set wdApp = Word.Application
If Err.Number > 0 then
set wdApp = new Word.Application
Err.Clear
End If
on error GoTo 0 'disable error handling to let you
'know if other tipes of errors occur
Const prsTemplate = "MyTemp"
Const TOTAL_LOCATION_TEXT = "~"
Dim prcNetTotal
'-----
'for debug purpouse:
wdApp.Visible = true
'-----
Dim mobjdocument as Word.Document
set mobjdocument = wdApp.Documents.Add(prsTemplate & ".dot")
mobjdocument.Activate
mobjdocument.Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = TOTAL_LOCATION_TEXT
.Replacement.Text = ""
.Forward = true
.Wrap = wdFindContinue
.Format = false
.MatchCase = false
.MatchWholeWord = false
.MatchWildcards = false
.MatchSoundsLike = false
.MatchAllWordForms = false
End With
Selection.Find.Execute
If Selection.Text = TOTAL_LOCATION_TEXT then
Selection.TypeText "Net Total = " & FormatCurrency(prcNetTotal)
else
MsgBox "Tag not found"
End If
'You should close and release all reference...
'wdApp.DisplayAlerts = wdAlertsNone
'mobjdocument.SaveAs "c:\docMy01.doc"
'mobjdocument.Close
'set mobjdocument = nothing
'wdApp.Quit
'set wdApp = nothing
End Sub
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
d0wn_under
September 18th, 2001, 11:23 AM
I have included my code below. The problem I am having is not what you assumed, having multiple instances, but being able to access each one. What is happening is that if createDocAndTable is called, it creates a word doc, finds the tag, selects it and then changes it for the table. This works fine if word is not running before hand, ie no other docs are open. If I try to run this a second time, or when another doc is open, the selection object is pointing to the first document not mine, even after doing the mobjDocumet.select (This does seem to hightlight/select my document but not to change the selection object). The result of this is that the selection.find works on the document that selection is pointing at, not at my document that I have just created and consequently the tag isn't found and replaced.
What I need to do is to get the selection object pointing at my document.
Try running the code below twice on a template with the search tag in it. Don't close word or the program in between runs.
Robin
option Explicit
Const TABLE_TAG = "**TABLE_HERE**"
private mObjDocument as Word.Document
private mobjTable as Word.Table
private Sub Command1_Click()
CreateDocAndTable "sergon"
End Sub
public Function CreateDocAndTable(prsTemplate as string) as Boolean
on error GoTo errh
Dim objWord as Word.Application
MsgBox "The template specified was not found", vbCritical
set objWord = nothing
End Function
private Function createTable() as Boolean
Debug.print Selection.Document.FullName
mObjDocument.Activate
mObjDocument.Select
Debug.print Selection.Document.FullName
If Not Selection.Find.Execute(TABLE_TAG, true, false, false, false, false, true, wdFindContinue, false, "", wdReplaceNone) then
MsgBox "Can't create table, no tag found", vbCritical
createTable = false
Exit Function
End If
set mobjTable = mObjDocument.Tables.Add(Selection.Range, 1, 4, wdWord9TableBehavior, wdAutoFitWindow)
Dim rRow as Row
set rRow = mobjTable.Rows.Item(1)
With rRow
.Cells.Item(1).Range.Text = "Code"
.Cells.Item(2).Range.Text = "Description"
.Cells.Item(3).Range.Text = "Quantity"
.Cells.Item(4).Range.Text = "Unit"
End With
rRow.Range.Bold = true
createTable = true
End Function
Cimperiali
September 19th, 2001, 02:58 AM
'I've tried, and found out that even if ObjWord points to correct application
'and mObjDoc is correct too, the Selection object seems to point always at
'first doc as you said (this is clearly a Microsoft bug).
'The only solution I found out is to do all at firs instance of word, that
'means: if you have several instances of word, you can add your new doc from
'your template at first of it.
'While if you want to look for all word applications opened, you'd better use
'enumwindow api and/or findwindow to switch focus to each (but I have not tryed
'to pass values).
'The first solution is rewrited below following your code
on error resume next
Err.Clear
'if it exist, get word
set objWord = GetObject(, "word.application")
If Err > 0 then
'if it does not exist, create a new one
set objWord = CreateObject("word.application")
Err.Clear
End If
on error GoTo 0
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
d0wn_under
September 19th, 2001, 03:21 AM
Thanks for trying and proving to me that I'm not loosing my marbles!
I've just been playing with the range features and managed to do what I want with them as you can set them to be document specific. I was about to post a message saying so.
Again, thanks
Robin
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.