Open form in Word

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

What line does the error occur on?

I assume the missing trailing quotemark on the .Add command, is a posting
typo?

TC
 
I need to print a very large form in word here's what I got so far.

Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "C:\SocServDocs\childinneedofaidPetition.dot
objWord.Visible = True

This is the error message I get
"Object variable or With block variable not set"
and the referrences are set

Can anyone see what it is I've forgot????

Thanks
Mike
 
Try this change to your code; the code you have isn't actually opening Word,
it's just setting a reference to it, I believe. My code change assumes that
Word is not already open on the machine.

Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = CreateObject("Word.Application")

objWord.Documents.Add "C:\SocServDocs\childinneedofaidPetition.dot"
objWord.Visible = True


If Word is already open when you run your code, then try this:

Dim objWord As Object
' Get Microsoft Word using automation
Set objWord = GetObject(, "Word.Application")

objWord.Documents.Add "C:\SocServDocs\childinneedofaidPetition.dot"
objWord.Visible = True
 
'Set objWord = New Word.Application' should do just as well as
Get/CreateObject, methinks!

Assuming that the code he quoted is accurate - which it might not be,
because it did contain at least one typo - the only cause that I can see for
his error, is that the New statement failed to instantiate Word for some
reason, so objWord was left at Nothing. This would cause the error in
question to occur on the next line.

I still don't see why he can't confirm where the error occurs. If there is
no error trapping, then surelt, execution will stop on the line in question,
and that line will be highlighted. No?

TC
 
Ok guys I've got it to where Access opens Word, but for some reason I can't
it to open the template doc. I keep getting a Synatx error.

The location of the file is as follows
C:\Templates\Doc1.dot

One line is all I need I think???
At this point in time I would kill to have a programer in this town.

Mike
 
You want to open an existing document? Then don't use the .Add method. Use
the .Open method instead:

Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = CreateObject("Word.Application")

objWord.Documents.Open "C:\SocServDocs\childinneedofaidPetition.dot"
objWord.Visible = True
 
TC said:
'Set objWord = New Word.Application' should do just as well as
Get/CreateObject, methinks!

Yes, using New Word.Application should enable the opening of Word when his
first code line using objWord is utilized. I prefer using CreateObject
because it's easier to debug the code when errors occur with the
application.
 
Thanks for the Open,
Now one more problem,

Now it is telling me the file name or path are not right.
They are, I even change the name of the doc to make is easier and set it
right in the C: dirve
Current path

"C:\Doc1.dot" this is the full path and doc name.
The full code as it is:
____________________________________________
Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = CreateObject("Word.Application")

objWord.Documents.Open "C:\Doc1.dot" (Runs to here then errors out)
objWord.Visible = True
_______________________________________________

??Do I need a statement in there to open Word first then the Document??

Mike
 
Michael E Phillps said:
Thanks for the Open,
Now one more problem,

Now it is telling me the file name or path are not right.
They are, I even change the name of the doc to make is easier and set
it right in the C: dirve
Current path

"C:\Doc1.dot" this is the full path and doc name.
The full code as it is:
____________________________________________
Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = CreateObject("Word.Application")

objWord.Documents.Open "C:\Doc1.dot" (Runs to here then
errors out) objWord.Visible = True
_______________________________________________

??Do I need a statement in there to open Word first then the
Document??

Mike

I believe your original code was correct, and that the error you were
getting was happening elsewhere. I use this code in one of my
applications, to create a new document based on a template other than
Normal.dot:

Dim appWord As Word.Application

' Open Word and create a new document based on the template
' we've defined for this purpose.
Set appWord = New Word.Application
appWord.Documents.Add CurrentProject.Path & "\TearSheet.dot"

' ... code working withe the document ...

With appWord.ActiveDocument

If SaveIt Then
.SaveAs strFileName
End If

If PrintIt Then
.PrintOut Background:=True
End If
End With

appWord.Quit False
Set appWord = Nothing

