• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

OLE-Automation via VB

Hello,
i have written a vb-program which create word-docs over OLE-automation.
the program works fine. But e.g. when a user open winword, my vb-program get a error.
It looks like as lose my vb-program the focus on the created wordObject.
Rather my vb-program will work on the other open word-session and get a error.

I'am create the word-object as follow:
Public gobjWord As Object
Set gobjWord = CreateObject("Word.Application." + gsWordVersion)

who can help me?

Danke
richi
0
die-raubfischer
Asked:
die-raubfischer
  • 6
  • 4
1 Solution
 
GrahamSkanRetiredCommented:
Hi,
What is the error message?
What is the line of code that gives the error message?
What version of VB and what version of Word are you using?

Also, have you tried early binding:

Set a referenence to the Microsoft Word Object Library and declare Word Objects by specific type e.g

Dim gobjWord As New Word.Application
Dim Doc as Word.Document
0
 
die-raubfischerAuthor Commented:
hello grahamskan,

the error-code is in this case not so important. this is a aftereffect.
the vb-version is 6 and word-version ist 9.

decisive is, the vb-program is losing the focus on her own word-instance.
the program will work on the other new opend word-session.

how can i bind the  "CreateObject(..." only on this word-session?

thanks
richi
0
 
GrahamSkanRetiredCommented:
Don't worry about early or late binding. If you know late binding then keep using it. I find early binding easier, because VB then knows about the objects and properties when you Dim them.

The program will work on the instance that contains the document being referenced. How do you tell the program which document to use? Is it a new document?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
die-raubfischerAuthor Commented:
Here a short overview:
1. step: Creating a Word-Object!
    Public gobjWord As Object
    gsWordVersion = GetIniValue("DocServer", "WordVersion")
    Set gobjWord = CreateObject("Word.Application." + gsWordVersion)

2.  step: Load Template and save as new document
    Dim wdDocument As Object
    Set wdDocument = gobjWord.Documents.Add(Template:=sTemplate)
    wdDocument.SaveAs FileName:=sDocNameWithPath

3. step: Processing Document( Substitute Text ......)

4. step: Saving the Document
     wdDocument.Save
     wdDocument.Close
0
 
NazermohideeenCommented:
Hi,
   Once you get the handle of the Word Application, Word document, it dosen't matter that the user opens another instance of word. You would be working only with the handle that you got for the object which you created. I am using Word 10. I dint get those errors. If you said how you write the text to your document, or you post the complete module where you get the error, I could help you better. (I used selection object to write the text to the document)

Nazer MNM
0
 
die-raubfischerAuthor Commented:
hi,
sorry that is not correct. My VB-Programm does not work only with the handle of the self opend resp. created word-dokument. The VB-Programm work with the "word-application-object". it's never mind which document is opend. important is which document has the focus. And exact that
is the problem.
Recap: No word is running. My vb-application is starting an open a word-session with "created object". my vb-app is processing some ole-operations (selection find /selection replace ....).
When in this moment a user open a other word-session, the user-word-session get the focus.
Just in this moment my vb-app will processing the job on the open word from the user.

The question is now:
How can i fix bind the created object so, that is independent from the currently focus?
0
 
die-raubfischerAuthor Commented:
hi,
i have a suspicion. is the ".ActiveDocument." the problem????

Public Sub DoDocument(ByVal vsDocName, ByVal vsDotNameWithPath, ByVal vsOID_DOCO As String, ByVal viLFDNR As Integer, ByVal viSLFDNR As Integer, ByVal vskopf As String, ByVal vszentral As String, ByVal vsfuss As String, ByVal vsprotect As String, ByVal vsfldkezi As String)
   
    Dim wdDocument As Object
    Dim sTemplate As String
    Dim sDocName As String
    Dim sDocNameWithPath As String
    Dim sSQL As String
    Dim sTextName As String
    Dim sTextValue As String
    Dim aTableIsExtended(100) As Boolean
    Dim rsWork As New ADODB.Recordset
    Dim bReplaceAllText
    Dim i As Integer
    Dim rows As Integer
       
    On Error GoTo ErrorHandler
         
     sTemplate = vsDotNameWithPath
    sDocName = vsDocName
       'Lege neues Dokument an
    Set wdDocument = gobjWord.Documents.Add(Template:=sTemplate)
    wdDocument.SaveAs FileName:=sDocNameWithPath
    If wdDocument.ProtectionType > 0 Then
        wdDocument.Unprotect (gsDotSchutzPwd)
    End If
    gobjWord.Documents(sDocName).Activate
    For i = 0 To gobjWord.ActiveDocument.Tables.Count - 1
        aTableIsExtended(i) = False
    Next i
    rows = 0
    ReDim gs_sTextName(1)
    ReDim gs_sTextValue(1)
    'Hole zu ersetzende Daten, ersetze in Dokument
   select ....
    rsWork.Open sSQL, gevtcn.objConnection
    While Not rsWork.EOF
        rows = rows + 1
        If rows > 1 Then
            ReDim Preserve gs_sTextName(rows)
            ReDim Preserve gs_sTextValue(rows)
        End If
        gs_sTextName(rows) = rsWork("FORMFLD_DOCD")
        gs_sTextValue(rows) = rsWork("VAL_DOCD")
        rsWork.MoveNext
    Wend
    rsWork.Close
    rows = UBound(gs_sTextName)

    If vszentral = "1" Then
        'Zum ersetzen Ansicht auf Hauptdokoument setzen
        SetSeekView wdPrintView, wdSeekMainDocument
        For i = 1 To rows
            SubstituteText gs_sTextName(i), gs_sTextValue(i), True
        Next i
        ResetSeekView
    End If
    If vskopf = "1" Then
        'Zum ersetzen Ansicht auf Kopfzeile setzen
        SetSeekView wdPrintView, WdSeekCurrentPageHeader
        For i = 1 To rows
             SubstituteText gs_sTextName(i), gs_sTextValue(i), True
        Next i
        If gobjWord.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True Then
            With gobjWord.ActiveWindow.View
                .NextHeaderFooter
                For i = 1 To rows
                    SubstituteText gs_sTextName(i), gs_sTextValue(i), True
                Next i
            End With
        End If
        ResetSeekView
    End If
    If vsfuss = "1" Then
        'Zum ersetzen Ansicht auf Fusszeile setzen
        SetSeekView wdPrintView, wdSeekCurrentPageFooter
        For i = 1 To rows
             SubstituteText gs_sTextName(i), gs_sTextValue(i), True
        Next i
        If gobjWord.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True Then
            With gobjWord.ActiveWindow.View
                .NextHeaderFooter
                For i = 1 To rows
                    SubstituteText gs_sTextName(i), gs_sTextValue(i), True
                Next i
            End With
        End If
        ResetSeekView
    End If
   
    'Mehrere Positionen
    sSQL = "SELECT .....'"
    rsWork.Open sSQL, gevtcn.objConnection
   
    While Not rsWork.EOF
        sTextName = rsWork("FORMFLD_DOCD")
        SubstituteMultipleText vsOID_DOCO, viLFDNR, viSLFDNR, sTextName, aTableIsExtended
        rsWork.MoveNext
    Wend
    rsWork.Close
   
'    If wdDocument.ProtectionType = wdNoProtection Then
    If wdDocument.ProtectionType = -1 And vsprotect = "1" Then
'        wdDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
        wdDocument.Protect Type:=2, NoReset:=True, Password:=gsDocSchutzPwd
    End If
    wdDocument.Save
    wdDocument.Close
   
    Set rsWork = Nothing
    Set wdDocument = Nothing
       
  .....
    Exit Sub

ErrorHandler:
  .....
End Sub
Sub SubstituteText()
 Selection find execute replace e.g........................
End Sub
0
 
GrahamSkanRetiredCommented:
Hi Richi,
I suspect so. That's why I asked you how you were getting your document.
Your program correctly gets the document object as wdDocument. You then activate it (probably unnecessarily) by name instead of using the object you already have.

  wdDocument.Activate would do that OK

Later on you hope that your document is still the ActiveDocument.

Stick to referring to it as wdDocument


0
 
GrahamSkanRetiredCommented:
Incdentally, you'll probably want to close the application down afterwards, unless a user has opened another document

     If gobjWord.Documents.Count = 0 Then
        gobjWord.quit
    End If

0
 
die-raubfischerAuthor Commented:
hi grahamskan,
i will make some code-changes and try it again.
After this i give a response of succes or failure.
Thank you for your time.
RICHI
0
 
die-raubfischerAuthor Commented:
hi,

the problem was: Word.Application-Object work with the word-instance that have the focus.
Workaround:
For all Word.application-Methods i set my open document to active.
Sub SetActiveDocument()
   If gobjWord.Documents.Count > 1 Then
        gwdDocument.Activate
   End If
End Sub

thanks for your help!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now