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