Help...Object variable or With block variable not

Results 1 to 2 of 2

Thread: Help...Object variable or With block variable not

  1. #1
    Sbarlow Guest

    Default Help...Object variable or With block variable not

    Here is the code in my class file...<BR><BR>Dim ObjWord As Object<BR>Dim MyDoc As Word.Document<BR><BR>Private Function OpenWord()<BR> Set ObjWord = CreateObject("Word.Application")<BR>End Function<BR><BR>Private Sub KillWord()<BR> Set ObjWord = Nothing<BR>End Sub<BR><BR>Public Sub CreateCertificate(ByVal Firstname As String, ByVal LastName As String, ByVal CourseName As String, ByVal CourseNumber As Variant, ByVal Location As String, Optional ByVal CompletionDate As Date)<BR>If OpenWord Then<BR> Set MyDoc = ObjWord.Documents.Add(Template:="c:alpha")<BR>End If<BR> If Not IsDate(CompletionDate) Then<BR> CompletionDate = Date<BR> Exit Sub<BR> End If<BR> MyDoc.Bookmarks.Item("NameFirst").Range.Text = Firstname<BR> MyDoc.Bookmarks.Item("NameLast").Range.Text = LastName<BR> MyDoc.Bookmarks.Item("CourseName").Range.Text = CourseName<BR> MyDoc.Bookmarks.Item("CourseNo").Range.Text = CourseNo<BR> MyDoc.Bookmarks.Item("Location").Range.Text = Location<BR> MyDoc.Bookmarks.Item("Date").Range.Text = CompletionDate<BR> ObjWord.ActiveDocument.SaveAs FileName:="Certificates.doc"<BR> ObjWord.Quit<BR> Set MyDoc = Nothing<BR>Call KillWord<BR>End Sub<BR><BR>I have tried everything. I have checked with Microsoft and done everything they said and I still get the error. It is a simple com dll and I just cant figure out what I am missing. Please help. The project is due tomorrow and I have been pulling my hair out for the last 2 days. I have rewritten it 4 different ways and same error everytime.<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Help...Object variable or With block variable

    Sbarlow:<BR><BR>I popped your code into a VB com object and noticed that the error happened on the first call to:<BR><BR>MyDoc.Bookmarks.Item("NameFirst").Range .Text = Firstname<BR><BR>When I stepped through your code, it appeared that the setting of the MyDoc object wasn&#039t getting executed...<BR><BR>The reason why (it appears) is that OpenWord wasn&#039t returning anything. I&#039m guessing that you meant it to return a value - that&#039s why you defined it as a Function (rather than a Sub)...Take a look-see at the code snippet below - I think this might get you over the hump:<BR><BR>Private Function OpenWord() As Boolean<BR> Set ObjWord = CreateObject("Word.Application")<BR> If Not ObjWord Is Nothing Then<BR> OpenWord = True<BR> Else<BR> OpenWord = False<BR> End If<BR>End Function<BR><BR>HTH - Heather<BR><BR><BR>

Posting Permissions

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