Inserting Date into table

M

MarkN

Hello,

I am new to Word VBA and would like some help with a part of a macro. I need
the code to check whether the first cell of the first table in the document
is empty and if it is empty, enter the current date.
 
G

Greg Maxey

Here is one way:

Sub ScratchMacro()
If Len(ActiveDocument.Tables(1).Cell(1, 1).Range.Text) = 2 Then
ActiveDocument.Tables(1).Cell(1, 1).Range.Text = Date
End If
End Sub
 
M

MarkN

Thanks Greg,

Could I ask why the Len function =2 is used as the test in your IF function?
 
G

Greg Maxey

Yes,

The end of cell marker in and empty cell takes up 2 characters. I wouldn't
swear in a court of law, but I am pretty sure it is chr(13) & chr(7).
 
G

Greg Maxey

Now that I have been duly sworn , it is chr(13) and chr(7). Select an
empty cell and run the following code:

Sub Test()
MsgBox Asc(Selection.Text) _
& " and " & Asc(Right(Selection.Text, 1))
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