ILIVEAGAIN2001
New Member
- Joined
- Jan 24, 2010
- Messages
- 3
Good Afternoon ,
I have the following macro code that I've been unable to resolve a particular run-time error. The macro is called from Excel 2007, takes an Excel worksheet to be used as a Word 2007 document.
I want to generate simply a list.
The macro does work sometimes. When I say sometimes, it's literally about 50% of the time!
When the macro fails, it causes a run-time error of 426. The VBA help does not do much for me as I can't figure out why it would work sometimes, and not others...
Here's the macro code:
Sub esporta2()
Dim Wrd As Word.Application
Dim Doc As Word.Document
Set Wrd = CreateObject("Word.Application", "Roberto")
Wrd.Visible = True
i = 2
j = 1
Do While Cells(i, j).Value <> ""
i = i + 1
Loop
i = i - 1
Set Doc = Wrd.Documents.Open("C:\Prova\Word.doc")
Doc.Bookmarks("Inizio").Select
Range(Cells(2, 1), Cells(i, 4)).Select
Selection.Copy
Wrd.Selection.Font.Name = "Arial"
Wrd.Selection.Font.Size = 8
Wrd.Selection.Font.Underline = wdUnderlineNone
Wrd.Selection.Font.Bold = False
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(14), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(15.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(17.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(2, 8).Select
Selection.Copy
Doc.Bookmarks("Nomecliente").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(3, 8).Select
Selection.Copy
Doc.Bookmarks("Indirizzo").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(4, 8).Select
Selection.Copy
Doc.Bookmarks("cap").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(5, 8).Select
Selection.Copy
Doc.Bookmarks("Città").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(6, 8).Select
Selection.Copy
Doc.Bookmarks("pv").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(15, 9).Select
Selection.Copy
Doc.Bookmarks("Nettomateriali").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Doc.SaveAs Filename:="OFFERTA.doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
The run-time error occurs at the following line:
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(14), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(15.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(17.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
When I bring up the help for the run-time error, it's pretty vague
Hoping someone can help me solve this problem
THANKSSSSSSS
<!-- / message -->
I have the following macro code that I've been unable to resolve a particular run-time error. The macro is called from Excel 2007, takes an Excel worksheet to be used as a Word 2007 document.
I want to generate simply a list.
The macro does work sometimes. When I say sometimes, it's literally about 50% of the time!
When the macro fails, it causes a run-time error of 426. The VBA help does not do much for me as I can't figure out why it would work sometimes, and not others...
Here's the macro code:
Sub esporta2()
Dim Wrd As Word.Application
Dim Doc As Word.Document
Set Wrd = CreateObject("Word.Application", "Roberto")
Wrd.Visible = True
i = 2
j = 1
Do While Cells(i, j).Value <> ""
i = i + 1
Loop
i = i - 1
Set Doc = Wrd.Documents.Open("C:\Prova\Word.doc")
Doc.Bookmarks("Inizio").Select
Range(Cells(2, 1), Cells(i, 4)).Select
Selection.Copy
Wrd.Selection.Font.Name = "Arial"
Wrd.Selection.Font.Size = 8
Wrd.Selection.Font.Underline = wdUnderlineNone
Wrd.Selection.Font.Bold = False
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(14), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(15.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(17.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(2, 8).Select
Selection.Copy
Doc.Bookmarks("Nomecliente").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(3, 8).Select
Selection.Copy
Doc.Bookmarks("Indirizzo").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(4, 8).Select
Selection.Copy
Doc.Bookmarks("cap").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(5, 8).Select
Selection.Copy
Doc.Bookmarks("Città").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(6, 8).Select
Selection.Copy
Doc.Bookmarks("pv").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Cells(15, 9).Select
Selection.Copy
Doc.Bookmarks("Nettomateriali").Select
Wrd.Selection.PasteAndFormat (wdFormatPlainText)
Doc.SaveAs Filename:="OFFERTA.doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
The run-time error occurs at the following line:
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(14), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(15.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Wrd.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(17.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
When I bring up the help for the run-time error, it's pretty vague
Hoping someone can help me solve this problem
THANKSSSSSSS
<!-- / message -->