473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

MS Word properties that are read-only in VB.Net but not in VBA

Apologies if this is the wrong forum. I normally post in the Word Programming
group, but this seemed like more of a VB.Net question.

I'm moving code from VBA over to Visual Studio Express 2005 (Visual Basic)
while still learning about VB.Net. (Not a good combination, I know.)

One problem I'm seeing is that certain lines of code that work when run
inside Word fail in a VB.Net app because I'm trying to set a read-only
property. For example, when dealing with the Range object in Word, in the VBA
Editor, you can write this:

dim r as Range
[blah blah blah]
r.Characters.Last= "T"

This should make the last character in the range "r" a capital T. It works
in the VBA Editor with no errors, but in Visual Studio Express, it tells me
"Last is a read-only property."

I tried creating a variable:

dim strTmp as String
strTmp=r.Characters.Last
strTmp="T"

But that doesn't work, because it won't let you assign "Last" to a string.
Any ideas?

Again, I wasn't certain where to put this, but I saw other Microsoft Word
questions here and figured I'd add my own. Thanks in advance!
Aug 21 '07 #1
4 2722
Solved by the following, but I'm still curious to understand what's really
going on.

Dim rLastChar As Word.Range
rLastChar = r.Characters.Last
rLastChar.Text = "T"

"Benjamino5" wrote:
Apologies if this is the wrong forum. I normally post in the Word Programming
group, but this seemed like more of a VB.Net question.

I'm moving code from VBA over to Visual Studio Express 2005 (Visual Basic)
while still learning about VB.Net. (Not a good combination, I know.)

One problem I'm seeing is that certain lines of code that work when run
inside Word fail in a VB.Net app because I'm trying to set a read-only
property. For example, when dealing with the Range object in Word, in the VBA
Editor, you can write this:

dim r as Range
[blah blah blah]
r.Characters.Last= "T"

This should make the last character in the range "r" a capital T. It works
in the VBA Editor with no errors, but in Visual Studio Express, it tells me
"Last is a read-only property."

I tried creating a variable:

dim strTmp as String
strTmp=r.Characters.Last
strTmp="T"

But that doesn't work, because it won't let you assign "Last" to a string.
Any ideas?

Again, I wasn't certain where to put this, but I saw other Microsoft Word
questions here and figured I'd add my own. Thanks in advance!
Aug 21 '07 #2
"Benjamino5" <Be********@discussions.microsoft.comschrieb
Apologies if this is the wrong forum. I normally post in the Word
Programming group, but this seemed like more of a VB.Net question.

I'm moving code from VBA over to Visual Studio Express 2005 (Visual
Basic) while still learning about VB.Net. (Not a good combination, I
know.)

One problem I'm seeing is that certain lines of code that work when
run inside Word fail in a VB.Net app because I'm trying to set a
read-only property. For example, when dealing with the Range object
in Word, in the VBA Editor, you can write this:

dim r as Range
[blah blah blah]
r.Characters.Last= "T"
The type of the Last property is Range. "T" is a string. You can not assign
a string to a range property. Maybe you wanted to write

r.Characters.Last.Text = "T"
Armin

Aug 21 '07 #3
"Benjamino5" <Be********@discussions.microsoft.comschrieb
Solved by the following, but I'm still curious to understand what's
really going on.

Dim rLastChar As Word.Range
rLastChar = r.Characters.Last
rLastChar.Text = "T"

Do you know what Default properties are? "Text" is the default property of
the Range class. In VB.Net, Default properties without arguments are not
supported. You must explicitly refer to the property. It wasn't clear
whether you wanted to assign something to the Last property or to the
Default property of the Last property. In VBA, it's not as strict, which has
the disadvantage that the code is not as clear (as your question shows), and
it has performance drawbacks because it has to find the default property at
runtime, then assign the value or raise an error if there wasn't a Default
property at all. VB.Net eliminates this cause of fault before, at compile
time.
Armin

Aug 21 '07 #4
Armin,

I'd heard vaguely of this issue but hadn't fully understood it. Thanks! I'm
going to spend a lot more time looking into default properties from now on. I
see what you mean about this reducing problems and improving performance.

Ben

"Armin Zingler" wrote:
"Benjamino5" <Be********@discussions.microsoft.comschrieb
Apologies if this is the wrong forum. I normally post in the Word
Programming group, but this seemed like more of a VB.Net question.

I'm moving code from VBA over to Visual Studio Express 2005 (Visual
Basic) while still learning about VB.Net. (Not a good combination, I
know.)

One problem I'm seeing is that certain lines of code that work when
run inside Word fail in a VB.Net app because I'm trying to set a
read-only property. For example, when dealing with the Range object
in Word, in the VBA Editor, you can write this:

dim r as Range
[blah blah blah]
r.Characters.Last= "T"

The type of the Last property is Range. "T" is a string. You can not assign
a string to a range property. Maybe you wanted to write

r.Characters.Last.Text = "T"
Armin

Aug 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str'...
1
by: Ken | last post by:
Hi, I've written a C# app in VS .Net 2003 that gets and sets custom/summary properties of Word documents by selecting a directory and get/setting values using BuiltInDocumentProperties and...
8
by: Asma | last post by:
Dear Sir, I am trying to find a way to open a Word document using C language and read the text of word doc into a variable. (Turbo C on Dos 6.0). Can anyone please tell me which libraries in...
4
by: Daniel | last post by:
Hello, i have a problem with the word automation from c#. First, i want to mention, that i don't have any dependencies from word in my c#-project, i want to use the system.reflection model to...
3
by: gabe | last post by:
I have a directory of word docs (maybe 2 or 3 hundred), all of the docs are based on a custom template and they have 20 or so fields, I would like to read all of the fields into a db. Any help...
3
by: Niyazi | last post by:
Hi, Its like a I am searching alot but still not found or satisfy what I found it. My question is that I had SQL server that contains some data. I also have a application folder call...
6
by: royan | last post by:
Help please! I have the same problem which this post ...
3
by: Maurizio | last post by:
I create an application with Access 2000 and I wish to read/write the properties of a specified word document. I use dsofile.dll my code is Function fGetDocProps(strInFile As String, strProp As...
10
by: annalisa | last post by:
I really need some help with langauge conversion to HTML. My translators are translating into Word and I need to convert Word to HTML. It's been awhile since I've worked with Unicode and know that...
3
by: brat33 | last post by:
I am trying to use a command button to automate a process for creating mailing labels from a query in Access 2007, going into word 2007. Current code looks like the following: 'Start MS Word Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.