Solved

OLE-Automation via VB

Posted on 2003-11-26
11
512 Views
Last Modified: 2013-12-26
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
Comment
Question by:die-raubfischer
  • 6
  • 4
11 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9824227
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
 

Author Comment

by:die-raubfischer
ID: 9824684
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9825054
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
 

Author Comment

by:die-raubfischer
ID: 9825284
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
 
LVL 4

Expert Comment

by:Nazermohideeen
ID: 9826032
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:die-raubfischer
ID: 9830156
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
 

Author Comment

by:die-raubfischer
ID: 9830241
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 100 total points
ID: 9830478
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9830508
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
 

Author Comment

by:die-raubfischer
ID: 9830937
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
 

Author Comment

by:die-raubfischer
ID: 9835465
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now