Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-13-2015, 08:51 AM
peligroso peligroso is offline vba find numbers in document and align text left Windows 8 vba find numbers in document and align text left Office 2013
Novice
vba find numbers in document and align text left
 
Join Date: Sep 2015
Posts: 6
peligroso is on a distinguished road
Default vba find numbers in document and align text left

I am calling a embedded word document from excel and populating it with tables from excel with financial data to produce a report.

When the tables are copied into word the numbers contain leading spaces that are spoiling the formatting. I have written the code to strip out the leading spaces however the data is still left justified rather than right justified.

I am trying to use the following code using wildcards to locate any financial numbers and left justify them however I am off the mark as the code 1) removes the financial numbers and 2) does not justify.

With wrdObj


.Content.SelectAll
With .Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "*.[0-9][0-9]"
.MatchWildcards = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
If .Found = True Then .Parent.Font.Alignment = wdAlignParagraphRight
End With
End with


Any help appreciated.
Reply With Quote
  #2  
Old 09-13-2015, 03:25 PM
macropod's Avatar
macropod macropod is offline vba find numbers in document and align text left Windows 7 64bit vba find numbers in document and align text left Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

How are you updating the table? The insertion of leading spaces that aren't in the source data is not the normal behaviour. Furthermore, if the tables are formatted as right-aligned before updating, adding data to the cells, or changing what's already there, won't change the alignment.

There's a couple of problems with your code, but we'll address those if necessary after sorting out the data issues.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-14-2015, 09:56 PM
peligroso peligroso is offline vba find numbers in document and align text left Windows 8 vba find numbers in document and align text left Office 2013
Novice
vba find numbers in document and align text left
 
Join Date: Sep 2015
Posts: 6
peligroso is on a distinguished road
Default

Hi macropod, thanks so much for the response. I will admit that I am the worst type of programmer (IT exec playing as required to get reports out) so I am happy to hear any feedback, good or bad.

To answer your question, the process is to select a range in excel that contains the table data and then do a search for a placeholder in the word document. For example:

Range("dep_comparison_table").Copy
wrdObj.Selection.Find.Text = "<dep_comparison_table>"
Call FormatPaste
wrdObj.Selection.MoveLeft Unit:=wdCharacter, Count:=20
wrdObj.Selection.Tables(1).AllowAutoFit = False
wrdObj.Selection.Tables(1).PreferredWidth = wrdObj.CentimetersToPoints(18.4)

Sub FormatPaste()

wdFind.Replacement.Text = ""
wdFind.Forward = True
wdFind.Wrap = wdFindContinue
wdFind.Execute
Call CheckClipBrd
wrdObj.Selection.Paste
CutCopyMode = False

End Sub

After all the tables are copied the tables are formatted with the following:

For Each Table In wrdDoc.Tables
Table.Select
For i = 1 To Table.Rows.Count
Table.Rows(i).Range.ParagraphFormat.KeepTogether = True
If i < Table.Rows.Count Then
Table.Rows(i).Range.ParagraphFormat.KeepWithNext = True
End If
Next i
Next Table
Reply With Quote
  #4  
Old 09-14-2015, 10:18 PM
Guessed's Avatar
Guessed Guessed is offline vba find numbers in document and align text left Windows 7 32bit vba find numbers in document and align text left Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

FWIW, I think the leading spaces is normal behaviour with Excel content pasted into Word depending on the cell formatting applied in Excel.

How are you finding the leading spaces to remove them? A quick fix to remove leading spaces is to centre-align so you could kill two birds with the one stone if you wanted by following that with a right-align.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 09-14-2015, 10:20 PM
macropod's Avatar
macropod macropod is offline vba find numbers in document and align text left Windows 7 64bit vba find numbers in document and align text left Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

A few questions:
1. Why an embedded Word document, rather than a Word template stored in the same folder as the workbook?
2. Does the Excel table always span the same range?
3. Why does your last code block loop through all tables in the document instead of processing just the one that's being inserted/updated?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 09-14-2015, 10:31 PM
macropod's Avatar
macropod macropod is offline vba find numbers in document and align text left Windows 7 64bit vba find numbers in document and align text left Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Guessed View Post
FWIW, I think the leading spaces is normal behaviour with Excel content pasted into Word depending on the cell formatting applied in Excel.
When I paste an Excel range containing both text numeric data into Word, the data retain the same formatting and alignments they had in Excel and there are no leading/trailing spaces. In this regard, I note the OP is just using an ordinary Paste, not Word's PasteExcelTable or PasteAndFormat methods, for example.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 09-14-2015, 10:47 PM
Guessed's Avatar
Guessed Guessed is offline vba find numbers in document and align text left Windows 7 32bit vba find numbers in document and align text left Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Paul
On my machine, cells formatted as 'Accounting' brings a leading space into Word with an ordinary paste.

