Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2005

Tip: Looking for answers? Try searching our database.

Why error 91 Objectvariable or Withblock not set ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Filips Benoit - 28 Dec 2005 17:33 GMT
Dear All,

Function to compose a new Word.doc from Access-data.
From some records a pich the content of a memofield, from others i insert
the content of a file.
I supose there somthing wrong with the selection-object.
I'm a novice in word-vba.

Thanks

Public Function MakeNewDoc(ByVal iDocID As Long, ByVal strDocName As String)
As Boolean

On Error GoTo ErrHandling

   Dim wordApp As Word.Application
   Dim wordDoc As Word.Document
   Dim WordSel As Word.Selection
   Dim rs As ADODB.Recordset

   DoCmd.Hourglass True

   Set wordApp = CreateObject("Word.Application")
   MakeNewDoc = False

      wordApp.Visible = True
      wordApp.WindowState = wdWindowStateMaximize
      wordApp.Documents.Add
      Set WordSel = Selection
       Set rs = New ADODB.Recordset
       rs.Open "SELECT TBL_TEKSTBLOK_DOCUMENT_MAP.TBDOCM_VOLG_NR,
TBL_TEKST_BLOK.TB_TEKST, TBL_TEKST_BLOK.TB_USE_FILE,
TBL_TEKST_BLOK.TB_FILE_FULL_PATH FROM TBL_TEKST_BLOK INNER JOIN
TBL_TEKSTBLOK_DOCUMENT_MAP ON TBL_TEKST_BLOK.TB_ID =
TBL_TEKSTBLOK_DOCUMENT_MAP.TBDOCM_TB_ID WHERE
(((TBL_TEKSTBLOK_DOCUMENT_MAP.TBDOCM_DOC_ID) = " & iDocID & ")) ORDER BY
TBL_TEKSTBLOK_DOCUMENT_MAP.TBDOCM_VOLG_NR;", CurrentProject.Connection,
adOpenForwardOnly, adLockOptimistic, adCmdText
       With WordSel
            .Font.Size = 12
            .TypeText strDocName & " (" & Environ("username") & ")" &
vbCrLf
            .TypeText "--------------------------------------------------"
& vbCrLf & vbCrLf

            Do Until rs.EOF
                If rs.Fields("TB_USE_FILE") Then 'Add the content of a file
                   .InsertFile rs.Fields("TB_FILE_FULL_PATH"), "", False,
False, False
                Else 'Add the content of a memo-field
                   .InsertAfter rs.Fields("TB_TEKST")
                End If
               .TypeText vbCrLf & vbCrLf
             rs.MoveNext
           Loop
       End With
      wordApp.ActiveDocument.SaveAs CurrentProject.Path & "\" & strDocName
& ".Doc"
      wordApp.Quit

   Set WordSel = Nothing
   Set wordDoc = Nothing
   Set wordApp = Nothing

   MakeNewDoc = True
   DoCmd.Hourglass False

Exit Function

ErrHandling:

   DoCmd.Hourglass False
   MsgBox "Error in function MakeNewDoc" & vbCrLf & vbCrLf & Err.Number & "
" & Err.Description

End Function
RobertH - 28 Dec 2005 18:19 GMT
Have you stepped through to find exactly where the error gets thrown?

> Dear All,
>
[quoted text clipped - 72 lines]
>
> End Function
Filips Benoit - 28 Dec 2005 19:02 GMT
Sorry I found it myself !
WORD.selection because my Access-interface has references to Outlook and
Excel too.

> Have you stepped through to find exactly where the error gets thrown?
>
[quoted text clipped - 41 lines]
> > vbCrLf
> >              .TypeText
"--------------------------------------------------"
> > & vbCrLf & vbCrLf
> >
[quoted text clipped - 29 lines]
> >
> > End Function
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.