Anthony Matovu
asked on
Cursor position in a word document created by vb.net
Iam using vb.net to write to a word document that has four columns. Some text I have to put it in a text box.
Problem:
I want to know the position of the cursor as i am writing such that I can position the the text box in the right place. (I want to position the left and top values of the box depending on the position of the cursor)
can any one help me out.
regards
Anthony Matovu
Problem:
I want to know the position of the cursor as i am writing such that I can position the the text box in the right place. (I want to position the left and top values of the box depending on the position of the cursor)
can any one help me out.
regards
Anthony Matovu
ASKER
Excuse me Graham,
wdApp.Selection.CreateText box
seems to work but the box doesn't show.
what do i have to add.
anthony Matovu
wdApp.Selection.CreateText
seems to work but the box doesn't show.
what do i have to add.
anthony Matovu
ASKER
Actually I want to insert textbox at current cursor position.
thanks
thanks
We still don't know any details about waht you are trying to do. Can you show what code you are currently using, so that we can fit it in?
This is an early-binding (set a reference to the Microsoft Word library) example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wdApp As New Word.Application()
Dim wdDoc As Word.Document
Dim rng As Word.Range
wdApp = CreateObject("Word.Applica tion")
wdApp.Visible = True
wdDoc = wdApp.Documents.Add
wdDoc.Range.Text = "Some text. Some for the TextBox"
rng = wdDoc.Range(11, 31)
rng.Select()
wdApp.Selection.CreateText box()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wdApp As New Word.Application()
Dim wdDoc As Word.Document
Dim rng As Word.Range
wdApp = CreateObject("Word.Applica
wdApp.Visible = True
wdDoc = wdApp.Documents.Add
wdDoc.Range.Text = "Some text. Some for the TextBox"
rng = wdDoc.Range(11, 31)
rng.Select()
wdApp.Selection.CreateText
End Sub
ASKER
I think I have attached too much code.
this thing creates a four colum document. populates it with data from a dataset reader (addresses). Put some text in a box. The solution above seems to work only that I can't come out of the box.
The size of the box matters to me much because some text has to be put in a bigger box and with a bigger font.
Dim cat As Integer
'Dim shpShape As Microsoft.Office.Interop.W ord.Shape
'Dim newTextbox As Microsoft.Office.Interop.W ord.Shape
Dim x, y, d As Int16
Dim WordApp As New Microsoft.Office.Interop.W ord.Applic ation()
Dim wdDoc As Microsoft.Office.Interop.W ord.Docume nt
Dim rng As Microsoft.Office.Interop.W ord.Range
'Town Classification ccode temail1 temail2 temail3 tweb tspace spotredyn Amount
' 0 1 2 3 4 5 6 7 8 9 10 11
tstr = "SELECT ccode,cname, plot, street, Building,POBox,town,Tel1,T el2,Tel3,f ax,mob" _
& " FROM [spaceOrderForm] order BY [ccode],[Town],[cname];"
WordApp = CreateObject("Word.Applica tion")
WordApp.Visible = True
wdDoc = WordApp.Documents.Add
With WordApp.ActiveDocument.Pag eSetup.Tex tColumns
.SetCount(NumColumns:=1)
.EvenlySpaced = False
.LineBetween = True
WordApp.Selection.PageSetu p.LeftMarg in = WordApp.CentimetersToPoint s(1.27)
WordApp.Selection.PageSetu p.RightMar gin = WordApp.CentimetersToPoint s(0.95)
.Add(Width:=WordApp.Centim etersToPoi nts(3.89), _
Spacing:=WordApp.Centimete rsToPoints (1.07), EvenlySpaced:=False)
.Add(Width:=WordApp.Centim etersToPoi nts(3.89), _
Spacing:=WordApp.Centimete rsToPoints (1.07), EvenlySpaced:=False)
.Add(Width:=WordApp.Centim etersToPoi nts(3.89), _
Spacing:=WordApp.Centimete rsToPoints (1.07), EvenlySpaced:=False)
End With
dcmd = New OleDb.OleDbCommand
dcmd.CommandText = tstr
dcmd.Connection = db
drd = dcmd.ExecuteReader
x = 1
y = 1
d = 0
While drd.Read
With WordApp.Selection
.Font.Size = 9
For i As Int16 = 0 To drd.FieldCount - 1
If i = 0 Then
If cat <> drd(i) Then
d = d + 1
.TypeParagraph()
wdDoc.Range.Text = returnName(drd(i))
rng = wdDoc.Range(0, 5)
rng.Select()
rng.SetRange(0, 5)
rng.Bookmarks.Add("x" & d)
rng = wdDoc.Range(6, 6)
rng.Select()
Application.DoEvents()
.TypeParagraph()
.TypeParagraph()
cat = drd(i)
End If
Else
y = y + 1
If y Mod 40 = 0 Then
x = x + 1
y = 1
End If
.Font.Size = 9
.Font.Bold = False
End If
If Not IsDBNull(drd(i)) Then
If Trim(drd(i).ToString).Leng th > 0 Then
If i = 5 Then
' wdDoc.Range.Text = "P.O.Box " & drd(i) & ", "
.TypeParagraph()
.TypeText("P.O.Box " & drd(i) & ", ")
ElseIf i = 7 Or i = 8 Or i = 9 Then
.TypeParagraph()
.TypeText("Tel............ .......... ...." & drd(i) & " ")
ElseIf i = 10 Then
.TypeParagraph()
.TypeText("Fax............ .......... ...." & drd(i) & " ")
ElseIf i = 1 Or i = 3 Or i = 4 Or i = 6 Or i = 11 Then
.TypeText(drd(i) & " ")
End If
If i = 1 Then .TypeParagraph()
End If
End If
Next
.TypeParagraph()
.TypeParagraph()
End With
End While
drd.Close()
************************** ****
Example of the output.
************************** ****
ABOTTOIR - THIS IS IN A BOX (ALSO SOME OF THE ADDRESSES SHD BE IN A BOX IF DIFF SIZES)
City Abattoir
Old Portbell Road
P.O.Box 1036, Kampala
Tel....................... ...254028
Uganda Meat Industries Ltd
Old Portbell Road
P.O.Box 180, Kampala
Tel....................... ...320940
Fax....................... ...230989
this thing creates a four colum document. populates it with data from a dataset reader (addresses). Put some text in a box. The solution above seems to work only that I can't come out of the box.
The size of the box matters to me much because some text has to be put in a bigger box and with a bigger font.
Dim cat As Integer
'Dim shpShape As Microsoft.Office.Interop.W
'Dim newTextbox As Microsoft.Office.Interop.W
Dim x, y, d As Int16
Dim WordApp As New Microsoft.Office.Interop.W
Dim wdDoc As Microsoft.Office.Interop.W
Dim rng As Microsoft.Office.Interop.W
'Town Classification ccode temail1 temail2 temail3 tweb tspace spotredyn Amount
' 0 1 2 3 4 5 6 7 8 9 10 11
tstr = "SELECT ccode,cname, plot, street, Building,POBox,town,Tel1,T
& " FROM [spaceOrderForm] order BY [ccode],[Town],[cname];"
WordApp = CreateObject("Word.Applica
WordApp.Visible = True
wdDoc = WordApp.Documents.Add
With WordApp.ActiveDocument.Pag
.SetCount(NumColumns:=1)
.EvenlySpaced = False
.LineBetween = True
WordApp.Selection.PageSetu
WordApp.Selection.PageSetu
.Add(Width:=WordApp.Centim
Spacing:=WordApp.Centimete
.Add(Width:=WordApp.Centim
Spacing:=WordApp.Centimete
.Add(Width:=WordApp.Centim
Spacing:=WordApp.Centimete
End With
dcmd = New OleDb.OleDbCommand
dcmd.CommandText = tstr
dcmd.Connection = db
drd = dcmd.ExecuteReader
x = 1
y = 1
d = 0
While drd.Read
With WordApp.Selection
.Font.Size = 9
For i As Int16 = 0 To drd.FieldCount - 1
If i = 0 Then
If cat <> drd(i) Then
d = d + 1
.TypeParagraph()
wdDoc.Range.Text = returnName(drd(i))
rng = wdDoc.Range(0, 5)
rng.Select()
rng.SetRange(0, 5)
rng.Bookmarks.Add("x" & d)
rng = wdDoc.Range(6, 6)
rng.Select()
Application.DoEvents()
.TypeParagraph()
.TypeParagraph()
cat = drd(i)
End If
Else
y = y + 1
If y Mod 40 = 0 Then
x = x + 1
y = 1
End If
.Font.Size = 9
.Font.Bold = False
End If
If Not IsDBNull(drd(i)) Then
If Trim(drd(i).ToString).Leng
If i = 5 Then
' wdDoc.Range.Text = "P.O.Box " & drd(i) & ", "
.TypeParagraph()
.TypeText("P.O.Box " & drd(i) & ", ")
ElseIf i = 7 Or i = 8 Or i = 9 Then
.TypeParagraph()
.TypeText("Tel............
ElseIf i = 10 Then
.TypeParagraph()
.TypeText("Fax............
ElseIf i = 1 Or i = 3 Or i = 4 Or i = 6 Or i = 11 Then
.TypeText(drd(i) & " ")
End If
If i = 1 Then .TypeParagraph()
End If
End If
Next
.TypeParagraph()
.TypeParagraph()
End With
End While
drd.Close()
**************************
Example of the output.
**************************
ABOTTOIR - THIS IS IN A BOX (ALSO SOME OF THE ADDRESSES SHD BE IN A BOX IF DIFF SIZES)
City Abattoir
Old Portbell Road
P.O.Box 1036, Kampala
Tel.......................
Uganda Meat Industries Ltd
Old Portbell Road
P.O.Box 180, Kampala
Tel.......................
Fax.......................
Because I didn't know whwer you want the textbox to be, I showed you one way creating it out of existing .
This method shows how to create a text box with particular size and position in terms of an Anchor range.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wdApp As New Word.Application()
Dim wdDoc As Word.Document
Dim rng As Word.Range
Dim tb As Word.Shape
wdApp = CreateObject("Word.Applica tion")
wdApp.Visible = True
wdDoc = wdApp.Documents.Add
wdDoc.Range.Text = "Some text." & vbCr & "Some more Text"
rng = wdDoc.Bookmarks.Item("\End OfDoc").Ra nge
tb = wdDoc.Shapes.AddTextbox(1, 30, 100, 168, 66, rng)
tb.TextFrame.TextRange.Tex t = "City(Abattoir)" & vbCr & "Old Portbell Road" & vbCr & "P.O.Box(1036, Kampala)" & vbCr & "Tel...................... ....254028 """
End Sub
I don't understand about not coming out of the text box. You just click elsewhere and the cursor moves to the new position.
This method shows how to create a text box with particular size and position in terms of an Anchor range.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wdApp As New Word.Application()
Dim wdDoc As Word.Document
Dim rng As Word.Range
Dim tb As Word.Shape
wdApp = CreateObject("Word.Applica
wdApp.Visible = True
wdDoc = wdApp.Documents.Add
wdDoc.Range.Text = "Some text." & vbCr & "Some more Text"
rng = wdDoc.Bookmarks.Item("\End
tb = wdDoc.Shapes.AddTextbox(1,
tb.TextFrame.TextRange.Tex
End Sub
I don't understand about not coming out of the text box. You just click elsewhere and the cursor moves to the new position.
I think my first sentence came out a bit more garbled than usual. Read it thus:
Because I didn't know where you want the textbox to be, I showed you a way of creating it around existing text
Because I didn't know where you want the textbox to be, I showed you a way of creating it around existing text
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Graham. This solves it
ASKER
I am thanking you agin Graham. It works very fine.
That's good to hear. Thanks. Good luck with the rest of your project.
wdApp.Selection.CreateText
However because it is simple, there is no control over parameters such as Left, Top, Height and Width which are all defaulted.
You will see that this will work without you knowing the details of the Seletion object. If your programme allows the user to set the selection, that may not be a problem. Otherwise it would have to be defined programatically.
You have told us that the document has four columns. Is there any other relevant information about the requirement?