This all works just fine. However, it's easy when working with the
application object and document to omit a necessary object-reference
qualifier. So if you could post the complete code you originally had we
might be able to see where the error came in. Or else you could set a
breakpoint, then step through the code and identify the line where the
error occurs.
 
Hey there, Dirk. Thanks for your assistance!

His original code was this:

Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "C:\SocServDocs\childinneedofaidPetition.dot
objWord.Visible = True

He got an error related to Object variable not set.



The code to which I changed him is this:

Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = CreateObject("Word.Application")

objWord.Documents.Open "C:\SocServDocs\childinneedofaidPetition.dot"
objWord.Visible = True


I'm not as familiar with Word as I am with Excel. Thanks.
 
Ken Snell said:
Hey there, Dirk. Thanks for your assistance!

His original code was this:

Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = New Word.Application

objWord.Documents.Add "C:\SocServDocs\childinneedofaidPetition.dot
objWord.Visible = True

He got an error related to Object variable not set.



The code to which I changed him is this:

Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = CreateObject("Word.Application")

objWord.Documents.Open "C:\SocServDocs\childinneedofaidPetition.dot"
objWord.Visible = True


I'm not as familiar with Word as I am with Excel. Thanks.

I'm no expert on Word either, but the practical difference between your
proposed code and mine is that yours opens the document template as the
document to be worked on, while mine creates a new document based on
that template. I *think* the latter is probably what Mike had in mind.

As for the difference between
Set objWord = CreateObject("Word.Application")
and

Set objWord = New Word.Application

, I don't think it's relevant to the error. Something else is wrong,
though some of the modifications and gyrations that have transpired
since his original post may have obscured it. We really need for Mike
to post the complete original code and step through it to find the line
on which the error occurs. I wonder what his VB option settings for
Error trapping are, that he's not being brought directly to the
offending line?
 
Gentlemen I really do appreciate your help but I am completely confused??
How about if I tell you what I'm trying to do
______________
I have a large legal document for the Social Services Department. I have
created basic intake form in Access with a command button which is suppose
to open a template in Word and enter the data into the bookmarks.
The template name is Doc1.dot and in sets in this folder C:\SocServDocs. So
I'm going from Access to Word.

Thanks Ever So Much for your help
Mike
 
Michael E Phillps said:
Gentlemen I really do appreciate your help but I am completely
confused?? How about if I tell you what I'm trying to do
______________
I have a large legal document for the Social Services Department. I
have created basic intake form in Access with a command button which
is suppose to open a template in Word and enter the data into the
bookmarks.
The template name is Doc1.dot and in sets in this folder
C:\SocServDocs. So I'm going from Access to Word.

Thanks Ever So Much for your help
Mike

That's very much like the routine from which I snipped the code I
posted. Here's the whole routine (with just a few nonessentials
trimmed) in the hope that it may help you:


'----- start of code -----
Function BuildQuoteAsWordDoc( _
Optional PrintIt As Boolean = False, _
Optional SaveIt As Boolean = True) _
As String

' Builds a quote for an item as a Word document.
' Returns the name of the document file created, or a zero-length
' string if no document could be created. If the optional argument
' PrintIt is specified as True, the generated quote document will
' be printed. If the optional argument SaveIt is specified as
False,
' the document will not be saved.

On Error GoTo Err_BuildQuoteAsWordDoc

Dim frmCatalog As Form
Dim appWord As Word.Application
Dim strFileName As String

DoCmd.Hourglass True

' Get a reference to the Catalog form, which *must* be open and
' not at a new record.
Set frmCatalog = Forms("frmCatalogItems")

SysCmd acSysCmdSetStatus, _
"Preparing quote for item " & frmCatalog.StockNo & " ..."

' Get a recordset listing all the available pictures for the current
' item.
Set rsPix = _
CurrentDb.OpenRecordset( _
"SELECT PhotoFile FROM ItemPhotos WHERE StockNo=" & _
frmCatalog.StockNo & _
" ORDER BY IsNull(PhotoOrder) DESC, PhotoOrder ASC")

