CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Sep 2001
    Posts
    14

    VB/VBA talking to Word

    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?

    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

    Thanks

    Robin


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Sep 2001
    Posts
    14

    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



  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Sep 2001
    Posts
    14

    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.


  6. #6
    Join Date
    Sep 2001
    Posts
    14

    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.






  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  8. #8
    Join Date
    Sep 2001
    Posts
    14

    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


  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  10. #10
    Join Date
    Sep 2001
    Posts
    14

    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



  11. #11
    Join Date
    Sep 2001
    Posts
    14

    Re: VB/VBA talking to Word

    Sorry, make that


    Debug.print Selection.Document.FullName mObjDocument.Select
    Debug.print Selection.Document.FullName




    Robin


  12. #12
    Join Date
    Sep 2001
    Posts
    14

    Re: VB/VBA talking to Word

    Sorry, make that


    Debug.print Selection.Document.FullName
    mObjDocument.Select
    Debug.print Selection.Document.FullName




    Robin


  13. #13
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Quite the time...

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  14. #14
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  15. #15
    Join Date
    Sep 2001
    Posts
    14

    rephrase the question

    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

    set objWord = CreateObject("word.application")

    objWord.Visible = true
    set mObjDocument = objWord.Documents.Add(prsTemplate & ".dot", false, 0, true)

    Debug.print mObjDocument.FullName

    CreateDocAndTable = createTable

    set objWord = nothing

    Exit Function
    errh:
    CreateDocAndTable = false

    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






Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured