Before you put your macro files into production, make sure there ain't no bugs or problems to be encountered in the future. But who knows, no system is perfect and we should be prepared to face these things.
Common errors encountered in some types of macro or in any other programs are the 'Index out-of-bound' where usually it occurs in arrays. Check your program which part uses arrays and check the array size in your declaration.
In looking for errors or possible bugs, you must have a keen eye in checking each line so that you wouldn't miss the line and you must understand the logic of the program. There must be a connection with the program and the programmer.
Saturday, February 13, 2010
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.
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.
Labels:
active document,
document open,
filename,
macro
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
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.
Private Sub Document_Open()
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.
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.
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.
Subscribe to:
Comments (Atom)
