Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

visual basic and Microsoft word

Status
Not open for further replies.

mciing7912

Programmer
Jul 14, 2006
16
MX
HI guys!

I have a VB application and I have a function there, for working with Microsoft Word.

this is the begin the code:

Set WordTemplate = CreateObject("WORD.APPLICATION")
Set WordReport = GetObject(app.Path + "\NewDoc.doc")

With WordReport.Application

.Visible = True
.Selection.Find.ClearFormatting
.Selection.Find.Text = "Construction"
.Selection.Find.Replacement.Text = "-----------------"
.Selection.Find.Forward = True
.Selection.Find.Execute

........

My problem is, if the user already has a word document openend or, while I'm working in VB with the Word document and the user goes to other editor, the program crashed and say me the error:

**Object variable or with block variable not set**

this error can occurs in any part of the code where I'm using the wordreport object, for example

WordReport.Application.Selection.MoveLeft Unit:=wdCharacter, Count:=1

I was thinking if I need to close all word applications before I start with this, but, I saw, also, if you don't have any Word document opened, this error occurs

any idea??

thanks
 
This error usually occurs, if you forgot to specify that you are referring to the word application object in any part of your code. It can also occur, if you move your focus away from the executing code by making the word application visible.
Besides that, you should use your newly created word application rather than the getobject method,
e.g.
Code:
Set WordTemplate = CreateObject("WORD.APPLICATION")
  Set WordReport = [b]WordTemplate.Documents.Open[/b](app.Path + "\NewDoc.doc")

  With [b]WordTemplate[/b]
    .Selection.Find.ClearFormatting
    .Selection.Find.Text = "Construction"
    .Selection.Find.Replacement.Text = "-----------------"
    .Selection.Find.Forward = True
    .Selection.Find.Execute
...
As you can see, I have also removed the "visible" line from your code, as it is not necessary.
Is it?
;-)
Hope this helps.
Andy

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
Hi Makeitso!

I tried you tell me but, if a remove the VISIBLE property, the document is a mess!! and if I leave it, the documents is created fine!!

any idea??


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top