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

Running Word Macros

Status
Not open for further replies.

BSando

Technical User
Joined
Jun 29, 2003
Messages
73
Location
AU
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?'
 
Hi,
Assuming that the Macro is already saved in the Word Document, once the Word Document is open you can run the macro from Access using...

Code:
appWord.Run ("Exporttoexcel")

After the

Code:
Set worddoc = appword.Documents.Open("C:\sales.doc")

Assuming the Macro name is Exporttoexcel (otherwise just substitute the correct name).



There are two ways to write error-free programs; only the third one works.
 
Thankyou.It works a treat.

Some people make things happen, some watch while things happen, and some wonder 'What happened?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top