issues when change word table column width and insert a picture into word from excel vba
up vote
1
down vote
favorite
I am trying to use vba to combine the text, table and picture into a word file from excel file. The below code works most of time. However, error 462 appears occasionally.
I searched around. Some comments said it appears because the unqualified references exist. However, I don't know what and where the unqualified reference is. A weird thing: usually the first running is ok. After that, this error usually appears in the even times, only. I am sure I closed the word file every time, no matter whether the code executes successfully or not.
Here is the code. I put the comment to indicate the line where the error occurs.
Dim WordApp As Object, WordDoc As Object
Set WordApp = New Word.Application
With WordApp
.Visible = True
Set WordDoc = .Documents.Add
.ActiveDocument.Select
With .Selection
'narrative
Sheet3.Range("G2").Copy
.PasteExcelTable False, False, False
'IO
Sheet2.Range("A1","B11").Copy
.PasteExcelTable False, False, False
WordDoc.Tables(1).Columns(1).Width = InchesToPoints(1.5) 'Error 462 appears periodically on this line
WordDoc.Tables(1).Columns(2).Width = InchesToPoints(4.5)
WordDoc.Tables(1).Rows.Height = 20
'HMI
Sheet9.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet9.Shapes("Group_Htr").Copy
.PasteSpecial
.ShapeRange(1).WrapFormat.Type = wdWrapInline
.EndKey unit:=wdStory
'P&ID
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
With .PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(11)
.PageHeight = InchesToPoints(17)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
If .Orientation = wdOrientPortrait Then
.Orientation = wdOrientLandscape
End If
End With
Sheet10.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet10.Shapes("Group_Htr").Copy
.PasteSpecial 'Error 4198 appears occasionally on this line
End With
.Activate
End With
excel vba ms-word
add a comment |
up vote
1
down vote
favorite
I am trying to use vba to combine the text, table and picture into a word file from excel file. The below code works most of time. However, error 462 appears occasionally.
I searched around. Some comments said it appears because the unqualified references exist. However, I don't know what and where the unqualified reference is. A weird thing: usually the first running is ok. After that, this error usually appears in the even times, only. I am sure I closed the word file every time, no matter whether the code executes successfully or not.
Here is the code. I put the comment to indicate the line where the error occurs.
Dim WordApp As Object, WordDoc As Object
Set WordApp = New Word.Application
With WordApp
.Visible = True
Set WordDoc = .Documents.Add
.ActiveDocument.Select
With .Selection
'narrative
Sheet3.Range("G2").Copy
.PasteExcelTable False, False, False
'IO
Sheet2.Range("A1","B11").Copy
.PasteExcelTable False, False, False
WordDoc.Tables(1).Columns(1).Width = InchesToPoints(1.5) 'Error 462 appears periodically on this line
WordDoc.Tables(1).Columns(2).Width = InchesToPoints(4.5)
WordDoc.Tables(1).Rows.Height = 20
'HMI
Sheet9.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet9.Shapes("Group_Htr").Copy
.PasteSpecial
.ShapeRange(1).WrapFormat.Type = wdWrapInline
.EndKey unit:=wdStory
'P&ID
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
With .PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(11)
.PageHeight = InchesToPoints(17)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
If .Orientation = wdOrientPortrait Then
.Orientation = wdOrientLandscape
End If
End With
Sheet10.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet10.Shapes("Group_Htr").Copy
.PasteSpecial 'Error 4198 appears occasionally on this line
End With
.Activate
End With
excel vba ms-word
When you have errors it's important to also give the text, not just the number. No one has memorized the text for thousands of possible error numbers...
– Cindy Meister
Nov 8 at 21:57
You should have only one question / Question - site rules. There are two reasons for this: 1) This is a Q&A site, not a forum and 2) No single person may know all the answers to all the questions. So you may get no answes, or only partial answers... and multiple partial answers is not good for the Q&A concept. I've edited your question to leave only one question - the first. Implement the answer I'll write, then see if that helps solve any of the other problems. If not, post new questions for each problem.
– Cindy Meister
Nov 8 at 22:01
Thanks for letting me know. Also, thank you for correcting my English.
– Peter Lau
Nov 9 at 21:43
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to use vba to combine the text, table and picture into a word file from excel file. The below code works most of time. However, error 462 appears occasionally.
I searched around. Some comments said it appears because the unqualified references exist. However, I don't know what and where the unqualified reference is. A weird thing: usually the first running is ok. After that, this error usually appears in the even times, only. I am sure I closed the word file every time, no matter whether the code executes successfully or not.
Here is the code. I put the comment to indicate the line where the error occurs.
Dim WordApp As Object, WordDoc As Object
Set WordApp = New Word.Application
With WordApp
.Visible = True
Set WordDoc = .Documents.Add
.ActiveDocument.Select
With .Selection
'narrative
Sheet3.Range("G2").Copy
.PasteExcelTable False, False, False
'IO
Sheet2.Range("A1","B11").Copy
.PasteExcelTable False, False, False
WordDoc.Tables(1).Columns(1).Width = InchesToPoints(1.5) 'Error 462 appears periodically on this line
WordDoc.Tables(1).Columns(2).Width = InchesToPoints(4.5)
WordDoc.Tables(1).Rows.Height = 20
'HMI
Sheet9.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet9.Shapes("Group_Htr").Copy
.PasteSpecial
.ShapeRange(1).WrapFormat.Type = wdWrapInline
.EndKey unit:=wdStory
'P&ID
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
With .PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(11)
.PageHeight = InchesToPoints(17)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
If .Orientation = wdOrientPortrait Then
.Orientation = wdOrientLandscape
End If
End With
Sheet10.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet10.Shapes("Group_Htr").Copy
.PasteSpecial 'Error 4198 appears occasionally on this line
End With
.Activate
End With
excel vba ms-word
I am trying to use vba to combine the text, table and picture into a word file from excel file. The below code works most of time. However, error 462 appears occasionally.
I searched around. Some comments said it appears because the unqualified references exist. However, I don't know what and where the unqualified reference is. A weird thing: usually the first running is ok. After that, this error usually appears in the even times, only. I am sure I closed the word file every time, no matter whether the code executes successfully or not.
Here is the code. I put the comment to indicate the line where the error occurs.
Dim WordApp As Object, WordDoc As Object
Set WordApp = New Word.Application
With WordApp
.Visible = True
Set WordDoc = .Documents.Add
.ActiveDocument.Select
With .Selection
'narrative
Sheet3.Range("G2").Copy
.PasteExcelTable False, False, False
'IO
Sheet2.Range("A1","B11").Copy
.PasteExcelTable False, False, False
WordDoc.Tables(1).Columns(1).Width = InchesToPoints(1.5) 'Error 462 appears periodically on this line
WordDoc.Tables(1).Columns(2).Width = InchesToPoints(4.5)
WordDoc.Tables(1).Rows.Height = 20
'HMI
Sheet9.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet9.Shapes("Group_Htr").Copy
.PasteSpecial
.ShapeRange(1).WrapFormat.Type = wdWrapInline
.EndKey unit:=wdStory
'P&ID
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
With .PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(11)
.PageHeight = InchesToPoints(17)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
If .Orientation = wdOrientPortrait Then
.Orientation = wdOrientLandscape
End If
End With
Sheet10.Range("A4").Copy
.PasteExcelTable False, False, False
Sheet10.Shapes("Group_Htr").Copy
.PasteSpecial 'Error 4198 appears occasionally on this line
End With
.Activate
End With
excel vba ms-word
excel vba ms-word
edited Nov 8 at 22:08
Cindy Meister
13.3k101934
13.3k101934
asked Nov 8 at 20:09
Peter Lau
83
83
When you have errors it's important to also give the text, not just the number. No one has memorized the text for thousands of possible error numbers...
– Cindy Meister
Nov 8 at 21:57
You should have only one question / Question - site rules. There are two reasons for this: 1) This is a Q&A site, not a forum and 2) No single person may know all the answers to all the questions. So you may get no answes, or only partial answers... and multiple partial answers is not good for the Q&A concept. I've edited your question to leave only one question - the first. Implement the answer I'll write, then see if that helps solve any of the other problems. If not, post new questions for each problem.
– Cindy Meister
Nov 8 at 22:01
Thanks for letting me know. Also, thank you for correcting my English.
– Peter Lau
Nov 9 at 21:43
add a comment |
When you have errors it's important to also give the text, not just the number. No one has memorized the text for thousands of possible error numbers...
– Cindy Meister
Nov 8 at 21:57
You should have only one question / Question - site rules. There are two reasons for this: 1) This is a Q&A site, not a forum and 2) No single person may know all the answers to all the questions. So you may get no answes, or only partial answers... and multiple partial answers is not good for the Q&A concept. I've edited your question to leave only one question - the first. Implement the answer I'll write, then see if that helps solve any of the other problems. If not, post new questions for each problem.
– Cindy Meister
Nov 8 at 22:01
Thanks for letting me know. Also, thank you for correcting my English.
– Peter Lau
Nov 9 at 21:43
When you have errors it's important to also give the text, not just the number. No one has memorized the text for thousands of possible error numbers...
– Cindy Meister
Nov 8 at 21:57
When you have errors it's important to also give the text, not just the number. No one has memorized the text for thousands of possible error numbers...
– Cindy Meister
Nov 8 at 21:57
You should have only one question / Question - site rules. There are two reasons for this: 1) This is a Q&A site, not a forum and 2) No single person may know all the answers to all the questions. So you may get no answes, or only partial answers... and multiple partial answers is not good for the Q&A concept. I've edited your question to leave only one question - the first. Implement the answer I'll write, then see if that helps solve any of the other problems. If not, post new questions for each problem.
– Cindy Meister
Nov 8 at 22:01
You should have only one question / Question - site rules. There are two reasons for this: 1) This is a Q&A site, not a forum and 2) No single person may know all the answers to all the questions. So you may get no answes, or only partial answers... and multiple partial answers is not good for the Q&A concept. I've edited your question to leave only one question - the first. Implement the answer I'll write, then see if that helps solve any of the other problems. If not, post new questions for each problem.
– Cindy Meister
Nov 8 at 22:01
Thanks for letting me know. Also, thank you for correcting my English.
– Peter Lau
Nov 9 at 21:43
Thanks for letting me know. Also, thank you for correcting my English.
– Peter Lau
Nov 9 at 21:43
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
The reason you get the error is because InchesToPoints
is not part of Excel, which is where the code is running. This method is specific to Word, so it requires a reference to the Word.Application. Put a .
in front of each occurrence so that it picks up WordApp
from the With
:
WordDoc.Tables(1).Columns(1).Width = .InchesToPoints(1.5)
I put aWordApp.
in front of.InchesToPints()
and it works well. Looks like putting a.
will let the code pick up.Selection
or.PageSetup
, notWordApp
, from thewith
. Thank you, Cindy.
– Peter Lau
Nov 12 at 20:14
@PeterLau Ah, yes, because it's within a secondWith
- I missed that. Glad you figured it out!
– Cindy Meister
Nov 15 at 12:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The reason you get the error is because InchesToPoints
is not part of Excel, which is where the code is running. This method is specific to Word, so it requires a reference to the Word.Application. Put a .
in front of each occurrence so that it picks up WordApp
from the With
:
WordDoc.Tables(1).Columns(1).Width = .InchesToPoints(1.5)
I put aWordApp.
in front of.InchesToPints()
and it works well. Looks like putting a.
will let the code pick up.Selection
or.PageSetup
, notWordApp
, from thewith
. Thank you, Cindy.
– Peter Lau
Nov 12 at 20:14
@PeterLau Ah, yes, because it's within a secondWith
- I missed that. Glad you figured it out!
– Cindy Meister
Nov 15 at 12:58
add a comment |
up vote
0
down vote
accepted
The reason you get the error is because InchesToPoints
is not part of Excel, which is where the code is running. This method is specific to Word, so it requires a reference to the Word.Application. Put a .
in front of each occurrence so that it picks up WordApp
from the With
:
WordDoc.Tables(1).Columns(1).Width = .InchesToPoints(1.5)
I put aWordApp.
in front of.InchesToPints()
and it works well. Looks like putting a.
will let the code pick up.Selection
or.PageSetup
, notWordApp
, from thewith
. Thank you, Cindy.
– Peter Lau
Nov 12 at 20:14
@PeterLau Ah, yes, because it's within a secondWith
- I missed that. Glad you figured it out!
– Cindy Meister
Nov 15 at 12:58
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The reason you get the error is because InchesToPoints
is not part of Excel, which is where the code is running. This method is specific to Word, so it requires a reference to the Word.Application. Put a .
in front of each occurrence so that it picks up WordApp
from the With
:
WordDoc.Tables(1).Columns(1).Width = .InchesToPoints(1.5)
The reason you get the error is because InchesToPoints
is not part of Excel, which is where the code is running. This method is specific to Word, so it requires a reference to the Word.Application. Put a .
in front of each occurrence so that it picks up WordApp
from the With
:
WordDoc.Tables(1).Columns(1).Width = .InchesToPoints(1.5)
answered Nov 8 at 22:06
Cindy Meister
13.3k101934
13.3k101934
I put aWordApp.
in front of.InchesToPints()
and it works well. Looks like putting a.
will let the code pick up.Selection
or.PageSetup
, notWordApp
, from thewith
. Thank you, Cindy.
– Peter Lau
Nov 12 at 20:14
@PeterLau Ah, yes, because it's within a secondWith
- I missed that. Glad you figured it out!
– Cindy Meister
Nov 15 at 12:58
add a comment |
I put aWordApp.
in front of.InchesToPints()
and it works well. Looks like putting a.
will let the code pick up.Selection
or.PageSetup
, notWordApp
, from thewith
. Thank you, Cindy.
– Peter Lau
Nov 12 at 20:14
@PeterLau Ah, yes, because it's within a secondWith
- I missed that. Glad you figured it out!
– Cindy Meister
Nov 15 at 12:58
I put a
WordApp.
in front of .InchesToPints()
and it works well. Looks like putting a .
will let the code pick up .Selection
or .PageSetup
, not WordApp
, from the with
. Thank you, Cindy.– Peter Lau
Nov 12 at 20:14
I put a
WordApp.
in front of .InchesToPints()
and it works well. Looks like putting a .
will let the code pick up .Selection
or .PageSetup
, not WordApp
, from the with
. Thank you, Cindy.– Peter Lau
Nov 12 at 20:14
@PeterLau Ah, yes, because it's within a second
With
- I missed that. Glad you figured it out!– Cindy Meister
Nov 15 at 12:58
@PeterLau Ah, yes, because it's within a second
With
- I missed that. Glad you figured it out!– Cindy Meister
Nov 15 at 12:58
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215400%2fissues-when-change-word-table-column-width-and-insert-a-picture-into-word-from-e%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When you have errors it's important to also give the text, not just the number. No one has memorized the text for thousands of possible error numbers...
– Cindy Meister
Nov 8 at 21:57
You should have only one question / Question - site rules. There are two reasons for this: 1) This is a Q&A site, not a forum and 2) No single person may know all the answers to all the questions. So you may get no answes, or only partial answers... and multiple partial answers is not good for the Q&A concept. I've edited your question to leave only one question - the first. Implement the answer I'll write, then see if that helps solve any of the other problems. If not, post new questions for each problem.
– Cindy Meister
Nov 8 at 22:01
Thanks for letting me know. Also, thank you for correcting my English.
– Peter Lau
Nov 9 at 21:43