Consulting

Results 1 to 3 of 3

Thread: Word VBA Macro Can only be Used Once

  1. #1

    Word VBA Macro Can only be Used Once

    Hello everyone,

    I am very new to vba in word. I am writing some code from an excel spreadsheet that exports an AutoCAD Script file from an imported CSV file. The code is designed to take a file and remove any unwanted commas and format spaces where required. I have some simple error handling but its by no means ideal.

    I have loaded the word libraries into my excel macro and everything works fine the first time I run it. Every other time an error occurs. I think there must be something running in the background or invisible that i have not closed down but i have no Idea what.

    Any help would be greatly appreciated.


    Sub Word_Munipulation()
    On Error GoTo Errhandler
    '
    ' Word_Munipulation Macro
    '
    Dim MyPath As String, MyCompletePath As String
    MyPath = ActiveWorkbook.Path
    'Finds the path of the current excel sheet in order to save the scr file next to it'

    Set wordapp = CreateObject("word.Application")

    wordapp.Documents.Open (MyPath & "\Autocad_Iso.csv")

    wordapp.Visible = True
    On Error Resume Next

    Word.ActiveDocument.Activate

    Word.ActiveDocument.Range.Select

    Word.Selection.find.ClearFormatting

    Word.Selection.find.Replacement.ClearFormatting
    On Error GoTo Errhandler

    With Word.Selection.find
    .Text = "line,"
    .Replacement.Text = "line"
    .Forward = True
    .Wrap = wdFindAsk
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Word.Selection.find.Execute Replace:=wdReplaceAll
    With Word.Selection.find
    .Text = "*cancel*,"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Word.Selection.find.Execute Replace:=wdReplaceAll


    Word.ChangeFileOpenDirectory MyPath
    ActiveDocument.SaveAs2 Filename:="Autocad_Iso.scr", FileFormat:= _
    wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False, Encoding:=1252, InsertLineBreaks:=False, AllowSubstitutions:=False _
    , LineEnding:=wdCRLF, CompatibilityMode:=0

    Word.Application.Quit

    Exit Sub

    Errhandler:

    MsgBox "An unknown error occurred, please close word and try again"

    End Sub

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "Every other time an error occurs."

    When reporting an error please state what the error is, including the text of the error.

    There are some things possibly wrong, but let's start with what exactly is the error you are getting.

  3. #3
    Hi sorry,

    The error message is that the remote server machine does not exist or is unavailable. (rte 462).
    Every other time was a bit vague. What i mean is that the first time it is run it works perfectly. Ever time after that it will come up with this error.

    Thanks for your quick reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •