Object variable or With block variable not set error in macro

C

cuppless

Ok usually I would just take my time and try to figure this out on my
own but time is short and I willing admit I am way out of my league.
I found the code on gmayor.com for splitting mail merges. The macro
works to the extent that it will give the first record but then errors
out with Object variable or With block variable not set. Does anyone
see what is obviously wrong here? And a side question is how do I get
these to be saved as .pdf files?

Thanks in advance for any help

Sub SplitMergeLetter()
' splitter Macro modified to save individual letters with
' information from data source. The filename data must be added to
' the top of the merge letter - see web article.

Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
Application.ScreenUpdating = False
With Selection
.HomeKey Unit:=wdStory
.EndKey Unit:=wdLine, Extend:=wdExtend
.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With
sName = Selection
Docname = "F:\Test\" & sName & ".doc"
ActiveDocument.Sections.First.Range.Cut
Documents.Add
With Selection
.Paste
.HomeKey Unit:=wdStory
.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
.Delete
End With
ActiveDocument.SaveAs FileName:=Docname, _
FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Application.ScreenUpdating = True
Wend
End Sub
 
D

Doug Robbins - Word MVP

Are you running the macro on a document created by executing the mail merge
to a new document? If so, what line of code is highlighted when you get the
error message.

To create .pdf files, after the line that saves each file, and before the
active document is closed, you would have to include a command to print out
the file to a .pdf printer.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

cuppless

I am running the macro on a document that has already had the mail
merge executed. There aren't any lines highlighted which is why I am
so confused. Would it be better to run it another way?

Thanks,
Sallie
 
D

Doug Robbins - Word MVP

But was the document on which you are running the macro the document that
was created by executing the mail merge to a new document?

When the error occurs, what line of code in the VBE editor is highlighted.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

I am running the macro on a document that has already had the mail
merge executed. There aren't any lines highlighted which is why I am
so confused. Would it be better to run it another way?

Thanks,
Sallie
 
C

cuppless

Yes the document I am running the macro on is the document created by
executing the mail merge to a new document.

No lines of code in the VBE editor are highlighted.
 
D

Doug Robbins - Word MVP

If you want to send the document (or several sections of it) to me at
(e-mail address removed) I will take a look at it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Yes the document I am running the macro on is the document created by
executing the mail merge to a new document.

No lines of code in the VBE editor are highlighted.
 
C

cuppless

So, I looked at the script and this is what I am seeing...

1. It looks like the while loop may not be working correctly as it
stops after the macro processes the first section in the merged word
document
2. When I run the macro manually I get the following: "Object
Variable or with variable not set", but the first section still gets
saved with the right document name to the locatioin I specified in the
macro. The other sections in the merged file havent changed.

Thoughts?
 
D

Doug Robbins - Word MVP

After the second line of code, insert the command

MsgBox Letters

and see what it returns.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

So, I looked at the script and this is what I am seeing...

1. It looks like the while loop may not be working correctly as it
stops after the macro processes the first section in the merged word
document
2. When I run the macro manually I get the following: "Object
Variable or with variable not set", but the first section still gets
saved with the right document name to the locatioin I specified in the
macro. The other sections in the merged file havent changed.

Thoughts?
 
C

cuppless

Hi Doug,

Sorry I did not see this last night it. The message box returned a 5
there were four rows merged. So I take that to mean the counter is
working, correct.
 
C

cuppless

I am so excited. I don't want to scare something but I think it is
working!!!

Thank you again for all your help. The writers of this code are my
heroes!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top