I remember having seen other cell formats that also bring multiple leading spaces but I believe this didn't happen with left aligned text.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 09-14-2015, 11:21 PM
macropod's Avatar
macropod macropod is offline vba find numbers in document and align text left Windows 7 64bit vba find numbers in document and align text left Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

With the 'Accounting, format you'll get either a single space or - before the $ symbol, depending on whether the number is +ve or -ve. Plus you'll get however many spaces are needed to pad out the $ symbol from the first numeral to make the $ symbols line up. Either way, you'd need to allow the same amount of space (not number of spaces) in the cell, unless you have no -ve values; in which case a Find for ' $' replaced by '$' would delete that space. You'll also get a space after the last digit, which a wildcard Find/Replace could eliminate.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 09-15-2015, 05:26 AM
peligroso peligroso is offline vba find numbers in document and align text left Windows 8 vba find numbers in document and align text left Office 2013
Novice
vba find numbers in document and align text left
 
Join Date: Sep 2015
Posts: 6
peligroso is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
A few questions:
1. Why an embedded Word document, rather than a Word template stored in the same folder as the workbook?
2. Does the Excel table always span the same range?
3. Why does your last code block loop through all tables in the document instead of processing just the one that's being inserted/updated?
Hi macropod in answer to your questions:

1. We send the template out to our field staff. its designed in such a way they can download in anywhere onto their PC / tablet and can run the program. Basically saves us writing a separate install program.

2. There are approximately 20 tables ad all bar one span the same range

3. No particular reason... just the way the program was coded.
Reply With Quote
  #10  
Old 09-15-2015, 05:27 AM
peligroso peligroso is offline vba find numbers in document and align text left Windows 8 vba find numbers in document and align text left Office 2013
Novice
vba find numbers in document and align text left
 
Join Date: Sep 2015
Posts: 6
peligroso is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
When I paste an Excel range containing both text numeric data into Word, the data retain the same formatting and alignments they had in Excel and there are no leading/trailing spaces. In this regard, I note the OP is just using an ordinary Paste, not Word's PasteExcelTable or PasteAndFormat methods, for example.
Thanks, I will try those methods and see how well they work.
Reply With Quote
  #11  
Old 09-15-2015, 05:29 AM
peligroso peligroso is offline vba find numbers in document and align text left Windows 8 vba find numbers in document and align text left Office 2013
Novice
vba find numbers in document and align text left
 
Join Date: Sep 2015
Posts: 6
peligroso is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Paul
On my machine, cells formatted as 'Accounting' brings a leading space into Word with an ordinary paste.

I remember having seen other cell formats that also bring multiple leading spaces but I believe this didn't happen with left aligned text.
Hi guessed,

I just checked, the current format is custom -> -* #,##0.00_-;-* #,##0.00_-;_-* "-"??_-;_-@_-

I'll try a few scenarios changing the format to see what happens.
Reply With Quote
  #12  
Old 09-15-2015, 06:10 AM
macropod's Avatar
macropod macropod is offline vba find numbers in document and align text left Windows 7 64bit vba find numbers in document and align text left Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You will get an almost identical visual result with:
#,##0.00_-;#,##0.00_-;"-"??_-;_-@_-
or, if you can do without the trailing space:
#,##0.00;#,##0.00;"-"??;_-@
Either of these will eliminate the space padding.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 09-17-2015, 03:09 AM
peligroso peligroso is offline vba find numbers in document and align text left Windows 8 vba find numbers in document and align text left Office 2013
Novice
vba find numbers in document and align text left
 
Join Date: Sep 2015
Posts: 6
peligroso is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
You will get an almost identical visual result with:
#,##0.00_-;#,##0.00_-;"-"??_-;_-@_-
or, if you can do without the trailing space:
#,##0.00;#,##0.00;"-"??;_-@
Either of these will eliminate the space padding.
Hi guys, thanks for all your help. Reformatting the source cells in excel worked the best.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
vba find numbers in document and align text left How to either move document display to right or left; or reduce size of find/replace window. pmstock Word 2 08-02-2014 08:34 PM
vba find numbers in document and align text left Find and Replace function shooting off screen to the left of the document Mattblack Word 1 04-10-2014 03:06 PM
vba find numbers in document and align text left Align...both left and right? supersimu Word 1 01-25-2014 02:05 AM
vba find numbers in document and align text left Numbers on the left side of the text Felipe Word 2 04-19-2012 07:35 AM
vba find numbers in document and align text left How to align page to left side instead of centre ghumdinger Word 2 08-18-2011 10:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:33 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft