Re: VB/VBA talking to Word
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
Re: VB/VBA talking to Word
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
Re: VB/VBA talking to Word
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
Re: VB/VBA talking to Word
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.
Re: VB/VBA talking to Word
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.
Re: VB/VBA talking to Word
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
Re: VB/VBA talking to Word
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
Re: VB/VBA talking to Word
'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
Re: VB/VBA talking to Word
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
Robin
Re: VB/VBA talking to Word
Sorry, make that
Debug.print Selection.Document.FullName mObjDocument.Select
Debug.print Selection.Document.FullName
Robin
Re: VB/VBA talking to Word
Sorry, make that
Debug.print Selection.Document.FullName
mObjDocument.Select
Debug.print Selection.Document.FullName
Robin
Re: VB/VBA talking to Word
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