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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me with this error: Error 462 "Remote server machine does not exist..."

Status
Not open for further replies.

ceddins

Technical User
Jan 25, 2011
44
0
0
US
Hello all,

I've tried so many things to get this to work, but I cannot figure it out. Below is my latest code. I have a spreadsheet that pastes a table into Word and does some formatting to the Word document. I've never used VBA in Word before. The code always works the first time but breaks at this line any time I try to run it afterwards: WordDoc.PageSetup.TopMargin = InchesToPoints(0.5)
Code:
    'open a new Word document, format it, and paste the data in from Excel
    Set WordApp = Nothing
    Set WordDoc = Nothing
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    Set WordDoc = WordApp.Documents.Add
    WordDoc.Activate
    WordDoc.Application.ScreenUpdating = False
    WordDoc.PageSetup.Orientation = 1
    WordDoc.Paragraphs.SpaceAfter = 0
    WordDoc.ActiveWindow.Selection.Font.Name = "Times New Roman"
    WordDoc.ActiveWindow.Selection.Font.Size = 12
    WordDoc.ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
    WordDoc.PageSetup.TopMargin = InchesToPoints(0.5)
    WordDoc.PageSetup.BottomMargin = InchesToPoints(0.5)
    WordDoc.PageSetup.LeftMargin = InchesToPoints(0.5)
    WordDoc.PageSetup.RightMargin = InchesToPoints(0.5)
    WordDoc.PageSetup.HeaderDistance = InchesToPoints(0.2)
    WordDoc.PageSetup.FooterDistance = InchesToPoints(0.2)
    If WordDoc.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        WordDoc.ActiveWindow.Panes(2).Close
    End If
    If WordDoc.ActiveWindow.ActivePane.View.Type = wdNormalView Or WordDoc.ActiveWindow.ActivePane.View.Type = wdOutlineView Then
        WordDoc.ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    WordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    WordDoc.ActiveWindow.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    WordDoc.ActiveWindow.Selection.TypeText Text:="GUARANTEE"
    WordDoc.ActiveWindow.Selection.TypeParagraph
    WordDoc.ActiveWindow.Selection.TypeText Text:="GAYLORD OPRYLAND"
    WordDoc.ActiveWindow.Selection.TypeParagraph
    WordDoc.ActiveWindow.Selection.TypeText Text:=Format(wsPrep.Cells(2, 3), "DDDD MMMM DD, YYYY")
    WordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    wsOutput.Visible = xlSheetVisible
    wsOutput.UsedRange.Copy
    WordDoc.ActiveWindow.Selection.PasteExcelTable False, False, False
    Application.CutCopyMode = False
    wsOutput.Visible = xlSheetHidden
    WordDoc.ActiveWindow.Selection.Tables(1).Rows(1).HeadingFormat = wdToggle
    WordDoc.ActiveWindow.Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
    WordDoc.ActiveWindow.Selection.Tables(1).Rows.LeftIndent = InchesToPoints(0)
    WordDoc.ActiveWindow.Selection.Tables(1).Rows.AllowBreakAcrossPages = False
    WordDoc.ActiveWindow.Selection.Tables(1).LeftPadding = InchesToPoints(0)
    WordDoc.ActiveWindow.Selection.Tables(1).RightPadding = InchesToPoints(0)
    WordDoc.ActiveWindow.Selection.Tables(1).Spacing = 0
    WordDoc.ActiveWindow.Selection.Tables(1).Select
    WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceBefore = 0
    WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceBeforeAuto = False
    WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceAfter = 0
    WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceAfterAuto = False
    WordDoc.ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
    WordDoc.ActiveWindow.Selection.ParagraphFormat.LineUnitBefore = 0
    WordDoc.ActiveWindow.Selection.ParagraphFormat.LineUnitAfter = 0
    WordApp.Application.ScreenUpdating = True
    
Dim fd As FileDialog
    Set fd = WordApp.FileDialog(msoFileDialogSaveAs)
    With fd
        .InitialFileName = Format(wsPrep.Cells(2, 3), "M.D.YY") & " GTD Sheet"
        If .Show = 0 Then 'user pressed the cancel button

        Else
            .Execute
        End If
    End With
    Set fd = Nothing
    Set WordDoc = Nothing
    Set WordApp = Nothing

Thanks in advance for your help.
 
Since you use late binding with:
[tt]Set WordApp = CreateObject("Word.Application")[/tt]
and then use Word's constants:
[tt]WordDoc.PageSetup.TopMargin = [blue]InchesToPoints(0.5)[/blue][/tt]

