Type Change

F

Fred

Hello,

Does anyone know who to change a string to an integer in
VB? We have a DB which exports a bunch of stuff into
Word, which we then format and move stuff around. One of
the exports is a Time for when an action was done, say
1100. What I need to do is subtract times to come up
with the total time worked, so if the next time input was
1200, I want to subtract the two and get 1 hour.
However, it seems that once the information is exported
into Word, the 1100 and 1200 become strings rather than
reamining integers.

Any help would be great.
 
J

Jezebel

VBA has a set of functions for this: Val() or CLng() will meet your current
needs. For reliable coding you need to provide error-handling in case the
string is not convertible; and be aware that some unusual strings will
convert in unexepected ways.

Or for a quick-and-dirty method use variants and do your arithmetic with
them.
 
F

Fred

Thank you Jezebel, I think Val is exactly what I need.
May I ask one other question? I'm need to select a few
numbers out of a Table in Word, however with the way I'm
currently doing that, I think I'm not only selecting the
contents, but some other garbage with it. Here's what
I'm doing to select the number out of a cell in a table:

ActiveDocument.Tables(1).Cell(1, 2).Select
Start = Selection.Text

I noticed that this seems to not only get the number out
of the cell, but it seems to have some other non-readable
stuff that comes along with it. What's the better way to
select a number out of a cell in a table in Word?

Thanks, really appreciate it.

Fred
 
C

Chad DeMeyer

ActiveDocument.Tables(1).Cell(1,2).Select
Selection.MoveLeft Extend:=wdExtend
Start = Selection.Text

Regards,
Chad DeMeyer
 
J

Jezebel

Cells always contain two additional characters at the end: a paragraph and a
cell mark. You can use the Left() function to strip these out.

As a general guide, avoid using the Selection object in your code. You can
always work directly with the underlying objects, which is both more
reliable, and easier on the user:

pVal = ActiveDocument.Tables(1).Cell(1,2).Range
pVal = Left(pVal, Len(pVal)-2)

Also avoid using variables with names like 'Start' because in some contexts
this word has special meaning; and at the least it's confusing - there are
several objects that have a property with this name. Do Google on 'variable
naming conventions' and pick one to follow. Doesn't much matter which. It's
the consistency that's valuable.
 

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