How can I make VBA to run Microsoft word macro?

B

bufuro

Hi. I made a macro in Microsoft Word.

But I need to create a VBA which does the macro.

So what I have to create is, make a program that imports user's word
document, and run the macro.

Can any of you help me on this? I mean how can I transform the macro into
VBA?



following is my macro.

----------------------------
Sub breakapart()
' splitter Macro



Dim mask As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
mask = "ddMMyy"

Selection.HomeKey Unit:=wdStory
Counter = 1
FileId = " "
FileId2 = " "
While Counter < Letters

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
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
FileId = Selection
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
FileId2 = Selection
DocName = "C:\Progress Report\students\" & FileId
DocName2 = "C:\Progress Report\advisors\" & FileId2
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
ActiveWindow.Close
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=DocName2, FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Wend

End Sub
 
B

bufuro

Then can I copy the macro code to Visual Basic .NET and make a program?
Don't I need to import somekind of microsoft word reference?
 
H

Howard Kaikow

Moving VBA code to either VB or VB .NET requires learning a bit about
Automating Word from another app.

You have to ask at least the following questions:

1. Is the goal to create a library of code that can be referenced from
within Word?
2. Is the goal to have a stand-alone program that controls Word?
 
B

bufuro

I think my goal is to have a stand-alone program. Is that possible?

(Thank you so much for all your help)
 
H

Howard Kaikow

There is not really such a thing as a stand-alone Word program.

But, you can write your program in, e.g, VB 6 or VB .NET, and control Word
from that program.

For example, the following is a VB 6 program that works in Word 97 and up
that uses Word and Excel. In this case, neither Word nor Excel is made
visible to the user.

http://www.standards.com/index.html?Sorting

In another app, the VB program could act as a front-end for starting, say,
word or Excel, and, as needed, cleanup after itself.

Or you could put the code in a DLL and Word can use the code in the DLL.

For example, the following is ALL the code in my Normal template, all the
real code is in a number of VB 6 compiled DLLs:

Option Explicit
Public clsWordVBNormal As WordVBNormal

Public Sub AutoClose()
SetupClass
clsWordVBNormal.AutoClose
End Sub

Public Sub AutoExec()
' Runs only when global
SetupClass
clsWordVBNormal.AutoExec
End Sub

Public Sub AutoExit()
SetupClass
clsWordVBNormal.AutoExit
End Sub

Public Sub AutoNew()
SetupClass
clsWordVBNormal.AutoNew
End Sub

Public Sub AutoOpen()
SetupClass
clsWordVBNormal.AutoOpen
End Sub

Private Sub SetupClass()
Dim docTemp As Word.Document
If clsWordVBNormal Is Nothing Then
If Documents.Count = 0 Then
Set docTemp = Documents.Add(Visible:=vbFalse)
End If
Set clsWordVBNormal = New WordVBNormal
clsWordVBNormal.SetClass Word.Application
If Not (docTemp Is Nothing) Then
docTemp.Close
Set docTemp = Nothing
End If
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit

Public Sub ResetToolsOptionsView()
clsWordVBNormal.ResetToolsOptionsView
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top