[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3301
  • Last Modified:

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
0
Anthony Matovu
Asked:
Anthony Matovu
  • 7
  • 5
1 Solution
 
GrahamSkanCommented:
The text cursor position is provided via the Selection object. Ths simplest code to create a text-box uses it.

wdApp.Selection.CreateTextbox

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?
0
 
Anthony MatovuBusiness Analyst, MTN UgandaAuthor Commented:
Excuse me Graham,

wdApp.Selection.CreateTextbox
 seems to work but the box doesn't show.

what do i have to add.

anthony Matovu
0
 
Anthony MatovuBusiness Analyst, MTN UgandaAuthor Commented:
Actually I want to insert textbox at current cursor position.

thanks
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
GrahamSkanCommented:
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?
0
 
GrahamSkanCommented:
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.Application")
        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.CreateTextbox()
    End Sub
0
 
Anthony MatovuBusiness Analyst, MTN UgandaAuthor Commented:
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.Word.Shape
        'Dim newTextbox As Microsoft.Office.Interop.Word.Shape
        Dim x, y, d As Int16


        Dim WordApp As New Microsoft.Office.Interop.Word.Application()
        Dim wdDoc As Microsoft.Office.Interop.Word.Document
        Dim rng As Microsoft.Office.Interop.Word.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,Tel2,Tel3,fax,mob" _
               & " FROM [spaceOrderForm] order BY [ccode],[Town],[cname];"

        WordApp = CreateObject("Word.Application")
        WordApp.Visible = True
        wdDoc = WordApp.Documents.Add

        With WordApp.ActiveDocument.PageSetup.TextColumns
            .SetCount(NumColumns:=1)
            .EvenlySpaced = False
            .LineBetween = True
            WordApp.Selection.PageSetup.LeftMargin = WordApp.CentimetersToPoints(1.27)
            WordApp.Selection.PageSetup.RightMargin = WordApp.CentimetersToPoints(0.95)

            .Add(Width:=WordApp.CentimetersToPoints(3.89), _
         Spacing:=WordApp.CentimetersToPoints(1.07), EvenlySpaced:=False)
            .Add(Width:=WordApp.CentimetersToPoints(3.89), _
                 Spacing:=WordApp.CentimetersToPoints(1.07), EvenlySpaced:=False)
            .Add(Width:=WordApp.CentimetersToPoints(3.89), _
                 Spacing:=WordApp.CentimetersToPoints(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).Length > 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

0
 
GrahamSkanCommented:
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.Application")
        wdApp.Visible = True
        wdDoc = wdApp.Documents.Add
        wdDoc.Range.Text = "Some text." & vbCr & "Some more Text"
        rng = wdDoc.Bookmarks.Item("\EndOfDoc").Range
        tb = wdDoc.Shapes.AddTextbox(1, 30, 100, 168, 66, rng)
        tb.TextFrame.TextRange.Text = "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.
0
 
GrahamSkanCommented:
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
0
 
GrahamSkanCommented:
You can make the textbox height adjust automatically to fit the text.

    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.Application")
        wdApp.Visible = True
        wdDoc = wdApp.Documents.Add
        wdDoc.Range.Text = "Some text." & vbCr & "Some more Text"
        rng = wdDoc.Bookmarks.Item("\EndOfDoc").Range
        tb = wdDoc.Shapes.AddTextbox(1, 30, 100, 200, 10, rng)
        tb.TextFrame.TextRange.Text = "Uganda Meat Industries Ltd " & vbCr & "Old Portbell Road" & vbCr & "P.O.Box 180, Kampala" & vbCr & "Tel..........................320940" & vbCr & "Fax..........................230989"
        tb.TextFrame.AutoSize = True
    End Sub
0
 
Anthony MatovuBusiness Analyst, MTN UgandaAuthor Commented:
Thank you Graham. This solves it
0
 
Anthony MatovuBusiness Analyst, MTN UgandaAuthor Commented:
I am thanking you agin Graham. It works very fine.
0
 
GrahamSkanCommented:
That's good to hear. Thanks. Good luck with the rest of your project.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now