Monday, August 10, 2009

MacroLabs | ActiveDocument vs Documents(filename)

There are two different ways of accessing the objects properties in your macro for a word document. The first one is to use the 'ActiveDocument' object and the other one is the 'Documents(filename)' where you have to specify the file name of the word document.

The difference between them is that the ActiveDocument refers to the Document which is active, as the name defines it. The other one refers to a document, with a specific filenanme, whether active or inactive.

If you are working with processing of large documents, it is recommended to use the 'Document(filename)' so that when you switch application windows, the macro will still work on the same document.

Take the following examples below which counts all tables inside the document:

-
ActiveDocument.Tables.Count
-
Document('c:\testing.doc').Tables.Count

If your active document refers to C:\testing.doc, it will return the same value. But if your active document refers to another document, the values to be returned will not be the same anymore.

MacroLabs | Converting Document Tables into Text

If you want to convert Microsoft Word Document to a Text file, this code might be able to help you. For example, your doc file contains tables and you want to convert it to formatted text file, follow the procedures below.

Private Sub ConvertTableToText()

Dim tTable As Table

For Each tTable In ActiveDocument.Tables
'this for each loop will check all tables in the document


tTable.ConvertToText Separator:="|", NestedTables:=True
'this will convert the table to text and each cell column will be divided by the "|" character
'you can also use other character to divide each cells

Next

End Sub


'Note: When you record your macro in Microsoft Word, it automatically selects a default separator for your ConvertToText function. Make sure you choose your own separator so that it would not encounter errors when used in other PC's

Friday, August 7, 2009

MacroLabs | Copying contents from a Text File to Document

If you want to create a macro that will copy the contents of a text file to your current document, follow the codes below.

Private Sub Document_Open()
Dim mFileName As String
Dim buffer As String
mFileName = "C:\macfiles\MacroLabs.txt"

Open mFileName For Input As #1
Do While Not EOF(1) ' loop in the file while there's something to read

Line Input #1, buffer ' read a line of text and assign it to variable buffer

Selection.TypeText buffer

Loop
Close #1

End Sub




Well basically, it will just copy the text contents of the text file to your document. You can add additional functions to process the contents and format it.

Wednesday, August 5, 2009

MacroLabs | Open another file using macro

If you want to open another file or your macro wants to process another file, use these codes below to open the 'Open Dialog Box'. THe file will open after you select it.

Private Sub Document_Open()
'This macro will open a new file using the 'Open Dialog Box'

Dim defDir As String
'variable used which refers to Default Directory you want to set

defDir = "C:\fts"
'you can specify your own directory here

With Application.Dialogs(wdDialogFileOpen)
Options.DefaultFilePath(Path:=wdDocumentsPath) = defDir

If .Display Then
Documents.Open FileName:=.Name
End If

End With
'you can write other codes here

End Sub



Note: Basically the default file type you can open is a ".doc" file.
You can set and filter the file types you want to open using another property.

MacroLabs | The "Document_Open()" Method

We usually want our macro to run as soon as we open our document. To make this happen we use the 'Document_Open()' method which automatically runs any instruction inside this method.

Private Sub Document_Open()
MsgBox "Hello World!"
End Sub


It will depend on you if you want to make it public or private. Just make sure you have typed this code in the 'ThisDocument' Module so that it will run the command when you open the document file.

We can still use modules in creating your macro, but if you need the call the method in the 'ThisDocument' object to make it automatically run.

MacroLabs | Creating Your First Macro

1. To create a new macro file, create a new document in your Microsoft Word Application.

2. Press [alt] + [F11] in your keyboard to open the Visual Basic Editor.

3. From the project list, Click 'ThisDocument' and type the codes below.

Private Sub Document_Open()
Dim msg As String
msg = "Hello World!"
MsgBox msg
End Sub


4. Click the Play Button to run the macro.


Note: If you are kinda familiar with Visual Basic, you would not find it hard to develop macro in MS Word.

Monday, August 3, 2009

MacroLabs | Introduction to Macro

Hi All,

First of all, Don't get confused with macro i'm gonna discuss for the rest of this blog. Basically, I am referring to the macro which you can used in some microsoft office applications.

I will post random items here which I know you could use for future references.

So What is macro? According to a website that I found, A macro is a series of command which can be performed repeatedly. It can make your job easier like processing documents and the like. In Microsoft office, there is a built in application of Visual Basic where you can code your macro program.

If you are a first time user of macro, you can use the record macro tool in the office application. But if you are not, you can go directly to the visual basic editor and do your macro.


Next Topic: Displaying Text in Word Macro

Introduction

Good day and Welcome to My Macro Laboratory.

In this blog i will share some macro codes you might need for future references. I hope this could help.
I will post here soon. Thank you!