Unable to copy a table from a word document and paste it in to a excel sheet in VB.NET - vb.net

Following is the code snippet i tried
r.Cells(3).Range.Copy()
.Sheets("sheet4").Range("F" & k).Select()
.Sheets("sheet4").Cells(k, 6).Columnwidth = 14
.Sheets("sheet4").paste()
k = k + 1
this doesnt copy the entire string inside one cell.I tried to use the pastespecial()
when i use pastespecial its showing error that pastespecial is not a member of Application Class
.Sheets("sheet4").Range("F" & k).pasteSpecial(ExApp.XlPasteType.xlPasteAll, False, False)
Any help would be appreciated.

I haven't ever tried this before, but it seems to be an Excel-Word coordination issue. From my MS Office coupling experience, I know that there are many alternatives, which theoretically should be fine, provoking errors. Instead spending time on finding out a working alternative (perhaps there is none), why not removing the problem (Excel-Word communication)? You can store the Word value in a temporary variable and write this variable to the Excel cell.

Related

Dynamically select Excel cells with VB .Net in a VS 2010 project

I'm using Visual Studio 2010, to create a project from a Excel Workbook template. VS2010 already gives me a workbook with one worksheet. I added another one. The project consist of the following files:
ThisWorkbook.vb
Sheet1.vb
Sheet2.vb
Each file consist of class for each object: Sheet1.vb has its Sheet1 class, Sheet2.vb Sheet2 class and so on.
From MSDN help and another sources, I understood I can access from on vb file, or another class, the other class objects using Globals statement:
Me.Range("A10").Value = "Validation time:"
Me.Range("B10").Value = ValidationTime_T1.Item(ValidationTime_T1.Count - 1) - ValidationTime_T0.Item(ValidationTime_T0.Count - 1)
Dim x As Double
Dim rowOffset As Integer
rowOffset = 3
For x = 0 To JobCounter
Globals.Sheet2.Cells(x + rowOffset, 1) = x
Globals.Sheet2.Cells(x + rowOffset, 2) = ASy_Start_Mem.Item(x)
Globals.Sheet2.Cells(x + rowOffset, 3) = WSZ_Start_Mem.Item(x)
Globals.Sheet2.Cells(x + rowOffset, 4) = WSZ_Start_Pk_Mem.Item(x)
Globals.Sheet2.Cells(x + rowOffset, 5) = PFU_Start_Mem.Item(x)
As you can see, I move data from one sheet to another, or put data that is in memory to sheet2 from sheet1.
So far, so good. There were not that many issues I could not solve by my self.
I think is good to point out here that my background is C, C++, Perl and VBA in the last 4 years, so I'm struggling with VB since quite a bit already, I find .Net more useful, until I had to use it with Excel, it is giving me a hard time.
Now, I need to select specific cells base on positions and it is not always the same position so I need to select them dynamically, NO HARD CODED, as I have found in MSDN, forums, StackOverFlow and different sites, the solution they give is to use:
Globals.Sheet2.Range("C7").Select()
And yeah, that's fine for specific cells for which you use a string to select.
The problem is that I need to select different cells every time depending on the each day data, andt I don't know how to do it with Range. In the previous code you can see I used Cells for that.
I've tried:
Globals.Sheet2.Range(Globals.Sheet2.Cells(2, 2), Globals.Sheet2.Cells(6, 3)).Select()
Because one of Range definitions states that I can receive as parameter two Cells objects, but gives an error. I've tried creating Range objects for the cell positions. I have used Object object with CType, and it also fails.
If anyone could help how I can dynamically select cells, either with Range or Cells, or both, or if you have another solution, I will gladly appreciate it.
First thanks to TnTinMn, for replying, and I apologize for the late response again, it's been crazy at work.
Well, I found the solution thanks to your comment.
There seems to be a known issue with Excel.Interop and Visual Basic .Net called "two dots".
Normally, when you go programming VB in VS write a few letter and IntelliSense prompts you with what may be available (property, method or object) then you enter a dot and another list comes up showing you what’s available, once you chose the previous two categories you either assign them to a variable or object, or given them a value. But you can still put another dot and get another list, here is where VB get messed up and not working, probably VS will let you put whenever amount of dos, but at runtime you'll get the error I got.
Hope an experienced StackOverflow fellow can explain this much better.
So the solution for the Select method:
First you need to make the sheet active, then you can use the Select method, as TnTinMn predicted the error was with the Select method.
Globals.Sheet2.Activate()
Globals.Sheet2.Range(Globals.Sheet2.Cells(2, 2), Globals.Sheet2.Cells(6, 3)).Select()
And now Select method does not throw the runtime error.
With the previous code you can now have an option to go through any cell without hard coding.
The following can also be used to store data ranges from different section, to later use in chart for example:
Dim dataX As Excel.Range
Dim dataY As Excel.Range
Dim dataRange As Excel.Range
dataX = Globals.Sheet6.Cells(xRow, XColumn)
dataY = Globals.Sheet6.Cells(yRow, yColumn)
dataRange = Application.Union(dataX, dataY)
Thank you and hope this help someone.

cant get this formula to work, although the top and bottom code works fine

