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









share|improve this question























  • 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














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









share|improve this question























  • 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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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) 





share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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) 





share|improve this answer




















  • 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














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) 





share|improve this answer




















  • 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












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) 





share|improve this answer












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) 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 22:06









Cindy Meister

13.3k101934




13.3k101934











  • 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
















  • 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















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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)