word vba problem

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » WORD VBA problem

WORD VBA problem

resolvedResolved · Medium Priority · Version 2010

Stephen has attended:
Excel VBA Intro Intermediate course

WORD VBA problem

Hi There

This is funny one as its relating to Word VBA and not Excel:

I have a mailmerge splitter that gets to a certain point and then stops with a syntax error and i can't understand why!!

If anyone can help, that would be great. I have poseted the written VBA below:

Sub MailMergeSplitter()

' Parameters

Dim Letters As Long
Dim Counter As Long
Dim DocumentCounter As Long
Dim DocName As String
Dim oDoc As Document
Dim oNewDoc As Document
Dim GenisysFolderShare As String
Dim DateTimeFormatForDocumentName As String

' Location of Genisys Share where documents are store for indexing

GenisysFolderShare = "C:\TEMP\"

nTime = Timer

Application.ScreenUpdating = False

Set oDoc = ActiveDocument

Selection.EndKey Unit:=wdStory

Letters = Selection.Information(wdActiveEndSectionNumber)

Selection.HomeKey Unit:=wdStory

Counter = 1
DocumentCounter = 1

While Counter < Letters

LTrim$ (Str$(DocumentCounter)) & ".docx"


' Copy section contents and time to use as temporary document name

DateTimeFormatForDocumentName = Format(Now(), "yyyyMMdd.hhmmss")

' Set temporary document name using current date and time and counter

DocName = GenisysFolderShare & DateTimeFormatForDocumentName & "_" & oDoc.Sections(Counter).Range.Copy

' Check for extra page break or continuous break by checking if Copy has any content

If oDoc.Sections(Counter).Range.Characters.Count > 1 Then

Set oNewDoc = Documents.Add

With Selection
.Paste
.EndKey Unit:=wdStory
.MoveLeft Unit:=wdCharacter, Count:=1
.Delete Unit:=wdCharacter, Count:=1
End With

oNewDoc.SaveAs FileName:=DocName, _
FileFormat:=wdFormatDocument, _
AddToRecentFiles:=False
ActiveWindow.Close

DocumentCounter = DocumentCounter + 1

End If

Counter = Counter + 1

Wend

Application.ScreenUpdating = True

' Output completed message and details

MsgBox "Finished creating individual mail merge documents" & vbNewLine & vbNewLine & DocumentCounter - 1 & " documents created" & vbNewLine & vbNewLine & "Time taken: " & Timer - nTime & " seconds"

End Sub


the error happens on the following line:

DocName = GenisysFolderShare & DateTimeFormatForDocumentName & "_" & oDoc.Sections(Counter).Range.Copy

Thanks in anticipation to anyone that could help

Steve

RE: WORD VBA problem

Hi Stephen

Thanks for getting in touch. I've had a look over your code, and although I haven't tested it for myself one thing stood out: as part of the filename, in the date format, you insert a dot or period. This *shouldn't* be a problem but have you tried it without?

As this line is the part giving you issues, have you tried an entirely hardcoded filename to see if it throws that out too? eg. "c:\temp\MyDoc2013.docx", then gradually substitute variable parts.

Also what is added to end of the filename? Would that be a number? There is possibility that needs to be parsed into a String type before concatenating to the filename.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: WORD VBA problem

Hi Gary

Thanks for getting back to me quickly

I made the changes you suggested but now it seems to be stuck on the following line:


DocName = GenisysFolderShare & DateTimeFormatForDocumentName & "_" & oDoc.Sections(Counter).Range.Copy

In particular it freezes at the point .Copy - stating: Compile Error!! Expecting Function or Variable

Regards

Steve

RE: WORD VBA problem

Hi Steve

After hardcoding the filename, have you tried commenting out the problematic line? (You will have to make sure that it is a valid filepath and the file doesn't already exist)

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: WORD VBA problem

Hi Gary

I commented out the offending line , the code runs but it doesn't create any documents. The message box informs me that zero documents were created

Steve

RE: WORD VBA problem

Hi Steve

Interesting - so it never hits the IF block to increment DocumentCounter.

I'll run this properly and get back to you tomorrow.



Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: WORD VBA problem

Hi Steve

I found where you'd copied the original from, and you'd not quite grabbed all of it.

This line

DocName = GenisysFolderShare & DateTimeFormatForDocumentName & "_" & oDoc.Sections(Counter).Range.Copy

should be

DocName = GenisysFolderShare & DateTimeFormatForDocumentName & "_" & Counter & ".doc"

oDoc.Sections(Counter).Range.Copy

Which is TWO separate lines. In the first line the counter is added to the end of the filename, along with ".doc" (you can change this to docx if you prefer).

Then the next line performs the copy. By pushing them on to the same line Word wasn't sure what you were trying to do.

Give this a try and let me know how you get on.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: WORD VBA problem

Thanks for all the help on this project Gary... really appreciate it

Steve

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Quickly copy a formula across sheets

Suppose you have a formula in cell Sheet1!B2, say =A1*5%, that you wish to copy to cell B2 on Sheet2, Sheet3 and Sheet4. Instead of using copy and paste, try this: (1) Select Sheet1!B2. (2) Group Sheet1 with the worksheets Sheet2, Sheet3 and Sheet4 by holding down Ctrl and clicking on the tabs of the sheets to group them. (3) Press the F2 key, then immediately press Enter to copy the formula in Sheet1!B2 across the grouped sheets.

Remember to ungroup the sheets afterwards! Right-click on any tab and choose Ungroup Sheets to do that.

View all Excel hints and tips


Server loaded in 0.09 secs.