I did a search on this site, but can't find an answer to my problem. I am using VB 2010 Express. I'm trying to write formulas into a Excel spreadsheet.
oWorkSheet = oBook.Sheets(STRName)
'my variables are all declared strings
'this section works
FMLAValue1 = "=-L8"
FMLAValue2 = "=SUM($I$7;$F$8:$H$8)"
FMLAValue3 = "=SUM(M8:U8)"
oWorkSheet.Range("H8").Select()
oWorkSheet.Range("H8").Formula = FMLAValue1
oWorkSheet.Range("H8").AutoFill (oWorkSheet.Range("H8:H40"))
'up to here
'now this is the problem code below: if I paste the formula into a
'excel sheet cell, it works.
oWorkSheet.Range("I8").Select()
oWorkSheet.Range("I8").Formula = FMLAValue2
'**the line above gives an "Exception from HRESULT: 0x800A03EC "**
oWorkSheet.Range("I8").AutoFill (oWorkSheet.Range("I8:I40"))
'this part also works
oWorkSheet.Range("L8").Select()
oWorkSheet.Range("L8").Formula = FMLAValue3
oWorkSheet.Range("L8").AutoFill (oWorkSheet.Range("L8:L40"))
oWorkSheet.Range("J7").Select()
oWorkSheet.Range("J7").Formula = "=I7"
The formula stored in your FMLAValue2 variable doesn't look correct, you shouldn't have a ; (semi-colon) to separate the numbers, you should use a , (comma).
Use this instead:
FMLAValue2 = "=SUM($I$7,$F$8:$H$8)"
and you should hopefully be ok, unless there are other issues somewhere that I haven't noticed.
To test whether a formula is correct, use FormulaDesk. It will immediately tell you if there is an error, and pinpoint exactly where it is in your formula. You can then paste the formula into your VBA code, knowing that it works.
The image below shows how it can pinpoint exactly where your error is within the formula. So in your case, paste the non-working error into a cell in Excel, then view the error report in FormulaDesk to find out the exact cause.
[Disclosure: I am the author of FormulaDesk. I think it will definitely help the poster find any errors]

VBA Excel + using match function

I'm having some kind of problem in a piece of code.
I need to find values in a excel book and then copy it to another. The thing is that i need to copy it concept by concept in the right place. The problem is that the file doesnt come every week in the same order.
So i need to find the concept and then copy the next cell that is the value of that concept.
First of all i use need to locate the correct line so i can start copying (this part is easy and its done).
Secondly, having the correct line, i need to find the concept, which in this example im going to use the "619".
After having find the location of this value i store the value in "c_audiovisual".
On Error GoTo ErrhandlerCAV
lRowC_AV = Application.WorksheetFunction.Match(619, Range("A" & line & ":FI" & line), 0) + 1
'On Error GoTo ErrhandlerCAV
Continue:
If errorCAV = 1 Then
c_audiovisual = 0
errorCAV = 0
Else
c_audiovisual = ActiveSheet.Cells(line+ m2, lRowC_AV).Value
End If
I've made a escape in case that this concept isn't present in that line (which sometimes occurs).
Sometimes this piece of code works, and other it doesn't.
When i'm on debug mode (pressing F8) it works. And when i use small files to look for the values it works. On bigger files sometimes don't.
Any ideas?
I faced similar issues. I fixed it with using ThisWorkbook.Sheets("MySheet").Range(...) inside the Match function. Or try ActiveWorkbook as applicable. Let me know if this worked. Curious to know.
Btw I noticed column and row number both are "line". Is that a mistake?

excel Compile error: Can't assign to array

I am working on an Excel macro that uses several external references. Once in a while I get the error:
excel Compile error:
Can't assign to array
The highlighted line is an array assignments, not always the same. Something like:
Dim MyArray() As MyClass
MyArray = MyApplication.GetArrayOfElements
I get this error while the file is open on my computer once or twice per hour, after editing the VBA macro, especially (but not only) if I edit it while in break mode.
I usually compile before saving to make sure this problem is not there, but some users call me reporting this problem on their computer. So it looks like it breaks somewhere between my compilation and the next execution on another computer.
In order to get rid of the error all I need to do is to select all, cut, paste and compile (Ctrl+A, Ctrl+X, Ctrl+V, Alt+D, L.)
My wild guess is that it has to do with the VBA incremental compilation.
Anybody out there can help me get rid of this problem?

VBA - Unable to get TextBoxes Property of Worksheet Class

Fairly new to VBA and I just started encountering the error Unable to get TextBoxes Property of Worksheet Class.
It highlights the following line:
ActiveSheet.TextBoxes("txtFilePath").Text = Application.ActiveWorkbook.Path & "\"
The mystery to me is that I know this line was working before without any problems. The only thing I could think of is that I was messing with protecting the sheet, but even now that it is unprotected I still get the error. I've also tried the following solution, but encounter the same error:
ActiveSheet.OLEObjects("txtFilePath").Object.Text = Application.ActiveWorkbook.Path & "\"
Can anyone explain to me why I am encountering the error and why I started encountering it so randomly? How can I fix it?
Sadly enough, there is no clear way on how to access the properties of a grouped shape (a group is basically a shape, at least in 2010). There are two possible solutions to go about this.
One is obviously to ungroup the textboxes, access the textbox in question and modify it, then regroup them. However, this will prove to be difficult to track as the Group number increases with every regroup that you do. This can pose a potential problem if you have multiple groupings.
The other way, surprisingly enough, is to do it the 'dirty' way, which is basically what the macro recorder gives us in a nutshell. The way to do it is to select the shape itself and to change the text directly, emulating a direct click-and-type motion manually. I've tested it and it works, even when the textbox in question is grouped multiple times.
Sub Macro3()
foo = Application.ActiveWorkbook.Path & "\"
ActiveSheet.Shapes.Range(Array("txtFilePath")).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = foo
End Sub
Sorry to answer my own question. Figured it out by accident.
The problem was that I had my textbox (txtFilePath) grouped together with another textbox on my worksheet. When I ungrouped them everything worked fine. Can anyone explain to me why the grouping would make a difference?