PDA

View Full Version : Very Newbie: Creating Text Box in Word



shlomiassaf
08-20-2007, 06:32 PM
Hi, I am very new to Word VBA, Used Excel before but not I need to make a little output to a Word document.

This must be put as a text box (Same text box as INSERT -> Text Box)

I have the the text ready to be outputed in my program.

I just need the simple code that:

1) Opens a new word document
2) Creates a Text Box at the Top of the decomunet in the middle ( It is not importent to be accurate)
3) Paste the string that is in the variable strAddressData to the text box
4) Make the word document visible.

Thats it, if I will have this simple code of opening the document and creating the text box it will be easy to proceed alone.

If someone will help me on this I will appreciate if he will start from the start, meaning if I will just copy this to a new project and run it it will do it (Assuming I will put data into strAddressData manually)

I KNOW it is a very "newbie" question but I do not have any reference to word VBA and I could not find any example who creates a text box.

Thanks,
Shlomi

geekgirlau
08-20-2007, 08:53 PM
Welcome to the Board :wavey:


Sub StartNew()
Dim objShape As Shape

Documents.Add DocumentType:=wdNewBlankDocument

Set objShape = ActiveDocument.Shapes.AddTextbox _
(Orientation:=msoTextOrientationHorizontal, _
Left:=100, Top:=100, Width:=65, Height:=65)
With objShape
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wdShapeCenter
.Top = wdShapeTop

' the variable "strAddressData" needs to be populated
' before you get here
.TextFrame.TextRange = strAddressData
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End Sub


Don't apologise for being a newbie - we all had to start somewhere!

shlomiassaf
08-20-2007, 09:07 PM
WOW! quick reply! thanks.

I tryed it but It wont work, I guess i miss some addon or something.

Here the message:

"Compile Error:
Method or data member not found."

This goes for each one of the line codes within the WITH.

If I ignore all the WITH and just want to create an empty TextBox with no
positioning Then I get the error:

"Run Time Error 424
Object Requiered."

I dont know, never had this problem with VBA for Excel.

I just opened a new project and added the code down, maybe I am missing an addon

CODE:


Private Sub Command1_Click()
Dim objShape As Shape

Documents.Add DocumentType:=wdNewBlankDocument

Set objShape = ActiveDocument.Shapes.AddTextbox _
(Orientation:=msoTextOrientationHorizontal, _
Left:=100, Top:=100, Width:=65, Height:=65)
With objShape
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wdShapeCenter
.Top = wdShapeTop

' the variable "strAddressData" needs to be populated
' before you get here
.TextFrame.TextRange = "TESTTTTTTTTTTTTTING"
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With

End Sub


Sorry for nagging, but whats missing?

geekgirlau
08-20-2007, 09:33 PM
Is this code for Excel? You've posted in the Word forum, so the example I've provided is for Word ...

shlomiassaf
08-20-2007, 09:34 PM
No this is for Word.

Just noted that had no problem with excel...

So what do you think is the problem? why do I get this message?

Thanks.
Shlomi

geekgirlau
08-20-2007, 10:28 PM
Which line is causing the error? What version of Word are you running?

shlomiassaf
08-20-2007, 10:32 PM
Im using VB 6
Word version is 2002 SP3

Each of this lines:

.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wdShapeCenter
.Top = wdShapeTop
.TextFrame.TextRange = "TESTTTTTTTTTTTTTING"
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter


Will give this error:

"Compile Error:
Method or data member not found."



If we will delete whole of the positioning section we will get this error:
"Run Time Error 424
Object Requiered."


I believe it is some kind of addon I need to add to the project, but I am not expert.

Please try to help me this is a very importent code for me it will save me a lot of hours...

geekgirlau
08-20-2007, 10:50 PM
When you say VB6, are you trying to run the code in Visual Basic 6, or pasting it into a Word module and running it from there?

shlomiassaf
08-20-2007, 11:09 PM
I am trying to run it inside VB6 :doh:

I tought it is like using Excel VBA

What should I do then?

geekgirlau
08-20-2007, 11:31 PM
This is untested, but what's missing is the Word object. You will need a reference to the Microsoft Word Object Library to make this work.


Sub StartNew()
Dim objWd As Word.Application
Dim objShape As Word.Shape


Set objWd = CreateObject("", "Word.Application")

With objWd
.Visible = True
.Documents.Add DocumentType:=wdNewBlankDocument

Set objShape = .ActiveDocument.Shapes.AddTextbox _
(Orientation:=msoTextOrientationHorizontal, _
Left:=100, Top:=100, Width:=65, Height:=65)
With objShape
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wdShapeCenter
.Top = wdShapeTop

' the variable "strAddressData" needs to be populated
' before you get here
.TextFrame.TextRange = strAddressData
.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End With

Set objShape = Nothing
Set objWd = Nothing
End Sub

shlomiassaf
08-21-2007, 07:59 AM
Thanks, Managed to work it out thanks to your help!

Now I have another problem, my system is set to hebrew and the text file I get is a simple text file (not rich) with the standard Western Europe ASCII characters.

Until today I Opened the file with word and it told me that word has found that this is coded in Western Univode characters, would you to open it with that character code, I pressed YES and I saw the right letters.

However if i opened it with Notepad I saw hebrew characters where there were special non english charactrs, like special spanish or france or german letters.

Now, when I want to automate the whole thing I need to convert my string to take that hebrew Ascii chart and convert it to the right one

And special command for that?

I know it is not the forum for that, so if you do not have an answer thats ok, a reference to an other site will be great as well.

Thanks for the great help
Shlomi

geekgirlau
08-21-2007, 05:27 PM
Nope - don't have a clue :confused3

Given that this is a new topic, I would suggest you start a new post for this request with a heading that clearly describes your question - you'll be much more likely to get a response that way.

shlomiassaf
08-21-2007, 05:53 PM
Thanks for the help!

You were great!

I appreciate it!

Shlomi