Word (and Excel) will let you by first time around, but the second time will complain with this error. I know, I have done it myself many times. You will need to find out what to use instead of [tt][blue]InchesToPoints(0.5)[/blue][/tt] with late binding.

Also, do yourself a favor and do this:

Code:
With WordDoc
   .Activate
   .Application.ScreenUpdating = False
   .PageSetup.Orientation = 1
   .Paragraphs.SpaceAfter = 0
   With .ActiveWindow
        .Selection.Font.Name = "Times New Roman"
        .Selection.Font.Size = 12
   End With
    WordDoc.ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
   With PageSetup
      .TopMargin = InchesToPoints(0.5)
      ...
    End With
   ...
End With

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Hi Andrzejek, thanks for taking a look. I originally had used the With...End Withs, but I kept reading online regarding error 462 that it's best to type everything out, fully qualified.

Anyway, I cleaned the code up, not changing anything really, and it is all of a sudden working!?!? Here is the code that works:

Code:
    'open a new Word document, format it, and paste the data in from Excel
    Set WordApp = Nothing
    Set WordDoc = Nothing
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    Set WordDoc = WordApp.Documents.Add
    With WordDoc
        .Activate
        .Application.ScreenUpdating = False
        .Paragraphs.SpaceAfter = 0
        .ActiveWindow.Selection.Font.Name = "Times New Roman"
        .ActiveWindow.Selection.Font.Size = 12
        .ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
        With .PageSetup
            .Orientation = 1
            .TopMargin = InchesToPoints(0.5)
            .BottomMargin = InchesToPoints(0.5)
            .LeftMargin = InchesToPoints(0.5)
            .RightMargin = InchesToPoints(0.5)
            .HeaderDistance = InchesToPoints(0.2)
            .FooterDistance = InchesToPoints(0.2)
        End With
        If .ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            .ActiveWindow.Panes(2).Close
        End If
        If .ActiveWindow.ActivePane.View.Type = wdNormalView Or .ActiveWindow.ActivePane.View.Type = wdOutlineView Then
            .ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        With .ActiveWindow
            .ActivePane.View.SeekView = wdSeekCurrentPageHeader
            With .Selection
                .ParagraphFormat.Alignment = wdAlignParagraphCenter
                .TypeText Text:="GUARANTEE"
                .TypeParagraph
                .TypeText Text:="GAYLORD OPRYLAND"
                .TypeParagraph
                .TypeText Text:=Format(wsPrep.Cells(2, 3), "DDDD MMMM DD, YYYY")
            End With
            .ActivePane.View.SeekView = wdSeekMainDocument
            wsOutput.Visible = xlSheetVisible
            wsOutput.UsedRange.Copy
            .Selection.PasteExcelTable False, False, False
            Application.CutCopyMode = False
            wsOutput.Visible = xlSheetHidden
            With .Selection
                With .Tables(1)
                    .Rows(1).HeadingFormat = wdToggle
                    .Rows.Alignment = wdAlignRowCenter
                    .Rows.LeftIndent = InchesToPoints(0)
                    .Rows.AllowBreakAcrossPages = False
                    .LeftPadding = InchesToPoints(0)
                    .RightPadding = InchesToPoints(0)
                    .Spacing = 0
                    .Select
                End With
                With .ParagraphFormat
                    .SpaceBefore = 0
                    .SpaceBeforeAuto = False
                    .SpaceAfter = 0
                    .SpaceAfterAuto = False
                    .LineSpacingRule = wdLineSpaceSingle
                    .LineUnitBefore = 0
                    .LineUnitAfter = 0
                End With
            End With
        End With
    End With
    WordApp.Application.ScreenUpdating = True
    
Dim fd As FileDialog
    Set fd = WordApp.FileDialog(msoFileDialogSaveAs)
    With fd
        .InitialFileName = Format(wsPrep.Cells(2, 3), "M.D.YY") & " GTD Sheet"
        If .Show = 0 Then 'user pressed the cancel button

        Else
            .Execute
        End If
    End With
    Set fd = Nothing
    Set WordDoc = Nothing
    Set WordApp = Nothing
 
A lot easier to read (in my opinion) and possibly InchesToPoints(0.5) gets its meaning because of:
With WordDoc
(but that’s just my guess…)


Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Why not set up a TEMPLATE with all of that formatting set up, then just add a new document based on the template?
 
This is a known bug and you can google it to get more information. The solution is to fully reference everything. So inches to points is a Word function. So you would do
= WordApp.inchestopoints(.5)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top