Editing header text shape size with VBA (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 07:28
Joined
Sep 21, 2011
Messages
10,881
Hi all,

I managed to create a sub that allows me to change text in the header of word documents. We moved address in work and there were so many to change, I decided to write the routine.

Now I need to use it on another set of letters in the same way.

Unfortunately all it does is swap text in the textbox shape.
Now I need to add an extra line. I believe I could do that with vbCRLF and with some text. However my manager wants to also enlarge the font from 8 to 10, and this then exceeds the size of the text shape.

So I believe I need to somehow change the size of the text shape, but cannot find the method/properties to do it.? I've looked at MarginTop, Autosize etc.
The text shape is also going to have to move to the left to accommodate the larger size and the header depth is also going to have to grow to accommodate the text box.

Would anyone be able to tell me the properties I could use to do the above please.?

TIA
 

sneuberg

AWF VIP
Local time
Yesterday, 23:28
Joined
Oct 17, 2014
Messages
3,506
You are probably looking for the TextFrame.TextRange properties. Here's some snippets from our code.

Code:
Dim doc As Word.Document
Dim What As Word.Shape
Set apWord = CreateObject("Word.application")
Set doc = apWord.Documents.Add("Normal", False, 0)
Set What = doc.Shapes.AddTextbox(msoTextOrientationHorizontal, doc.PageSetup.LeftMargin, 200, 240, 0)
What.TextFrame.AutoSize = True
What.Line.Visible = False
What.TextFrame.MarginLeft = 0 'This puts the text right on the margin
What.TextFrame.TextRange.Paragraphs.SpaceAfter = 0
What.TextFrame.TextRange.Font.Size = 12

I believe if I want to insert a vbCrLF I would add

Code:
What.TextFrame.TextRange.InsertParagraph

If you are wondering the strange name "What" for the textbox is because that's what the customer call the text in the document.
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:28
Joined
Sep 21, 2011
Messages
10,881
Thank you sneuberg,

I think I can see where I was going wrong with MarginLeft. I thought that was for the Left margin in relation to the page, not text to the shape.
Will try in work on Monday.
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:28
Joined
Sep 21, 2011
Messages
10,881
Sneuberg,
In my sub I just substitute text, with no relation to where the line is in the textbox.
Using that .InsertParagraph, how would I place the cursor prior to that call?
I know I could identify the position of a certain character, but how to place the cursor at that point.?

TIA
 

sneuberg

AWF VIP
Local time
Yesterday, 23:28
Joined
Oct 17, 2014
Messages
3,506
Sorry I shouldn't have suggested that without knowing how to tell you to place the cursor. I never really got a handle on that topic. It's got some to do with range and/so selection objects. I wish I could find a source that would explain this clearly. Anyway I putzed with the following working code that you can find in the attached database and have come to the conclusion that it might be easier for you just to put in a vbCrLf. I couldn't get anything else to work albeit I'm sure there's a way to do it.


Code:
Sub t()

Dim apWord As Word.Application
Dim doc As Word.Document
Dim What As Word.Shape
Set apWord = CreateObject("Word.application")
Set doc = apWord.Documents.Add("Normal", False, 0)
Set What = doc.Shapes.AddTextbox(msoTextOrientationHorizontal, 90, 90, 200, 200)


What.TextFrame.AutoSize = False
What.Line.Visible = True
What.TextFrame.MarginLeft = 0 'This puts the text right on the margin
What.TextFrame.TextRange.Paragraphs.SpaceAfter = 0
What.TextFrame.TextRange.Font.Size = 12
What.TextFrame.TextRange.InsertParagraph
What.TextFrame.TextRange.InsertAfter "The quick brown fox jumped over the lazy dog's back" & vbCrLf
'What.TextFrame.TextRange.Select
'What.TextFrame.TextRange.EndOf  wdStory, wdMove
'What.TextFrame.TextRange.Collapse
'What.TextFrame.TextRange.InsertParagraph
What.TextFrame.TextRange.InsertAfter "The quick brown monkey jumped over the lazy elephant's back"

apWord.Visible = True
apWord.Activate

End Sub
 

Attachments

  • WordTextBox.accdb
    340 KB · Views: 142

Gasman

Enthusiastic Amateur
Local time
Today, 07:28
Joined
Sep 21, 2011
Messages
10,881
sneuberg,

That is OK. You have given me some extra ideas though which would make life easier in the long run. :)
Instead of substituting text, I could now delete all the text in the text box and create my own. That way I should be able to control the cursor position?
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:28
Joined
Sep 21, 2011
Messages
10,881
Sneuberg,

Would have any idea on how to adjust the header depth to allow for the new font size?

.headerdistance does not appear to work

Sorry I shouldn't have suggested that without knowing how to tell you to place the cursor. I never really got a handle on that topic. It's got some to do with range and/so selection objects. I wish I could find a source that would explain this clearly. Anyway I putzed with the following working code that you can find in the attached database and have come to the conclusion that it might be easier for you just to put in a vbCrLf. I couldn't get anything else to work albeit I'm sure there's a way to do it.


Code:
Sub t()

Dim apWord As Word.Application
Dim doc As Word.Document
Dim What As Word.Shape
Set apWord = CreateObject("Word.application")
Set doc = apWord.Documents.Add("Normal", False, 0)
Set What = doc.Shapes.AddTextbox(msoTextOrientationHorizontal, 90, 90, 200, 200)


What.TextFrame.AutoSize = False
What.Line.Visible = True
What.TextFrame.MarginLeft = 0 'This puts the text right on the margin
What.TextFrame.TextRange.Paragraphs.SpaceAfter = 0
What.TextFrame.TextRange.Font.Size = 12
What.TextFrame.TextRange.InsertParagraph
What.TextFrame.TextRange.InsertAfter "The quick brown fox jumped over the lazy dog's back" & vbCrLf
'What.TextFrame.TextRange.Select
'What.TextFrame.TextRange.EndOf  wdStory, wdMove
'What.TextFrame.TextRange.Collapse
'What.TextFrame.TextRange.InsertParagraph
What.TextFrame.TextRange.InsertAfter "The quick brown monkey jumped over the lazy elephant's back"

apWord.Visible = True
apWord.Activate

End Sub
 

sneuberg

AWF VIP
Local time
Yesterday, 23:28
Joined
Oct 17, 2014
Messages
3,506
I just played with a header in a Word document and the plain blank one grows to accommodate a change in font size. Isn't yours doing that? If not what type of header to you have in the document and how did they fix the size on it.

Sometimes you can figure out what you need to do by doing it in Word while recording a macro. Then look at the macro code.
 

Users who are viewing this thread

Top Bottom