' Open Word and create a new document based on the template
' we've defined for this purpose.
Set appWord = New Word.Application
appWord.Documents.Add CurrentProject.Path & "\TearSheet.dot"

' The template contains bookmarks for the information
' the quote should contain. Fill them in from the controls
' on this form and on the catalog form.

With appWord.ActiveDocument.Bookmarks

.Item("Customer").Range.Text = Me.txtCustomer & vbNullString
.Item("Address").Range.Text = Me.txtAddress & vbNullString
.Item("QuoteDate").Range.Text = Format(Date, "Long Date")
.Item("StockNo").Range.Text = frmCatalog.StockNo
.Item("Title").Range.Text = frmCatalog.Title & vbNullString
.Item("Description").Range.Text = frmCatalog.Description &
vbNullString
.Item("Dimensions").Range.Text = frmCatalog.Dimensions &
vbNullString
.Item("Price").Range.Text = Format(Me.txtQuotePrice, "$#,##0")

If Not IsNull(frmCatalog.SalesUnit) Then
.Item("SalesUnit").Range.Text = "/" & frmCatalog.SalesUnit
End If

End With

strFileName = CurrentProject.Path & "\QuoteItem" &
frmCatalog.StockNo & ".doc"

With appWord.ActiveDocument

If SaveIt Then
.SaveAs strFileName
End If

If PrintIt Then
.PrintOut Background:=True
End If
End With

BuildQuoteAsWordDoc = strFileName


Exit_BuildQuoteAsWordDoc:
On Error Resume Next
DoCmd.Hourglass False
SysCmd acSysCmdClearStatus
Set frmCatalog = Nothing
' appWord.Visible = True
appWord.Quit False
Set appWord = Nothing
Exit Function

Err_BuildQuoteAsWordDoc:
DoCmd.Hourglass False
MsgBox "Error building quote: " & Err.Description, _
vbExclamation, "Error " & Err.Number
Resume Exit_BuildQuoteAsWordDoc

End Function

'----- end of code -----
 
How do you guys do this stuff
I am about to pull my hair out. One more time

Here is the code I have setting behind the Command Button
-----------------------------------------------------------------
Dim oWord As Object

' Open Microsoft Word using automation and insert
' Data from Database

Set oWord = New Word.Application

oWord.Documents.Open CurrentProject.Path & "\Templates\Doc1.dot"

oWord.Documents.Open "C:\NVB Database\Templates\Doc1.dot"
oWord.Visible = True
----------------------------------------------------------------------------
--
This is the Error
This file could not be found.
Try one or more of the following:
*Check the spelling of the name of the Document
*Try a different File Name
(C:\NVB Database\Templates\Doc1.dot)

The spelling is correct
The File is correct
The Path is correct
So what gives???????????????

Going Crazy In Barrow
Mike
 
Michael E Phillps said:
How do you guys do this stuff
I am about to pull my hair out. One more time

Here is the code I have setting behind the Command Button
-----------------------------------------------------------------
Dim oWord As Object

' Open Microsoft Word using automation and insert
' Data from Database

Set oWord = New Word.Application

oWord.Documents.Open CurrentProject.Path & "\Templates\Doc1.dot"

oWord.Documents.Open "C:\NVB Database\Templates\Doc1.dot"
oWord.Visible = True
---------------------------------------------------------------------- ------

The spelling is correct
The File is correct
The Path is correct
So what gives???????????????

Did you really leave in this line:
oWord.Documents.Open CurrentProject.Path & "\Templates\Doc1.dot"

That line will fail if your .mdb file isn't in "C:\NVB Database". But
the following works just peachy for me:

Dim oWord As Object

Set oWord = New Word.Application
oWord.Documents.Open "C:\Temp\Doc1.dot"
oWord.Visible = True

(once I've created a Word file named "Doc1.dot" in my C:\Temp folder).
The only reason I can think of for your getting the error is that in
fact the file name or path isn't spelled right. Have you double- and
triple-checked this?
 
Back
Top