I ahve abutton in my db that opens a Word document. Now I want to be able to run a macro in that document. the Word document is sent to me and I need to be able to import the data into the db.
Does anyone know how I can do this? The macro exports the data in Word to an excel sheet and saves it.
Open Word Document
----------------------------------
Dim appword As Word.Application
Dim worddoc As Word.Document
Set appword = Word.Application
appword.Visible = True
Set worddoc = appword.Documents.Open("C:\sales.doc")
Set worddoc = Nothing
Set appword = Nothing
---------------------------------
macro code:Exporttoexcel
---------------------------------
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Date: "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product Code"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.InsertColumns
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Date"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Paste
'Isert Office Information
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Office: "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product Code"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.InsertColumns
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Office"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Paste
'Convert Table to delimited text
Selection.Tables(1).Select
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.Tables(1).Select
Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
True
ActiveDocument.SaveAs FileName:="Canberra Sales.txt", FileFormat:= _
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
ActiveWindow.Close
Some people make things happen, some watch while things happen, and some wonder 'What happened?'
Does anyone know how I can do this? The macro exports the data in Word to an excel sheet and saves it.
Open Word Document
----------------------------------
Dim appword As Word.Application
Dim worddoc As Word.Document
Set appword = Word.Application
appword.Visible = True
Set worddoc = appword.Documents.Open("C:\sales.doc")
Set worddoc = Nothing
Set appword = Nothing
---------------------------------
macro code:Exporttoexcel
---------------------------------
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Date: "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product Code"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.InsertColumns
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Date"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Paste
'Isert Office Information
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Office: "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product Code"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.InsertColumns
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Office"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Paste
'Convert Table to delimited text
Selection.Tables(1).Select
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.Paste
Selection.HomeKey Unit:=wdStory
Selection.Tables(1).Select
Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
True
ActiveDocument.SaveAs FileName:="Canberra Sales.txt", FileFormat:= _
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
ActiveWindow.Close
Some people make things happen, some watch while things happen, and some wonder 'What happened?'