Go Back   Access World Forums > Apps and Windows > Word

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 09-16-2016, 09:37 AM   #1
Gasman
Enthusiastic Amateur
 
Gasman's Avatar
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 4,563
Thanks: 442
Thanked 847 Times in 818 Posts
Gasman is a jewel in the rough Gasman is a jewel in the rough Gasman is a jewel in the rough
Editing header text shape size with VBA

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

__________________
Access novice. Sometimes trying to give something back.
Access 2007

Please, please use code tag # when posting code snippets

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
Old 09-16-2016, 06:37 PM   #2
sneuberg
AWF VIP
 
Join Date: Oct 2014
Location: Tucson, Arizona
Posts: 3,506
Thanks: 362
Thanked 976 Times in 943 Posts
sneuberg will become famous soon enough sneuberg will become famous soon enough
Re: Editing header text shape size with VBA

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.
__________________
And now nothing will be restrained from them, which they have imagined to do. Genesis 11:6

Steve
sneuberg is offline   Reply With Quote
Old 09-16-2016, 11:28 PM   #3
Gasman
Enthusiastic Amateur
 
Gasman's Avatar
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 4,563
Thanks: 442
Thanked 847 Times in 818 Posts
Gasman is a jewel in the rough Gasman is a jewel in the rough Gasman is a jewel in the rough
Re: Editing header text shape size with VBA

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.

__________________
Access novice. Sometimes trying to give something back.
Access 2007

Please, please use code tag # when posting code snippets

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
Old 09-17-2016, 10:45 AM   #4
Gasman
Enthusiastic Amateur
 
Gasman's Avatar
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 4,563
Thanks: 442
Thanked 847 Times in 818 Posts
Gasman is a jewel in the rough Gasman is a jewel in the rough Gasman is a jewel in the rough
Re: Editing header text shape size with VBA

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
__________________
Access novice. Sometimes trying to give something back.
Access 2007

Please, please use code tag # when posting code snippets

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
Old 09-17-2016, 11:47 AM   #5
sneuberg
AWF VIP
 
Join Date: Oct 2014
Location: Tucson, Arizona
Posts: 3,506
Thanks: 362
Thanked 976 Times in 943 Posts
sneuberg will become famous soon enough sneuberg will become famous soon enough
Re: Editing header text shape size with VBA

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
Attached Files
File Type: accdb WordTextBox.accdb (340.0 KB, 59 views)
__________________
And now nothing will be restrained from them, which they have imagined to do. Genesis 11:6

Steve
sneuberg is offline   Reply With Quote
Old 09-17-2016, 11:02 PM   #6
Gasman
Enthusiastic Amateur
 
Gasman's Avatar
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 4,563
Thanks: 442
Thanked 847 Times in 818 Posts
Gasman is a jewel in the rough Gasman is a jewel in the rough Gasman is a jewel in the rough
Re: Editing header text shape size with VBA

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?
__________________
Access novice. Sometimes trying to give something back.
Access 2007

Please, please use code tag # when posting code snippets

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
Old 09-21-2016, 05:03 AM   #7
Gasman
Enthusiastic Amateur
 
Gasman's Avatar
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 4,563
Thanks: 442
Thanked 847 Times in 818 Posts
Gasman is a jewel in the rough Gasman is a jewel in the rough Gasman is a jewel in the rough
Re: Editing header text shape size with VBA

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

Quote:
Originally Posted by sneuberg View Post
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

__________________
Access novice. Sometimes trying to give something back.
Access 2007

Please, please use code tag # when posting code snippets

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
Old 09-21-2016, 05:56 AM   #8
sneuberg
AWF VIP
 
Join Date: Oct 2014
Location: Tucson, Arizona
Posts: 3,506
Thanks: 362
Thanked 976 Times in 943 Posts
sneuberg will become famous soon enough sneuberg will become famous soon enough
Re: Editing header text shape size with VBA

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.

__________________
And now nothing will be restrained from them, which they have imagined to do. Genesis 11:6

Steve
sneuberg is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to save text with text format like font size , type .. in vb6 with data1 in mdb midosoft Modules & VBA 3 03-25-2013 04:31 AM
How to increase Report header size baba Reports 7 11-24-2011 08:05 AM
Text box, adding but no editing tjcinnamon Forms 1 01-31-2006 04:22 PM
[SOLVED] Editing text in a report susan gardner General 3 10-05-2005 01:40 PM
Editing text files? -td-shadow Modules & VBA 4 07-25-2002 11:49 AM




All times are GMT -8. The time now is 04:14 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World